Android notifications are not released

Asked

Viewed 115 times

2

I use RTP_WAKEUP to "wake up" my device when it realizes that it has notifications to release and in fact I can release notifications for 10/15 or even 20 minutes, but when I try to release a notification in 2 hours or even days this does not occur...

I leave here part of the code where I draw up the whole process. What’s wrong with me for this to happen?

Class where I create the alarm, to launch the notification.

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.DAY_OF_MONTH, 25);
            calendar.set(Calendar.MONTH, 6);
            calendar.set(Calendar.YEAR, 2017);
            calendar.set(Calendar.HOUR_OF_DAY, 20);
            calendar.set(Calendar.MINUTE, 40);
            calendar.set(Calendar.SECOND, 07);

            // Obtém um alarm manager
            AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(getContext().ALARM_SERVICE);

            // O id a ser usado no pending intent
            int id = (int) System.currentTimeMillis();

            // Prepare the intent which should be launched at the date
            Intent intent = new Intent(getContext(), CriarNotificacao.class);
            intent.putExtra("id", id);

            // Obtém o pending intent
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // Regista o alerta no sistema.
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

                    Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.DAY_OF_MONTH, 25);
            calendar.set(Calendar.MONTH, 6);
            calendar.set(Calendar.YEAR, 2017);
            calendar.set(Calendar.HOUR_OF_DAY, 20);
            calendar.set(Calendar.MINUTE, 40);
            calendar.set(Calendar.SECOND, 07);

            // Obtém um alarm manager
            AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(getContext().ALARM_SERVICE);

            // O id a ser usado no pending intent
            int id = (int) System.currentTimeMillis();

            // Prepare the intent which should be launched at the date
            Intent intent = new Intent(getContext(), CriarNotificacao.class);
            intent.putExtra("id", id);

            // Obtém o pending intent
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // Regista o alerta no sistema.
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Class where I make the notification

  public class CriarNotificacao extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {

                Bundle extras = intent.getExtras();
                int id = extras.getInt("id");

                PendingIntent resultPendingIntent =
                        PendingIntent.getActivity(context,
                                0,
                                intent,
                                PendingIntent.FLAG_CANCEL_CURRENT
                        );

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                        .setContentTitle("titulo")
                        .setContentText("mensagem")
                        .setContentIntent(resultPendingIntent)
                        .setAutoCancel(true);
                mBuilder.setSmallIcon(R.drawable.ic_cake);

                Intent resultIntent = new Intent(context, MainActivity.class);
                resultIntent.putExtra("id", String.valueOf(id));

                mBuilder.setContentIntent(resultPendingIntent);

                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(1500);

                mNotificationManager.notify(id, mBuilder.build());
            }
        }
  • The problem is the device not "waking up", only "waking up" when the period is small (10/15 or up to 20 minutes) or has nothing to do with the "waking up", but rather because the period is long (2 hours or even days)?

  • @ramaral when the time exceeds a longer time, ie when I try to schedule a notification to from here for example +40 minutes it just does nothing, the notification does not appear

  • @ramaral and when I open the app again the notification (the one I scheduled for the +40 minutes) that was not shown appears...although the time that should have appeared has already expired

  • the strangest thing is that I have already put a relatively large interval about 20 minutes, and the application even not being open the notification appeared at the scheduled time, I very soon tested for the 40 minutes and nothing happened....

  • What I want you to clarify is whether this has anything to do with the device being "awake" or not.

  • @ramaral I think so...

  • I think the phone only wakes up when time is relatively "small"

Show 2 more comments

1 answer

1

Instead of a Broadcastreceiver use an Intentservice to launch the notification.

public class CriarNotificacao extends IntentService {

    private PowerManager.WakeLock wakeLock;

    public CriarNotificacao() {
        super("name");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "BootService");
        wakeLock.acquire();
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        //Lance a notificação aqui.
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    if(wakeLock.isHeld()){
        //Verificou-se que o iluminar do ecrã
        //não acontecia devido ao WakeLock ser
        //rapidamente libertado(apesar de PowerManager.ON_AFTER_RELEASE !?).
        try {
            //Atrasa a libertação do WakeLock
            //de forma a permitir a iluminação do ecrâ.
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            wakeLock.release();
        }

    }
}

The service uses Powermanager to purchase a Wakelock to allow the notification to be launched.

You need to change the way you get Pendingintent by using getService() instead of getBroadcast():

PendingIntent pendingIntent = PendingIntent.getService(getContext(), id, intent,
                                                       PendingIntent.FLAG_UPDATE_CURRENT);

Add the permission

uses-permission android:name="android.permission.WAKE_LOCK"

at the Androidmanifest.xml

  • first of all I want to thank you for all your willingness to try to help me.

  • Before testing what you suggested to me, I’ve scheduled two notifications, in different places that use the same "Create Notification" class, at approximately 9:30 a.m., I scheduled one for 10:05 and one for 10:02 a.m., the weirdest thing I’m worried about is that the 10:02 didn’t show up on time because, And the 10:05 showed up at the right time. The most caricato of all, in my opinion, is that the notification that should have appeared at 10:02 a.m.

  • This behavior is possible for applications with targetSdkVersion >= 19. If you want the alarm to be "accurate" instead of alarmManager.set(), use alarmManager.setExact().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.