How to display multiple notification as a group?

后端 未结 4 1982
面向向阳花
面向向阳花 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

    When creating notifications for a handheld device, you should always aggregate similar notifications into a single summary notification.

    Check this it shows how to build stack notification.

    private void sendStackNotificationIfNeeded(RemoteNotification remoteNotification) {
        // only run this code if the device is running 23 or better
        if (Build.VERSION.SDK_INT >= 23) {
            ArrayList groupedNotifications = new ArrayList<>();
    
            // step through all the active StatusBarNotifications and
            for (StatusBarNotification sbn : getNotificationManagerService().getActiveNotifications()) {
                // add any previously sent notifications with a group that matches our RemoteNotification
                // and exclude any previously sent stack notifications
                if (remoteNotification.getUserNotificationGroup() != null &&
                        remoteNotification.getUserNotificationGroup().equals(sbn.getNotification().getGroup()) &&
                        sbn.getId() != RemoteNotification.TYPE_STACK) {
                    groupedNotifications.add(sbn);
                }
            }
    
            // since we assume the most recent notification was delivered just prior to calling this method,
            // we check that previous notifications in the group include at least 2 notifications
            if (groupedNotifications.size() > 1) {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    
                // use convenience methods on our RemoteNotification wrapper to create a title
                builder.setContentTitle(String.format("%s: %s", remoteNotification.getAppName(), remoteNotification.getErrorName()))
                        .setContentText(String.format("%d new activities", groupedNotifications.size()));
    
                // for every previously sent notification that met our above requirements,
                // add a new line containing its title to the inbox style notification extender
                NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle();
                {
                    for (StatusBarNotification activeSbn : groupedNotifications) {
                        String stackNotificationLine = (String)activeSbn.getNotification().extras.get(NotificationCompat.EXTRA_TITLE);
                        if (stackNotificationLine != null) {
                            inbox.addLine(stackNotificationLine);
                        }
                    }
    
                    // the summary text will appear at the bottom of the expanded stack notification
                    // we just display the same thing from above (don't forget to use string
                    // resource formats!)
                    inbox.setSummaryText(String.format("%d new activities", groupedNotifications.size()));
                }
                builder.setStyle(inbox);
    
                // make sure that our group is set the same as our most recent RemoteNotification
                // and choose to make it the group summary.
                // when this option is set to true, all previously sent/active notifications
                // in the same group will be hidden in favor of the notifcation we are creating
                builder.setGroup(remoteNotification.getUserNotificationGroup())
                    .setGroupSummary(true);
    
                // if the user taps the notification, it should disappear after firing its content intent
                // and we set the priority to high to avoid Doze from delaying our notifications
                builder.setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH);
    
                // create a unique PendingIntent using an integer request code.
                final int requestCode = (int)System.currentTimeMillis() / 1000;
                builder.setContentIntent(PendingIntent.getActivity(this, requestCode, DetailActivity.createIntent(this), PendingIntent.FLAG_ONE_SHOT));
    
                Notification stackNotification = builder.build();
                stackNotification.defaults = Notification.DEFAULT_ALL;
    
                // finally, deliver the notification using the group identifier as the Tag
                // and the TYPE_STACK which will cause any previously sent stack notifications
                // for this group to be updated with the contents of this built summary notification
                getNotificationManagerService().notify(remoteNotification.getUserNotificationGroup(), RemoteNotification.TYPE_STACK, stackNotification);
            }
        }
    }
    

提交回复
热议问题