通知ID不同时,Android徽章计数错误

时间:2019-07-23 01:11:07

标签: android android-notifications

我想显示徽章上的未读总数,但是当通知ID不同时,setNumber(count)错误。

这是我的代码,用于通过通知渠道显示徽章计数。

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,通知ID roomSeq = 100徽章计数为1 OK!

setNumber unReadCount = 2,通知ID roomSeq = 200徽章计数为3 ..为什么要添加?我应该是2。

setNumber unReadCount = 3,通知ID roomSeq = 200徽章计数为4 ..

setNumber unReadCount = 4,通知ID roomSeq = 100徽章计数为8 糟糕。...我不知道会发生什么情况

如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

在Xamarin Android上进行开发时,我遇到相同的问题。

这是因为我增加了“ .setNumber(value)”的值。

在android文档中说:

默认情况下,每个通知都会增加长按菜单上显示的数字(在图1中可见),但是您可以为您的应用覆盖此数字。例如,如果您仅使用一个通知来代表多封新邮件,但希望此处的计数代表新邮件总数,则这可能很有用。

只需不增加setNumber( 1 )值就可以了。

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