首页 > 解决方案 > 我创建了一个通知频道,但在 Android 10 上收到“频道不存在”或“频道无效”的错误消息

问题描述

自 1 年前以来,我一直在提供应用程序。

我为前台服务做了 createNotificationChannelsetChannelIdstartForeground,我为两个服务使用了相同的频道 ID 和名称。

一直到二月份都很好用。

private var notificationBuilder: Notification.Builder? = null

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    var channel = notificationManager.getNotificationChannel(notificationChannelId)
    if (channel == null) {
        channel = NotificationChannel(notificationChannelId, notificationChannelName, NotificationManager.IMPORTANCE_MIN)
        notificationManager.createNotificationChannel(channel)
    }

    if (notificationBuilder == null || startForeground) {
        notificationBuilder = Notification.Builder(service, channel.id)
                .setSmallIcon(R.drawable.my_icon)
                .setColor(ResourcesCompat.getColor(service.resources, R.color.my_color, null))
                .setOnlyAlertOnce(true)
                .setContentText(notificationMessage)
                .setContentIntent(notiClickPendingIntent)
                .setChannelId(notificationChannelId)

        val notiBuilder = notificationBuilder ?: return
        service.startForeground(notificationID, notiBuilder.build())
    } else {
        // update notification
        val notiBuilder = notificationBuilder ?: return
        notiBuilder
                .setContentText(notificationMessage)
                .setChannelId(notificationChannelId)
        notificationManager.notify(notificationID, notiBuilder.build())
    }
}


最近,虽然我没有修改相关代码,但是从3月份开始就开始出现bug报告。

Bad notification for startForeground: java.lang.IllegalArgumentException: Channel does not exist

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification

这些错误在 Android 10 中非常常见,并且在 Android 9 中经常发生。

它在 Galaxy s10 系列和 Galaxy Note 10 系列中发生了很多。

有谁知道任何相关信息?


ps 如果您能给我一个更好的英文表达方式,我将不胜感激!

标签: android

解决方案


推荐阅读