2

I am trying to cancel a notification I have requested to be sent to the user if another notification is trying to be sent within 15 seconds of the first one.

This is my code:

Global variable:

    public NotificationManager nm;

Notify function:

    final NotificationCompat.Builder b = new NotificationCompat.Builder(this);

    b.setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setContentTitle(title)
            .setContentText(message);

    if (nm != null) {
        Log.d(TAG, "notifyThis: cancelled");
        nm.cancelAll();
    } else {
        Log.d(TAG, "notifyThis: not cancelled");
    }

    nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            nm.notify(1, b.build());
            Log.d(TAG, "notifyThis: notify");

        }
    }, 15000);

I've noticed that nm remains null until the notification is posted so this method doesn't work, but I need a way to remove a notification after creating the notification, and before it is posted by .notify.

Thanks.

Haris
  • 521
  • 1
  • 6
  • 14
  • Please show a complete example. You should include the class and method declarations. – Code-Apprentice Mar 07 '17 at 12:51
  • You can call alarmManager.cancel(pendingIntent); Read more here: http://stackoverflow.com/questions/30075196/how-can-i-cancel-unshown-notifications-in-android – EtherPaul Mar 07 '17 at 13:03

1 Answers1

1

Ideally you don't want to rely on the null state of a variable for something like this.
Instead, the Handler class has methods to remove previously scheduled tasks. For this you need to keep the references to both the Handler and Runnable objects.

private Handler handler = new Handler();
private boolean isPosted = false;
private Runnable notificationRunnable;

void doNotification() {
    final NotificationCompat.Builder b = {...}

    if(isPosted) {
        handler.removeCallbacks(notificationRunnable);
        isPosted = false;
    }
    else {
        notificationRunnable = new Runnable() {
            @Override
            public void run() {
                nm.notify(1, b.build());
                Log.d(TAG, "notifyThis: notify");
            }
        };  
        handler.postDelayed(notificationRunnable, 15000);
        isPosted = true;  
    }  
}
RobCo
  • 6,240
  • 2
  • 19
  • 26
  • This answer is great but the only problem is that I lose my reference to the objects as this function is within a IntentService class (which I forgot to mention) so whenever it is called it creates a new reference to the objects. Is there any way to keep a reference to objects inside an IntentService? – Haris Mar 07 '17 at 18:53
  • Forgot to tag @RobCo – Haris Mar 07 '17 at 19:02