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

问题描述 投票:0回答:1

我想显示徽章上的未读总数,但是当通知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 糟糕。...我不知道会发生什么

如何解决此问题。

android android-notifications
1个回答
0
投票

在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

© www.soinside.com 2019 - 2024. All rights reserved.