How to display multiple notification as a group?

后端 未结 4 1984
面向向阳花
面向向阳花 2021-02-20 11:01

here is my code for notification. it generate a new notification each time

Random random = new Random();
int m = random.nextInt(9999 - 1000);    
Notific         


        
4条回答
  •  爱一瞬间的悲伤
    2021-02-20 11:40

    From https://developer.android.com/training/notify-user/group.

        String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
    
    Notification newMessageNotification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
            .setSmallIcon(R.drawable.new_mail)
            .setContentTitle(emailObject.getSenderName())
            .setContentText(emailObject.getSubject())
            .setLargeIcon(emailObject.getSenderAvatar())
            .setGroup(GROUP_KEY_WORK_EMAIL)
            .build();
    By default, notifications are sorted according to when they were posted, but you can change order by calling setSortKey().
    
    If alerts for a notification's group should be handled by a different notification, call setGroupAlertBehavior(). For example, if you want only the summary of your group to make noise, all children in the group should have the group alert behavior GROUP_ALERT_SUMMARY. The other options are GROUP_ALERT_ALL and GROUP_ALERT_CHILDREN.
    
    Set a group summary
    On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification. The user can expand this notification to see each separate notification, as shown in figure 1. To support older versions, which cannot show a nested group of notifications, you must create an extra notification that acts as the summary. This appears as the only notification and the system hides all the others. So this summary should include a snippet from all the other notifications, which the user can tap to open your app.
    
    Note: The behavior of the group summary may vary on some device types such as wearables. To ensure the best experience on all devices and versions, always include a group summary when you create a group.
    To add a group summary, proceed as follows:
    
    Create a new notification with a description of the group—often best done with the inbox-style notification.
    Add the summary notification to the group by calling setGroup().
    Specify that it should be used as the group summary by calling setGroupSummary(true).
    For example:
    
    //use constant ID for notification used as group summary
    int SUMMARY_ID = 0;
    String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
    
    Notification newMessageNotification1 =
        new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notify_email_status)
            .setContentTitle(emailObject1.getSummary())
            .setContentText("You will not believe...")
            .setGroup(GROUP_KEY_WORK_EMAIL)
            .build();
    
    Notification newMessageNotification2 =
        new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notify_email_status)
            .setContentTitle(emailObject2.getSummary())
            .setContentText("Please join us to celebrate the...")
            .setGroup(GROUP_KEY_WORK_EMAIL)
            .build();
    
    Notification summaryNotification =
        new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
            .setContentTitle(emailObject.getSummary())
            //set content text to support devices running API level < 24
            .setContentText("Two new messages")
            .setSmallIcon(R.drawable.ic_notify_summary_status)
            //build summary info into InboxStyle template
            .setStyle(new NotificationCompat.InboxStyle()
                    .addLine("Alex Faarborg  Check this out")
                    .addLine("Jeff Chang    Launch Party")
                    .setBigContentTitle("2 new messages")
                    .setSummaryText("janedoe@example.com"))
            //specify which group this notification belongs to
            .setGroup(GROUP_KEY_WORK_EMAIL)
            //set this notification as the summary for the group
            .setGroupSummary(true)
            .build();
    
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(emailNotificationId1, newMessageNotification1);
    notificationManager.notify(emailNotificationId2, newMessageNotification2);
    notificationManager.notify(SUMMARY_ID, summaryNotification);
    

    The summary notification ID should stay the same so that it is only posted once, and so you can update it later if the summary information changes (subsequent additions to the group should result in updating the existing summary).

提交回复
热议问题