1

通知渠道和群组似乎很简单。

直觉上,我希望为每个通知类别创建一个频道,然后为每个用户创建组。最后,所有通知都应基于其独特的通道组配对进行捆绑。

但是,情况似乎并非如此。通知仅按组分隔,频道似乎没有效果。

目前我在应用程序生命周期中创建所有内容:

const val NOTIF_CHANNEL_GENERAL = "general"
const val NOTIF_CHANNEL_MESSAGES = "messages"

fun setupNotificationChannels(c: Context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
    val manager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val appName = c.string(R.string.frost_name)
    val msg = c.string(R.string.messages)
    manager.createNotificationChannel(NOTIF_CHANNEL_GENERAL, appName)
    manager.createNotificationChannel(NOTIF_CHANNEL_MESSAGES, "$appName: $msg")
    manager.deleteNotificationChannel(BuildConfig.APPLICATION_ID)
    val cookies = loadFbCookiesSync()
    val idMap = cookies.map { it.id.toString() to it.name }.toMap()
    manager.notificationChannelGroups
            .filter { !idMap.contains(it.id) }
            .forEach { manager.deleteNotificationChannelGroup(it.id) }
    val groups = idMap.map { (id, name) ->
        NotificationChannelGroup(id, name)
    }
    manager.createNotificationChannelGroups(groups)
    L.d { "Created notification channels: ${manager.notificationChannels.size} channels, ${manager.notificationChannelGroups.size} groups" }
}

@RequiresApi(Build.VERSION_CODES.O)
private fun NotificationManager.createNotificationChannel(id: String, name: String): NotificationChannel {
    val channel = NotificationChannel("${BuildConfig.APPLICATION_ID}_$id",
            name, NotificationManager.IMPORTANCE_DEFAULT)
    channel.enableLights(true)
    channel.lightColor = Prefs.accentColor
    channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
    createNotificationChannel(channel)
    return channel
}

从我的日志中,它确实显示创建了 2 个通道和 2 个组。对于任何通知,我都会为其分配一个唯一的标签 ID 对,然后指定一个频道和一个与我希望提交的频道相对应的组。然后我添加一个摘要通知以匹配相同的组通道对。但是,我最终只得到了 2 个通知分组,其中每个组都有来自两个渠道的通知。

我注意到的一个问题是群组频道列表为空,这很可能是问题所在。但是,我可以看到绑定的唯一方法是setGroup在通道内调用。每个频道只能是一个组的一部分,但频道组的描述提到:

 *     For example, if your application supports multiple accounts, and those accounts will
 *     have similar channels, you can create a group for each account with account specific
 *     labels instead of appending account information to each channel's label.

该文档使通道看起来像是多个(或每个)组的一部分,并且几乎每一篇实现它的文章都使它看起来也如此。

我们应该如何将频道与频道组相关联?我们应该为每个组创建一个新频道还是重复使用它们?如果我们应该重用它们,我们如何或何时设置通道组?

4

0 回答 0