Delete notification channel without id

avatar
9    2 months ago

My app has a bug where it sends different push notification and up until now every notification got its own channel ID, so the user has got for every new notification a new channel which he can then enable/disable by toggle. This bug is now fixed but I want to delete the old unnecassary channels programmatically and I found the way do it like this:

// The id of the channel.
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val id: String = "my_channel_01"
notificationManager.deleteNotificationChannel(id)

But since every channel got created by a random generated ID and this ID has been discarded I can't delete the channel by his ID.

Is there a way to list all channel IDs a user got or any other way how I can delete old channels without knowing the ID

Answers { 1 }
avatar
2 months ago

have you tried obtain full list of all already created channels by calling getNotificationChannels()?

iterate through this list and call deleteNotificationChannel(id) for unnecessary channels (NotificationChannel has getId() method)