Android: Is there a way to disable notification bundling?

你离开我真会死。 提交于 2020-01-23 05:41:07

问题


I have an app where the user can receive multiple notifications for things they need to do. The user has a choice of making some of these notifications persistent (which I achieve by calling NotificationCompat.Builder.setOngoing). At least on my version of Android which is Nougat, when more than three notifications are posted by my app they get bundled together into one notification, which makes all of them dismissible to the user in one swipe. This makes the previously persistent notifications no longer persistent. Is there a way to programmatically instruct Android not to bundle my notifications?

This is the code I use to build the notification and display it:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(eventName + " " + notificationText)
        .setDefaults(Notification.DEFAULT_ALL)
        .setOnlyAlertOnce(true)
        .setContentIntent(eventListPendingIntent);

if (goalInfo.goal.persistNotification) {
    builder.setOngoing(true);
} else {
    builder.setAutoCancel(true);
}

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(eventType.value(), builder.build());

Thanks, Nir


回答1:


As per Google docs, notifications from the same app would be bundled automatically -

Note: If the same app sends four or more notifications and does not specify a grouping, the system automatically groups them together.

So in your case, what you can do is , instead of system applying the default grouping, you can separate your notifications into two groups using a separate group key for the persistent notifications and one for the non-persistent ones.

Check the Google docs. The method Builder.setGroup() on NotificationBuilderCompat takes a string parameter which is the key. There is a related method Builder.setGroupSummary which you should call on your Builder

Hope this is clear.



来源:https://stackoverflow.com/questions/42170889/android-is-there-a-way-to-disable-notification-bundling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!