Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group summary notification not canceled when last containing notification is canceled

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?

like image 964
sneps Avatar asked Aug 09 '17 12:08

sneps


People also ask

What is group notification in Android?

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.

What is the purpose of the notificationmanager class?

Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.


3 Answers

setAutoCancel(true) to the summary Notification solved my problem of summary notification being left in the tray.

like image 155
Vamsi Tallapudi Avatar answered Oct 16 '22 08:10

Vamsi Tallapudi


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);
    }
}
like image 43
Mateusz Kaflowski Avatar answered Oct 16 '22 06:10

Mateusz Kaflowski


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.

like image 20
Rafael Bardini Avatar answered Oct 16 '22 06:10

Rafael Bardini