Reputation: 2951
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?
Upvotes: 1
Views: 1006
Reputation: 4116
Each NotificationChannel can only be assigned to one group by using channel.setGroup(groupID)
. In your code I can't see where you do this assignment.
Groups are only a way to organize the channels in the UI but they don't contain any logic (i.e. you can't send a notification to a group id, only to a channel id). Also you have to create the NotificationChannelGroup before assigning a channel to it.
If you want to move a channel to another group there is a method in the NotificationManager
called updateNotificationChannel
where you can change the group of your channel and update it.
There is more info in the docs https://developer.android.com/develop/ui/views/notifications/channels#CreateChannelGroup
Upvotes: 0