Android notifications are not released

2

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

I leave here part of the code where I work out the whole process. What is wrong with this?

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 launch 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());
            }
        }
    
asked by anonymous 28.06.2017 / 22:57

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 acquire a WakeLock to allow the notification to be released.

You have to change the way you get PendingIntent, 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"

AndroidManifest.xml

    
29.06.2017 / 11:29