java是否需要检查通知通道是否已创建?
我们是否需要在创建通知通道之前检查它是否尚未创建
private fun createChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// todo: add here check if channel is already created
val defaultChannel = NotificationChannel(MEDIA_UPLOAD_NOTIFICATION_CHANNEL_ID, MEDIA_UPLOAD_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
defaultChannel.description = MEDIA_UPLOAD_NOTIFICATION_CHANNEL_DESC
defaultChannel.enableVibration(true)
notificationManager.createNotificationChannel(defaultChannel)
}
}
# 1 楼答案
不,你真的不必检查。如果存在具有相同ID的频道,则Android不会创建另一个频道
根据文件
更多信息请访问https://developer.android.com/training/notify-user/channels#CreateChannel
# 2 楼答案
正如文件中所建议的那样
请查看文档以了解更多详细信息-https://developer.android.com/training/notify-user/channels#CreateChannel