2

I want to group notifications into a summary.

I achieve this by having a single ID for all notifications. This way android will not create new notifications but update the existing one (reduced code):

Notification summaryNotification = new NotificationCompat.Builder(this)
    .setGroupSummary(true)
    .setDefaults(Notification.DEFAULT_ALL)
    .setStyle(new NotificationCompat.InboxStyle()
            .addLine(msg)
            .setBigContentTitle("My App")
            .setSummaryText("FooBar"))
    .build();

mNotificationManager.notify(uuid, summaryNotification);

UUID is always the same so that the notification should be updated. However when a new notification arrives, setStyle seems to be overwritten.

This cause the old addLine(msg) to disappear. However I want the new message to be added without having some kind of notification manager server side.

Any ideas or suggestions?

Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

1 Answers1

5

I think you are misinterpreting the notification builder.

The NotificationCompat.Builder builds the complete notification, with all the content. Reusing the same id simply tells the notification manager to replace an existing notification with the same id with the new one: (Source)

[...] update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously. If the previous notification is still visible, the system updates it from the contents of the Notification object.

Thus addLine is not an operation that is performed on an existing notification, but on the new builder you created (which is empty at that time).

If you want to add a line to an existing notification with the inbox style, you will need to either

  • keep the original builder object, add lines as needed and resend the notification with the same id
  • create a new builder and add the old lines first, then the new one. You will need to store or retrieve the old lines from somewhere (depending on your application).
Patrick
  • 4,720
  • 4
  • 41
  • 71
  • I create notifications from a gcm service, just like in the android gcm tutorial. Is it safe to keep an instance of notification builder in this service rather creating a new? – DarkLeafyGreen Aug 05 '14 at 11:57
  • 3
    If you use an IntentService as described in the tutorial, I would advise against it: "IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate." Thus your instance might go away - and with it your builder. You might want to think about how to persist the data you previously passed to the builder. Or - if you control the server - just send a reasonable subset of the old data along with the notification. – Patrick Aug 05 '14 at 12:08