上次包含通知被取消时,组摘要通知未取消

sne*_*eps 8 notifications android

我已经为组摘要创建了一个通知,其中可能包含许多通知。
这些通知添加了一些操作addAction()

我尝试在执行操作后取消通知:

NotitifactionCompat.from(context).cancel(notificationId);
Run Code Online (Sandbox Code Playgroud)

不幸的是,当取消的通知是摘要的最后一个时,仅通知本身将被取消,摘要也不会被取消。

我想念什么?

Vam*_*udi 6

setAutoCancel(true) 摘要通知解决了我的摘要通知留在托盘中的问题。

  • 如果您以编程方式取消通知,则不会取消摘要通知。 (3认同)

Mat*_*ski 5

有同样的问题。当我点击通知操作时,我以编程方式取消通知。如果你把它刷出来,效果很好。我做这个解决方法:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 1

当以编程方式取消所有分组通知时,不会自动取消摘要通知。来自正确处理捆绑的 Android 通知

显然,您需要跟踪新通知。但这也意味着您必须跟踪已被忽略的通知。否则,摘要可能仍包含有关捆绑包中不再包含的通知的信息。