I have created a notification for group summary, which may contain many notifications.
Those notifications have some actions added by addAction()
.
I try to cancel the notification after a action has been made:
NotitifactionCompat.from(context).cancel(notificationId);
Unfortunately, when the canceled notification was the last one of the summary, only the notification itself will be canceled, but not the summary too.
What am i missing?
Starting in Android 7.0 (API level 24), you can display related notifications in a group (previously called "bundled" notifications). For example, if your app shows notifications for received emails, you should put all notifications into the same group so they can be collapsed together.
Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.
setAutoCancel(true)
to the summary Notification solved my problem of summary notification being left in the tray.
Had the same problem. I cancel notification programmatically when I tap notification action. If you swipe it out it works well. I do this workaround:
public static void cancelNotification(Context context, int notifyId, int summeryId) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
boolean cancelSummary = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N && summeryId != 0) {
StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications();
String groupKey = null;
for (StatusBarNotification statusBarNotification : statusBarNotifications) {
if (notifyId == statusBarNotification.getId()) {
groupKey = statusBarNotification.getGroupKey();
break;
}
}
int counter = 0;
for (StatusBarNotification statusBarNotification : statusBarNotifications) {
if (statusBarNotification.getGroupKey().equals(groupKey)) {
counter++;
}
}
if (counter == 2) {
cancelSummary = true;
}
}
if (cancelSummary) {
notificationManager.cancel(summeryId);
} else {
notificationManager.cancel(notifyId);
}
}
The summary notification is not automatically cancelled when all the grouped notifications are programmatically cancelled. From Correctly handling bundled Android notifications:
Obviously, you're going to need to keep track of new notifications. But this also means you have to keep track of notifications that have been dismissed. Otherwise, the summary might still contain information about notifications no longer contained within the bundle.
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