简体   繁体   中英

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?

setAutoCancel(true)设置为摘要通知解决了我的摘要通知留在纸盒中的问题。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM