Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behaviour of grouped notification on Android 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.

like image 735
fweigl Avatar asked Jul 07 '17 15:07

fweigl


People also ask

Can you group notifications on Android?

Starting in Android 7.0 (API level 24), you can choose to display related notifications in a group (previously called "bundled" notifications). For example, if your app shows notifications for received emails, you should put all notifications into the same group so they can be collapsed together.

How do I show group notifications on Android?

The user can tap the group summary notification to open your app. On Android 7.0 and higher, the system shows your group summary notification as a nested group of notifications, labeled with snippets of text from each grouped notification; it does not display the text you set on the group summary notification.

What are Android notification categories?

Notification categories are an Android Oreo (8.0+) feature which gives users finer control over notifications. When the user receives a notification with a category they can long press on it or go into the Notification Settings to change the category's settings.

What is notification grouping?

Introduced in iOS 12, Notification Grouping does basically what the name implies. Rather than every single notification showing up on the lock screen and Notification Center separately, they can be grouped into bundles.


1 Answers

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/

like image 124
peter.o Avatar answered Sep 30 '22 22:09

peter.o