12

Android: I am trying to cancel a notification from the notification bar after a package being installed. What I am doing is the following:

 public class MyBroadcastReceiver extends BroadcastReceiver {

                private static final String TAG = "MyBroadcastReceiver";

                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
                        Uri data = intent.getData();
                        //some code goes here
                        //get the id of the notification to cancel in some way
                        notificationhelper._completeNotificationManager.cancel(id);     
                        }
                }
            }

where

public class notificationhelper {
    public static NotificationManager _completeNotificationManager = null;

    public void complete() {        
        if (_completeNotificationManager == null)
            _completeNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(
                R.drawable.notification,
                _context.getString(R.string.notification),
                System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        _completeNotificationManager.notify(TEXT, id, notification);
    }
}

But the notificationhelper._completeNotificationManager.cancel(id) does not work. I tried to use notificationhelper._completeNotificationManager.cancelAll(); and it works. What I am doing wrong?

Nikolai Samteladze
  • 7,337
  • 4
  • 41
  • 70
b.i
  • 1,007
  • 4
  • 21
  • 42

6 Answers6

24

In my experience, you can't cancel all notifications with a particular ID, regardless of tag.

That is, if you create two notifications like so:

notificationManager.notify(TAG_ONE, SAME_ID, notification_one);
notificationManager.notify(TAG_TWO, SAME_ID, notification_two);

Then, notificationManager.cancel(SAME_ID) won't cancel either of them! I suspect that this is because the "tag" field, if unspecified in notify() and cancel(), defaults to null, which you have to cancel explicitly.

So, to cancel these two notifications, you have to call:

notificationManager.cancel(TAG_ONE, SAME_ID);
notificationManager.cancel(TAG_TWO, SAME_ID);

In your case, you're supplying "TEXT" as the tag but cancelling just using the id, which defaults to using tag=null.

So, either don't provide TEXT as your tag:

_completeNotificationManager.notify(id, notification);

Or, if you need separate notifications and don't want them to clobber each other, keep track of the active tags:

_completeNotificationManager.notify(TEXT, id, notification);
collectionOfActiveTags.add(TEXT);

...

for (String activeTag : collectionOfActiveTags)    
    notificationhelper._completeNotificationManager.cancel(activeTag, id);

I wish that what you're trying to do was supported, as it seems that it should be.

spitzanator
  • 1,789
  • 4
  • 17
  • 27
  • ("you can't cancel all notifications with a particular ID") Note ... you can, because need assing a "tag unic to all notif" with a id unic to each notificacions. –  Nov 18 '15 at 11:18
10

Well this is probably irrelevant at this point, but it should be posted here so that people like me dealing with the same problem might find the solution.

If NotificationManager.cancel() isn't working, try changing the ID for the notification.

notificationManager.notify(NOTIFICATION_ID, notification);

When I changed NOTIFICATION_ID from 1 to [RANDOM_NUMBER], it magically started working. I assume that 1 is somehow reserved, although there is no note in any documentation...

An of course make sure you use the same NOTIFICATION_ID to cancel:

notificationManager.cancel(NOTIFICATION_ID);
Stephen
  • 101
  • 1
  • 3
  • I was scratching my head around same problem. stackoverflow.com/questions/38880330 Saved my day. – mauron85 Aug 10 '16 at 21:42
  • 1
    i ma generating a random number of 7 digit an still notification concel is not working :/ – Adeel Turk Mar 24 '17 at 12:15
  • Just a comment: If you use a random number, there is a slight chance that at some point you'll have a duplicate notification id and the cancel() method will fail again. It would be better to use a unique id for notifications. – checkmate711 Nov 15 '17 at 15:27
8

My notifications were not getting removed because my service was Foreground Service and NOT a regular service started by StartService.

If your service is foreground, call stopForeground(true) instead of stopself(). So now my code looks like this:

NotificationManagerCompat.from(this).cancel(NotificationHelper.PLAYER_NOTIFICATION_ID);
stopForeground(true);

and it worked, notification was removed.

Ajji
  • 2,721
  • 2
  • 26
  • 28
3

I was facing the same issue recently. I have managed to solve it.

So from what i understood.

1) use the id which is basically a random number to notify and send this same id to the piece of code(reciever/activity...) where you want to cancel it.

2) When using tags, it seems to not work for me as i was giving one tag to all notifications but with unique id.It worked only on the first tag. so i completely avoided using tags. If you want to use tags, issue unique tags along with unique id and use them both while cancelling.

So final answer... what i used and what works for me:

STEP 1:
int notif_id = (int)(System.currentTimeMillis()%10000);

STEP2: add this id inside the action intent (I am launching an activity where the notification gets cancelled on the action click):

                Intent notificationSettingsIntent = new Intent(context.getApplicationContext(), NotificationSettingsActivity.class);
                notificationSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                notificationSettingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                notificationSettingsIntent.putExtra("fromNotification",true);
                notificationSettingsIntent.putExtra("notif_id",notif_id);
                PendingIntent notificationSettingsActivityPendingIntent = PendingIntent.getActivity(context,notif_id,notificationSettingsIntent,PendingIntent.FLAG_ONE_SHOT);

STEP 3: notify using the id in the step 1 but with no tags

NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());

notificationCompat.notify(notif_id,notificationBuilder.build());

Now in the Activity which gets opened by my action click, i cancel the notification as:

NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());

notificationCompat.cancel(getIntent().getIntExtra("notif_id"));

Works every time now.

Kushan
  • 5,275
  • 2
  • 27
  • 41
1

Sorry for late joining! But following worked fine for me.

NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
mNotificationManager.cancel("<TAG>",<Notificatoin-id>);
Naimish Vinchhi
  • 677
  • 8
  • 11
0

Following worked for me:

final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
mNotificationManager.cancel(<Notificatoin-id>);
Sagar
  • 20,467
  • 4
  • 50
  • 56