7

I create several notifications like this:

public class NotificationCreator {
    Context context;
    int c = 0;

    public NotificationCreator(final Context context) {
        this.context = context;
    }

    void create() {

        String text = "" + c  + " " + new Date().toGMTString();

        // Intent
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra(SecondActivity.KEY, text);
        Intent[] intents = new Intent[1];
        intents[0] = intent;
        PendingIntent pendingIntent = PendingIntent.getActivities(
                context,
                c,
                intents,
                PendingIntent.FLAG_CANCEL_CURRENT);

        // Build notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.notification);
        builder.setContentTitle("Test");
        builder.setContentText(text);
//        builder.setGroup("");
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();

        // Send notification
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(c, notification);

        c++;
    }
}

The outcome on Android 7 is:

enter image description here

The system has grouped all notifications.

When I set explicitly on the builder:

 builder.setGroup("myGroup");

the outcome is:

enter image description here

The notifications are not grouped, but all shown individually, despite all having the same group key.

  1. Is this the expected behaviour?

  2. In the first case (grouped), can I determine what happens when the user clicks on the grouped notification? The individual intents seem to be ignored and just the app opened.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • Have you tried adding `setGroupSummary(true)` to the builder when you add the group? Like here https://stackoverflow.com/a/41114135/2910520. Btw this is the correct behaviour, not grouped notifications from the same app stack tocketer, while the grouped ones can be expanded or shrinked, [here](https://stackoverflow.com/a/37163589/2910520) you can find a GIF – MatPag Jul 07 '17 at 15:40
  • I am having the same issue. – J Blaz Sep 05 '17 at 20:07
  • Same issue, also tries setGroupSummary(true) for every notification, still same result. – Oleksandr Albul Nov 15 '17 at 17:06

1 Answers1

4

Had the same problem and figured it out by using a "Summary notification". Basically, you create one regular notification with the desired group and the "Summary notification" with the same group and setGroupSummary(true).

More here https://blog.stylingandroid.com/nougat-bundled-notifications/

peter.o
  • 3,460
  • 7
  • 51
  • 77