Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to cancel all notifications that have a certain tag?

I'm making an android app and I would like to cancel all notifications that have a certain tag.

Right now it only seems possible to cancel notifications by their id (int id) or by both their ids and tags.

mNotificationManager.cancel(int id);

or

mNotificationManager.cancel(String tag, int id);

I want to be able to cancel all notifications of String tag regardless of int id.

Is this possible?

like image 278
Abe Avatar asked Mar 12 '16 17:03

Abe


2 Answers

Yes , it is possible

  1. Get all active notification build by your app using mNotificationManager.getActiveNotifications()
  2. Filter Notification based on TAG.
  3. Cancel those notifications.

Try below code

void cancelGivenTagNotification(String tag){
    StatusBarNotification notiList[] = notificationManager.getActiveNotifications();
    for(int i=0;i<notiList.length;i++){
        if(notiList[i].getTag().equals(tag)){
            int notiId = notiList[i].getId();
            notificationManager.cancel(tag,notiId);
        }

    }
}
like image 181
Mayank Sahu Avatar answered Sep 23 '22 20:09

Mayank Sahu


On Android using API >= 23 you can do something like this to remove a group of notifications:

for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
    if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
        mNotificationManager.cancel(statusBarNotification.getId());
    }
}
like image 35
Szabolcs Becze Avatar answered Sep 22 '22 20:09

Szabolcs Becze