我有一个问题找不到答案。我已经尝试过 AndroidDeveloper 教程,我已经在 stackoverflow 和 google 上进行了搜索,但要么我的搜索技能太棒了,要么没有答案,我认为可以回答我的问题。
当有多条消息时,我想将所有新消息的消息通知堆叠到一个通知中。我可以为每条消息显示通知,但无法创建堆栈/摘要通知。
我得到的最好的是:http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png
我想要的是这样的:http://developer.android.com/images/android-5.0/notifications/Summarise_Do.png http://developer.android.com/images/android-5.0/notifications/Summarise_Do.png
我正在使用 API 19,它不必向后兼容。在 Android Studio 中编写代码并在 Genymotion 中进行测试。
我尝试使用NotificationCompat Manager,结果收到多个通知:NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
我也尝试使用NotificationManager得到相同的结果,即多个通知:
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
我的通知如下所示:
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle( "New message from... ")
.setContentText("Message:...")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION))
.setGroup(mNoti)
.setGroupSummary(true)
.setAutoCancel(true)
.build();
我像这样触发通知:
notificationManager.notify(counter, notification);
每个通知的计数器都会递增,因此每个通知都有自己的 ID。
我还尝试过使用此表单来构建和触发通知,但没有成功:
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("message from...")
.setContentText("message...")
.setContentIntent(pIntent)
.setAutoCancel(true)
.setGroup(mNoti)
.setGroupSummary(true)
.setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION));
notificationManager.notify(counter, notificationBuilder.build());
setGroup 显然对我来说很重要。我不明白为什么它不起作用或者我应该如何让它起作用。
我的小组看起来像这样:
final static String mNoti = "mNoti";
唯一与我想要的非常接近的是对所有通知使用相同的 ID,以便新通知覆盖旧通知,并在构建通知时使用 setNumber(int) 。然而,这并没有真正给我想要的结果,因为我认为我无法获得用户未处理的通知数量。或者我可以吗?
谁能帮助我实现我想要的目标?