我正在创建一个应用程序,其中创建了两个通知通道。比如说,
通道1:显示一种类型的通知 通道2:显示另一种类型的通知 现在,当用户单击通道 2 的任何一个通知时,我想消除通道 1 上的所有通知。
到目前为止,我只找到了跟踪通道 1 的所有通知 ID 的解决方案,然后使用这些 id 取消通知。
有没有更好的方法,无需跟踪 ID?
提前致谢:)
在使用 Kotlin 的 Android 中,可以使用“NotificationManager”和要删除的通道的通道 ID 来删除来自特定通道的通知。虽然这只能在android oreo及以上版本中实现。
// 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()
}
}