Collapsing Notifications implements the best practices to handle and collapse FCM notifications.
AppPushNotification is responsible for resolving PushNotificationItem. It also decides when to show/hide a notification. Here’s the flow:
CollapsingNotificationManager is one of the dependencies injected in AppPushNotification which manages collapsing of notifications.
- Notifications will collapse based on
collapse_key
from RemoteMessage. Ergo, you should be sending a payload with acollapse_key
. - When the first notification (of say
type_a
) is triggered, the usual notification will be shown. - When second notification (of say type_a) is triggered, it is added to the queue and if the size of this list of
type_a
notification is greater than 1, flagshouldCollapse
is set totrue
. - Hence, the previous notification is cleared and the collapsing text will be shown based on the type. In this case, it will be “You have 2 notifications of Type-A”.
- Similar steps will be followed for
type_b
notifications and so on. - When user taps on or dismisses this notification from Notification Tray, this list of
type_a
notifications cleared.MainActivity
handles the tap whereasNotificationDismissTracker
takes care of dismissal.