Petrakeas
Petrakeas

Reputation: 1690

Set badge number to 0 in an Android notification

I have created a notification channel in android with setShowBadge(true). In each notification I post there, I use setNumber() and set the number to the unread message count for that conversation. This affects the number shown on the Android launcher in the badge of my app icon.

However, there are cases where the unread number should be zero: e.g. the user using the "inline reply" or "mark as read" action of the notification. How can I set the counter to 0? There are apps such Telegram and Facebook messenger that have implemented this behavior.

Using setNumber(0), has the same behavior as not setting the number, which results to counting each notification as "1".

PS. I am using MessagingStyle and place multiple unread messages in a single notification.

Upvotes: 0

Views: 16

Answers (1)

Petrakeas
Petrakeas

Reputation: 1690

It seems that Telegram uses undocumented broadcasts to set the badge number as shown in the NotificationBadge class.

The code sees heavily influenced by the library ShortcutBadger.

Facebook messenger is also doing something similar.

So, I guess that there is no formal way of achieving what I asked.

A workaround I thought of is to create a secondary "silent" notification channel with setShowBadge(false). When a notification is updated after using one of the aforementioned actions, the "silent" channel can be used instead.

Upvotes: 0

Related Questions