25

I'm tring to create notification group, this is my code:

 // Build the notification, setting the group appropriately
 Notification notif = new NotificationCompat.Builder(getApplicationContext())
          .setContentTitle("New mail from " + 1)
          .setContentText("cv")
          .setSmallIcon(R.drawable.rh_logo)
          .setStyle(new NotificationCompat.InboxStyle()
            .addLine("Alex Faaborg   Check this out")
            .addLine("Jeff Chang   Launch Party")
            .setBigContentTitle("2 new messages")
            .setSummaryText("johndoe@gmail.com"))
          .setGroup(GROUP_KEY_EMAILS)
          .setGroupSummary(true)
          .build();

 // Issue the notification



 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 notificationManager.notify(++NOTIFICATION_ID, notif);

When I run the app, and send notification messages, they do not show in group. Can someone explain me what I need to change?

Matthias
  • 4,481
  • 12
  • 45
  • 84
Toda
  • 403
  • 1
  • 6
  • 13

3 Answers3

49

You have to create a group notification before creating your custom notification. Just like this:

NotificationCompat.Builder groupBuilder =
            new NotificationCompat.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setGroupSummary(true)
                    .setGroup("GROUP_1")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(content))
                    .setContentIntent(pendingIntent);

Do not forget setGroupSummary to true.

Then create your child notification which group value is same to groupBuilder's value。Here is "GROUP_1".

NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setGroup("GROUP_1")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(content))
                    .setContentIntent(pendingIntent)

Finally use NoticationManagerCompat to notify them.

    NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    manager.notify(GROUP_ID, groupBuilder.build());
    manager.notify(id, builder.build());
chenupt
  • 1,781
  • 1
  • 14
  • 13
  • 23
    But documentation says: "On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification. ". Still not working. – JacksOnF1re Jun 21 '18 at 16:43
  • 8
    The documentation is wrong: see https://blog.danlew.net/2017/02/07/correctly-handling-bundled-android-notifications/ – grebulon Oct 04 '18 at 16:00
  • 10
    Another important note is that if you have Notification.FLAG_AUTO_CANCEL set, grouping will not work. Debugging this was very frustrating. – RyanCheu Mar 08 '19 at 21:12
  • 2
    @RyanCheu grouping worked even when `setAutoCancel(true)` to both summary notification and normal notification – Ahmed na Oct 07 '19 at 05:19
  • I am doing everything same as above, but still cannot see the group notification. My current notification is getting replaced by the new one. – Karan Sharma Mar 30 '20 at 04:47
  • 1
    @KaranSharma you probably forgot to increment your notification id – ArcDexx Mar 31 '20 at 22:41
  • @Ahmedna That's might work on your phone, but please test the other phones too. In some phones, `setAutoCancel(true)` will not make the notifications showing at all. – HendraWD Mar 02 '21 at 06:24
0

Replace

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);

with

NotificationManagerCompat.from(mCtx).notify(++NOTIFICATION_ID,notif);

because you are using NotificationCompat

Borg
  • 55
  • 8
  • @Brog, I am doing this , but still same issue on my android N emulator. – Tasneem Feb 01 '17 at 09:47
  • Actually @chenupt answer is the correct one. What the questioner is doing is only create the notification which will handle the child notifications of this group. InboxStyle is convenient for android devices lower than N because they don't support bundled notifications. More information https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html – Borg Feb 01 '17 at 11:37
  • 3
    Seems like the documentation is wrong. You have to create a summary notification. See: https://blog.danlew.net/2017/02/07/correctly-handling-bundled-android-notifications/ – grebulon Oct 04 '18 at 15:59
0

There is a strange behavior of notification. If you add autocancel notification group doesn't work, if you add setGroup or setGroupSummary. It will not work. Removing all that fixed the problem for me. Basically, you don't need to mention any group it will auto group. TESTED on Redmi 10 pr0, Poco x3 NFC, huawei device.

   return new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_action_name)
            .setContentTitle(title)
            .setContentText(body)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
            .setPriority(NotificationCompat.PRIORITY_LOW);
Rohaitas Tanoli
  • 624
  • 4
  • 16