Home » Android » android – Is it possible to cancel all notifications that have a certain tag?

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

Posted by: admin June 16, 2020 Leave a comment

Questions:

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?

How to&Answers:

No, it isn’t. The way Notification is set up, id is the primary key. You can add a subkey with tag, and cancel an individual (tag, id) pair that way, but you can’t cancel based on tag alone. Really I’m not sure why they added a tag parameter, as it seems rather redundnt, except that possibly a string parameter makes for easier debugging.

Answer:

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

Answer:

On Android API >=23 you can do somehting like this to remove a group of notifications.
for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
mNotificationManager.cancel(statusBarNotification.getId());
}
}