Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display multiple notification as a group?

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

Random random = new Random();
int m = random.nextInt(9999 - 1000);    
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.quemark1)
                    .setContentTitle("New Message")
                    .setContentText(message)
Intent intent = new Intent(this, ActivityMain.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ActivityMain.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)     context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(m, mBuilder.build());

here is the output of my code

like image 624
sam chaudhari Avatar asked May 31 '16 07:05

sam chaudhari


2 Answers

It will generate notification with multiple messages like Gmail

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.quemark1)
                    .setContentTitle("Title")
                    .setContentText("New Message received");
            NotificationCompat.InboxStyle inboxStyle =
                    new NotificationCompat.InboxStyle();

            inboxStyle.setBigContentTitle("doUdo");

           // Add your All messages here or use Loop to generate messages

                inboxStyle.addLine("Messgare 1");
                inboxStyle.addLine("Messgare 2");
                          .
                          .
                inboxStyle.addLine("Messgare n");


            mBuilder.setStyle(inboxStyle);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

            stackBuilder.addNextIntent(intent);

            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(pIntent);

            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
            mBuilder.setAutoCancel(true);
            mNotificationManager.notify(0, mBuilder.build());
like image 181
sam chaudhari Avatar answered Oct 13 '22 17:10

sam chaudhari


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<StatusBarNotification> 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);
        }
    }
}
like image 20
Dipali Shah Avatar answered Oct 13 '22 16:10

Dipali Shah