我正在尝试使用setGroup()方法在Android应用程序中对通知进行分组。它正在工作,但是新通知将显示为单个通知,持续两三秒钟,并且仅在将这些通知添加到先前通知的堆栈中之后才显示。 Whatsapp似乎做同样的事情(新通知单独显示了几毫秒),但是我的问题是我的情况花了太多时间。
这是我的代码:
private boolean firstNotification = true;
public void createNotification(Context context, String name) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
if(firstNotification) {
firstNotification = false;
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context, context.getString(R.string.channel_id))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup(NOTIFICATIONS_GROUP)
.setGroupSummary(true)
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
int summaryNotificationId = 0;
notificationManager.notify(summaryNotificationId, summaryBuilder.build());
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, context.getString(R.string.channel_id))
.setContentTitle(name)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setGroup(NOTIFICATIONS_GROUP)
.setAutoCancel(true);
int notificationId = new Random().nextInt(1000000);
notificationManager.notify(notificationId, builder.build());
}