4

I have been struggling a lot with notifications lately.

Notifications can be easily dismissed after being clicked by using

notificationBuilder.setAutoCancel(true);

Summary notifications are required for grouping those individual notifications. Whenever child notifications are present, the summary notification is not displayed as is (titles, texts and actions are ignored, for example). When all have been individually dismissed, then the summary notification is fully displayed, with its texts and actions. This feels weird: why would we need a summary after having seen all notifications individually? Who knows... Anyway, we can get rid of this unexpected behaviour using

summaryNotificationBuilder.setAutoCancel(true);

Now, when the their action has been clicked, notifications do not automatically dismiss even though notificationBuilder.setAutoCancel(true) has been called. As explained in How to dismiss notification after action has been clicked, we need to keep track of their unique id we have provided when displaying the notification

notificationManager.notify(uniqueNotificationId, notificationBuilder.build());

From the receiving activity or broadcast receiver, we would call

notificationManager.cancel(uniqueNotificationId);

Let's be honest: this is horrible.. but it seems to be the way to go. I haven't been able to find any good alternative. Now, dismissing notifications like previously stated seems to interfere with summaryNotificationBuilder.setAutoCancel(true). When I individually dismiss all child notifications by clicking them or swiping them away, the summary notification is automatically cancelled. But when I trigger an action from any of those (thus triggering the mechanism of creating an Intent passing an extra uniqueNotificationId, and then calling NotificationManagerCompat.from(context).cancel(uniqueNotificationId) from the receiving activity or broadcast receiver, then the summary notification is not automatically canceled, even though summaryNotificationBuilder.setAutoCancel(true) was used.

Any idea on how to fix this?

I didn't find a way to programmatically figure out how many child notifications are still visible in order to also manually dismiss the summary notification, for example. Edit: Actually, now I'm trying to unconditionally dismiss the summary notification... without success.

Pat Lee
  • 1,567
  • 1
  • 9
  • 13

0 Answers0