Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - how to group notifications

Tags:

flutter

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?

like image 309
achoo254 Avatar asked Oct 21 '25 16:10

achoo254


1 Answers

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

like image 179
KHAL Avatar answered Oct 23 '25 06:10

KHAL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!