'Android badge count is wrong when notify id is different

I want to show total unread count on badge but setNumber(count) wrong when notify id is different.

This is my code to show badge count with notification channel.

val notification = NotificationCompat.Builder(context, "CHANNEL_ID_MESSAGE").apply {
                priority = chatNotificationData.channelImportance
                setGroup(NOTI_GROUP_ID)
                setContentTitle(roomName)
                setDefaults(NotificationCompat.DEFAULT_ALL)
                setCategory(NotificationCompat.CATEGORY_MESSAGE)
                setContentText(body)
                setTicker(body)
                setWhen(System.currentTimeMillis())
                setContentIntent(pendingIntent)
                setSmallIcon(smallIcon)
                setLargeIcon(ImageUtil.getCircularBitmap(profileImage))
                setNumber(unReadCount)
                setAutoCancel(true)
            }.build()
NotificationManagerCompat.from(context).apply { notify(roomSeq.toInt(), notification)}

setNumber unReadCount = 1, notify ID roomSeq = 100 Badge count is 1 OK!!

setNumber unReadCount = 2, notify ID roomSeq = 200 Badge count is 3 .. why added? I expected 2.

setNumber unReadCount = 3, notify ID roomSeq = 200 Badge count is 4 ..

setNumber unReadCount = 4, notify ID roomSeq = 100 Badge count is 8 Oops....I don't know what happen

How to solve this problem.



Solution 1:[1]

I have the same problem while developing in Xamarin Android.

The cause of this was that I increased the value of ".setNumber(value)".

In android documentation say:

By default, each notification increments a number displayed on the long- press menu (visible in figure 1), but you can override this number for your app. For example, this might be useful if you're using just one notification to represent multiple new messages but you want the count here to represent the number of total new messages.

Just not increment the setNumber(1) value and works fine.

val notification = NotificationCompat.Builder(context, "CHANNEL_ID_MESSAGE").apply {
            priority = chatNotificationData.channelImportance
            setGroup(NOTI_GROUP_ID)
            setContentTitle(roomName)
            setDefaults(NotificationCompat.DEFAULT_ALL)
            setCategory(NotificationCompat.CATEGORY_MESSAGE)
            setContentText(body)
            setTicker(body)
            setWhen(System.currentTimeMillis())
            setContentIntent(pendingIntent)
            setSmallIcon(smallIcon)
            setLargeIcon(ImageUtil.getCircularBitmap(profileImage))
            setNumber(1)
            setAutoCancel(true)
        }.build()

Android docs

Solution 2:[2]

.setNumber(4) function is incrementing the badge count by the number you pass there, in this case, increments by 4.

For example: If you already have a badge count of 4, when receiving another notification it will go to 8.

To solve this problem, first check how many active notification you have in the moment:

val notificationManager =
context.getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager

val activeNotificationCount = notificationManager.activeNotifications.size

Then, if no active notification, increment by unread number, otherwise increment by 1

if (activeNotificationCount > 0) { setNumber = unReadCount } else { setNumber = 1 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Denis.Coelho
Solution 2 Dayana Viana