在android中处理多个通知

Usm*_*iaz 1 android android-notifications android-notification-bar

我有一个应用程序,当用户在应用程序上输入的特定事件发生时生成通知.例如,用户输入了7个事件,而他没有点击通知,然后通知栏将变满.我不希望这样.我想只显示一个通知图标,但只显示所有7个通知.就像whatsapp只显示1个通知图标的地方一样.

AAD*_*ing 5

我猜你正在看的是通知的"堆叠".

这里有几个重要的API.
1 setGroup().:这将通知设置为共享相同密钥的一组通知的一部分.
2 setGroupSummary().:将此通知设置为一组通知的组摘要.

此外,我们需要具有相同的通知构建器ID.以下是我在课堂上的宣言:

final static String GROUP_KEY_EMAILS = "group_key_emails";
int UNIQUE_NOTIFICATION_ID=422;
Run Code Online (Sandbox Code Playgroud)

和发布通知的示例代码:

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);
Run Code Online (Sandbox Code Playgroud)

这将显示UI如下:

在此输入图像描述