Reputation: 51
i am using Flutter firebase messaging to receive notifications from backend, how to group notifications like android https://developer.android.com/training/notify-user/group?
Upvotes: 3
Views: 5345
Reputation: 337
To group notifications in Flutter you can use flutter_local_notifications. this package has many features and one of them is ([Android] Group notifications). and to do so.
AndroidNotificationChannelGroup channelGroup = AndroidNotificationChannelGroup('com.my.app.alert1', 'mychannel1');
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannelGroup(channelGroup);
void groupNotifications() async {
List<ActiveNotification>? activeNotifications = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.getActiveNotifications();
if (activeNotifications != null && activeNotifications.length > 0) {
List<String> lines = activeNotifications.map((e) => e.title.toString()).toList();
InboxStyleInformation inboxStyleInformation = InboxStyleInformation(
lines,
contentTitle: "${activeNotifications.length - 1} Updates",
summaryText: "${activeNotifications.length - 1} Updates",
);
AndroidNotificationDetails groupNotificationDetails = AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
styleInformation: inboxStyleInformation,
setAsGroupSummary: true,
groupKey: channel.groupId,
// onlyAlertOnce: true,
);
NotificationDetails groupNotificationDetailsPlatformSpefics = NotificationDetails(android: groupNotificationDetails);
await flutterLocalNotificationsPlugin.show(0, '', '', groupNotificationDetailsPlatformSpefics);
}
}
Note: you should call groupNotifications() every time notification recived
Upvotes: 7