1

Notification channels and groups seem to be straightforward.

Intuitively, I expected to make a channel for each notification category, and then create groups for each user. In the end, all notifications should be bundled based on their unique channel-group pairing.

However, this doesn't seem to be the case; notifications are only separated by groups, and the channels seem to have no effect.

Currently I create everything during the Application lifecycle:

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
}

From my logs, it does show that 2 channels and 2 groups are created. For any notification, I will assign it a unique tag id pair, then specify a channel and a group corresponding to the one I wish to submit to. I then add a summary notification to match the same group-channel pair. However, I only end up with 2 notification groupings, where each group has notifications from both channels.

One issue I've noticed is that the groups channel list is empty, which is very likely the issue. However, the only way I can see the binding is by calling setGroup within the channel. Each channel can only be a part of one group, but the description of channel groups mentions:

 *     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.

The documentation makes it seem like the channels can be a part of multiple (or every) group, and just about every article implementing it makes it seem like so as well.

How are we supposed to associate the channels to the channel groups? Are we supposed to make a new channel for every group or reuse them? If we are supposed to reuse them, how or when do we set the channel groups?

Allan W
  • 2,293
  • 2
  • 16
  • 36

0 Answers0