NotificationManager.cancel(id) is not working inside a broadcast receiver

后端 未结 6 926
温柔的废话
温柔的废话 2020-12-16 00:49

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 MyBr         


        
6条回答
  •  醉酒成梦
    2020-12-16 01:00

    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.

提交回复
热议问题