【问题标题】:Handling multiple notification in android在android中处理多个通知
【发布时间】:2015-02-02 11:21:26
【问题描述】:

我有一个应用程序,当用户在应用程序上输入的特定事件发生时会生成通知。例如,现在用户输入了 7 个事件并且他没有点击通知,那么通知栏将变满。我不想要那个。我只想显示一个通知图标,但要显示所有 7 个通知。就像 whatsapp 只显示 1 个通知图标。

【问题讨论】:

  • 为通知创建自定义布局,并为每个新通知创建相同的通知构建器 ID 和更新视图!
  • 创建一个唯一的 NOTIFICATION_ID 并将其用于所有通知。如果之前的通知已经存在,那么只需更新之前通知的 UI。
  • @user2163887 谢谢它对我来说更有意义,但一个例子会非常好。
  • 请参考以下tutorialspoint.com/android/android_notifications.htm。可能会对你有所帮助。

标签: android android-notifications android-notification-bar


【解决方案1】:

我猜你正在看的是通知的“堆叠”。

这里有几个重要的 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 如下:

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多