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?
Yes , it is possible
mNotificationManager.getActiveNotifications()
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);
}
}
}
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With