achoo254
achoo254

Reputation: 51

Flutter - how to group notifications

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

Answers (1)

KHAL
KHAL

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.

  1. First, you should setup the package configurations.
  2. Create GroupChannel in the same place you set the package configurations.
AndroidNotificationChannelGroup channelGroup = AndroidNotificationChannelGroup('com.my.app.alert1', 'mychannel1');
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannelGroup(channelGroup);
  1. Create a method to check if there are more than one active notification.
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

Related Questions