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

Viewed 3625

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?

4 Answers

Seems possible if you use notification groups. Cancelling the group summary notification seems to cancel the entire group.

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);
        }

    }
}
Related