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)