【问题标题】:Is check that notification channel is already created needed?是否需要检查通知通道是否已创建?
【发布时间】:2020-01-06 10:28:14
【问题描述】:

我们是否需要在创建通知通道之前检查它尚未创建?

 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)
    }
}

【问题讨论】:

    标签: java android kotlin push-notification android-notifications


    【解决方案1】:

    不,您实际上不必检查。如果存在具有相同 ID 的频道,则 Android 不会创建另一个频道。

    根据文档

    使用其原始值创建现有通知通道不会执行任何操作,因此在启动应用程序时调用此代码是安全的。

    更多信息https://developer.android.com/training/notify-user/channels#CreateChannel

    【讨论】:

      【解决方案2】:

      按照文档中的建议。

      如果创建具有相同原始值的 NotificationChannel,则不会进行任何操作。所以,调用代码是安全的。

      请查看文档了解更多详情 - https://developer.android.com/training/notify-user/channels#CreateChannel

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-27
        • 2021-10-26
        • 2012-01-22
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多