android - 在android中处理多个通知

标签 android android-notifications android-notification-bar

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

最佳答案

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

这里有几个重要的 API。
1. setGroup():这会将通知设置为一组共享相同 key 的通知的一部分。
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:

enter image description here

关于android - 在android中处理多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28276606/

相关文章:

android通知栏显示/隐藏切换

android - 通知标志的值为 -3

android - Activity 在强制关闭时重新启动

android - Jetpack 撰写 : How to change theme from light to dark mode programmatically onClick

android - 如果应用程序位于前台,则通知 ParentStack 重新启动父级

android - 如何停止在 Android 上以编程方式播放通知声音

当应用程序在后台时,Android 第二次推送通知有效负载数据未接收到额外内容

android - 仅为 TextView 复合可绘制对象实现 onClick

java - com.google.ads.AdView 实例化失败 - java.lang.ClassNotFoundException : org. json.JSONException

android - 禁止下拉通知面板