Behaviour of grouped notification on Android 7

Viewed 1883

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.

1 Answers
Related