Search code examples
androidnotifications

how to not count ongoing notification as app's badge count?


i have onGoing Notification notified by foregroundService. i wanna not count this notification for app badge count, i can't find the way to achieve it... i create a notificationchannel like this,

 fun createNotificationChannel(context: Context) {
        val channel = NotificationChannel(CHANNEL_ID, _NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT)
        val nm = context.getSystemService(NotificationManager::class.java) as NotificationManager
        nm.createNotificationChannel(channel)
    }

and create notification,

fun createNotification(context: Context, remoteViews: RemoteViews) =
        NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).setPriority(NotificationCompat.PRIORITY_LOW).setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("notification").setContentText("notification").setOngoing(true).setStyle(NotificationCompat.DecoratedCustomViewStyle()).setCustomContentView(remoteViews)
.setCustomBigContentView(remoteViews).setContentIntent(getClickPendingIntent(context))

how to not count this notification in my app's badge? thanks you for your commnet in advance.


Solution

  • This is exactly what the setShowBadge(false) method on NotificationChannel is for:

    Sets whether notifications posted to this channel can appear as application icon badges in a Launcher. Only modifiable before the channel is submitted to createNotificationChannel.

    So your channel creation should be using that method:

    val channel = NotificationChannel(CHANNEL_ID, _NOTIFICATION_CHANNEL_ID,
        NotificationManager.IMPORTANCE_DEFAULT)
    
    // Add this line
    channel.setShowBadge(false)
    
    val nm = context.getSystemService(NotificationManager::class.java) as NotificationManager
    nm.createNotificationChannel(channel)