是否需要检查是否已创建通知通道?
Is check that notification channel is already created needed?
我们是否需要在创建通知渠道之前检查它是否尚未创建?
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)
}
}
不,您真的不必检查它。如果存在具有相同 ID 的频道,则 Android 不会创建另一个频道。
根据文档
Creating an existing notification channel with its original values performs no operation, so it's safe to call this code when starting an app.
更多信息请见 https://developer.android.com/training/notify-user/channels#CreateChannel
按照文档中的建议。
If creating a NotificationChannel with the same original values there won't be any operation. So, it's safe to call the code.
请查看文档了解更多详情 - https://developer.android.com/training/notify-user/channels#CreateChannel
我们是否需要在创建通知渠道之前检查它是否尚未创建?
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)
}
}
不,您真的不必检查它。如果存在具有相同 ID 的频道,则 Android 不会创建另一个频道。
根据文档
Creating an existing notification channel with its original values performs no operation, so it's safe to call this code when starting an app.
更多信息请见 https://developer.android.com/training/notify-user/channels#CreateChannel
按照文档中的建议。
If creating a NotificationChannel with the same original values there won't be any operation. So, it's safe to call the code.
请查看文档了解更多详情 - https://developer.android.com/training/notify-user/channels#CreateChannel