最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Android notification badge count .setNumber is adding to itself - Stack Overflow

programmeradmin1浏览0评论

I have this code. Basically, when I get a notification, I want my badge number to be badgeCount (lets say 10). But, what happens is when I get another notification, it is adding to the old badgeCount. So, after two notifications, it would be 20.

I notice on iOS, when I get an apns with a badge attribute, it just sets the bade number to that. It does not add to the pervious notification value. So, its 10 every time.

Is it possible to do this on Android? I would basically want to clear any previous badge count value before setting a new one. If I get a PN with a badgeCount of 10, I want to see 10 on the badge, regardless of what it looked like before.

Is that possible?

var randomRequestCode = (0..1000000).shuffled().last()

val pendingIntent = PendingIntent.getActivity(this, randomRequestCode, intent, PendingIntent.FLAG_IMMUTABLE)

val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, "test")
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)

badgeCount?.let {
            notificationBuilder.setNumber(it)
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val channel = NotificationChannel("test", "testtest", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)

val notification = notificationBuilder.build()

notificationManager.notify(randomRequestCode, notification)
发布评论

评论列表(0)

  1. 暂无评论