在android中处理多个通知
我有一个应用程序,当用户在应用程序中输入的特定事件发生时,它会生成通知。例如,用户输入了7个事件,但他没有单击通知,则通知栏将变满。我不想那样。我只想显示一个通知图标,但所有7个通知。就像在android中处理多个通知,android,android-notifications,android-notification-bar,Android,Android Notifications,Android Notification Bar,我有一个应用程序,当用户在应用程序中输入的特定事件发生时,它会生成通知。例如,用户输入了7个事件,但他没有单击通知,则通知栏将变满。我不想那样。我只想显示一个通知图标,但所有7个通知。就像whatsapp一样,只显示一个通知图标。我猜您看到的是通知的“堆叠”。 这里有几个重要的API。1setGroup():将通知设置为共享同一密钥的通知组的一部分。2。setGroupSummary():将此通知设置为通知组的组摘要。 此外,我们需要具有相同的通知生成器id。下面是我在类中的声明: final
whatsapp
一样,只显示一个通知图标。我猜您看到的是通知的“堆叠”。这里有几个重要的API。
1
setGroup()
:将通知设置为共享同一密钥的通知组的一部分。2。
setGroupSummary()
:将此通知设置为通知组的组摘要。此外,我们需要具有相同的通知生成器id。下面是我在类中的声明:
final static String GROUP_KEY_EMAILS = "group_key_emails";
int UNIQUE_NOTIFICATION_ID=422;
和要发布通知的示例代码:
Notification summaryNotification = new NotificationCompat.Builder(context)
.setContentTitle("2 new messages")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Notification 1 First line of info")
.addLine("otification 2 Second line of info")
.addLine("otification 3 Second line of info")
.setBigContentTitle("3 new messages")
.setSummaryText("yourid@gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(UNIQUE_NOTIFICATION_ID, summaryNotification);
这将显示UI,如下所示:
我猜您看到的是通知的“堆叠”。
这里有几个重要的API。
1
setGroup()
:将通知设置为共享同一密钥的通知组的一部分。2。
setGroupSummary()
:将此通知设置为通知组的组摘要。此外,我们需要具有相同的通知生成器id。下面是我在类中的声明:
final static String GROUP_KEY_EMAILS = "group_key_emails";
int UNIQUE_NOTIFICATION_ID=422;
和要发布通知的示例代码:
Notification summaryNotification = new NotificationCompat.Builder(context)
.setContentTitle("2 new messages")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Notification 1 First line of info")
.addLine("otification 2 Second line of info")
.addLine("otification 3 Second line of info")
.setBigContentTitle("3 new messages")
.setSummaryText("yourid@gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(UNIQUE_NOTIFICATION_ID, summaryNotification);
这将显示UI,如下所示:
为通知创建自定义布局,并为每个新通知使用相同的通知生成器id和更新视图!创建唯一的通知ID,并将其用于所有通知。如果以前的通知已经存在,那么只需更新以前通知的用户界面。@user2163887谢谢,这对我来说更有意义,但是举个例子会很好。请参考下面的内容。这可能会对您有所帮助。为通知创建自定义布局,并为每个新通知使用相同的通知生成器id和更新视图!创建唯一的通知ID,并将其用于所有通知。如果以前的通知已经存在,那么只需更新以前通知的用户界面。@user2163887谢谢,这对我来说更有意义,但是举个例子会很好。请参考下面的内容。也许对你有帮助。