-
- Notification channel groups
- Notification channels let us bundle together notification channels
- Beneficial where we find ourselves using the same notification channels in several places in our app.
- Example use case:
- our app supports both a personal account and a business account
- both account types have identical notification channels;
- A personal user account including 2 notification channels:
- Notifications of new comments on your posts.
- Notifications recommending posts by your contacts.
- A business user account including 2 notification channels:
- Notifications of new comments on your posts.
- Notifications recommending posts by your contacts.
- In the above use case it makes sense to use notification channel groups
- To create a notification channel group:
```java // The id of the group. String group = "my_group_01"; // The user-visible name of the group. CharSequence name = getString(R.string.group_name); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group, name)); ```
- Once you've created a new group you can call `setGroup()` to associate a new channel with the group.
- You can only modify the association between notification channel and group before you submit the channel to the notification manager.
- To create a batch of notification groups by using the `createNotificationChannelGroups()` method, we can pass in a List of NotificationChannelGroup instances for the groups we wish to create:
```java NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); List<NotificationChannelGroup> notificationChannelGroups = new ArrayList(); notificationChannelGroups.add(new NotificationChannelGroup("group_one", "Group One")); notificationChannelGroups.add(new NotificationChannelGroup("group_two", "Group Two")); notificationChannelGroups.add(new NotificationChannelGroup("group_three", "Group Three")); notificationManager.createNotificationChannelGroup(notificationChannelGroups); ``` |