我正在实现通知并希望将它们分组以显示在通知栏中。
目前我正在实现
Create a Group of Notifications 中的示例在 Android 的官方开发者文档中。
我实现了这个方法:
private void makeNotification()
{
createNotificationChannel();
int SUMMARY_ID = 0;
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
String CHANNEL_ID = "MY_CHANNEL_ID";
Notification newMessageNotification1 =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_map_position_icon)
.setContentTitle("First summary")
.setContentText("You will not believe...")
.setGroup(GROUP_KEY_WORK_EMAIL)
.build();
Notification newMessageNotification2 =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_map_nav_position_icon_grey)
.setContentTitle("Second summary")
.setContentText("Please join us to celebrate the...")
.setGroup(GROUP_KEY_WORK_EMAIL)
.build();
Notification summaryNotification =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("Total summary")
.setContentText("Two new messages")
.setSmallIcon(R.drawable.ic_wdw_dont_drive)
.setGroup(GROUP_KEY_WORK_EMAIL)
.setGroupSummary(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, newMessageNotification1);
notificationManager.notify(2, newMessageNotification2);
notificationManager.notify(SUMMARY_ID, summaryNotification);
}
方法
createNotificationChannel()
只是创建 channel ,为了更好的可读性,我把它留在这里。现在文档说:
On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification.
所以最后
notify()
调用应该是可选的,并创建 summaryNotification
, 也。但是这个例子只在我通知摘要通知时对我有效。当我不这样做时,通知不会分组。这里出了什么问题?
最佳答案
On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification.
“摘要 [文本]”,但不是“组摘要通知”。这只是意味着您在摘要通知样式中设置的任何文本都将被 Android >= N 的分组通知中的组合文本替换。
这不会影响通知不分组而没有摘要的事实。
是的,这对我也有很大的误导性,必须努力学习。尝试在 Kitkat 上建立组并将其与 Nougat 组进行比较。
关于android - 通知组在没有摘要的情况下无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52497789/