Removing/Dismissed all notification belong to particular channel in android

205 Views Asked by At

I am creating one application where I created two notification channel. Let say,

Channel 1 : to show one type of notification channel 2 : to show another type of notification Now I want to dismiss all notification on channel 1 when user click on any one of the notification of channel 2.

Till now I only found solution that to keep track of all notification ID of channel 1 and after that cancel notification with those id.

Is there is better way to do, without keeping track of IDs??

Thanks in advance :)

1

There are 1 best solutions below

0
Sourav Dey On

In Android using Kotlin, notifications from a particular channel can be removed using "NotificationManager" and the channel id of the channel you want to delete. Although this can only be achieved in android oreo and above.

// Takes context and channel id as parameters
fun removeChannel(context: Context, channelId: String) {
    val notificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    //In Android Oreo and above you can delete the channel using channel id
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Delete the notification channel
        notificationManager.deleteNotificationChannel(channelId)
    } else {
        //In Android versions less than Oreo, you need to cancel all notifications
        notificationManager.cancelAll()
    }
}