Boby
Boby

Reputation: 1202

Grouping FCM Notification Flutter

i'm trying to achieve something like this

enter image description here

I'm using flutter_local_notifications: ^4.0.1+2 . Here is what i do so far

        BigTextStyleInformation bigTextStyleInformation =
            BigTextStyleInformation(
          payload.notification.body,
          htmlFormatBigText: true,
          contentTitle: payload.notification.title,
          htmlFormatContentTitle: true,
          summaryText: payload.data['group_name'].toString(),
          htmlFormatSummaryText: true,
        );

        AndroidNotificationDetails androidPlatformChannelSpecifics =
            AndroidNotificationDetails(
          payload.data['group_name'].toString(),
          payload.data['group_name'].toString(),
          payload.data['group_name'].toString(),
          styleInformation: bigTextStyleInformation,
          groupKey: payload.data['group_id'].toString(),
          setAsGroupSummary: true,
          importance: Importance.high,
        );
        //
        NotificationDetails platformChannelSpecifics =
            NotificationDetails(android: androidPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.show(
            int.parse(payload.data['chat_index']),
            payload.notification.title,
            payload.notification.body,
            platformChannelSpecifics);

And here is what i send to fcm server

{
  "registration_ids": [
    "dhFLSBuAT3yYC4XoeY7oKf:APA91bGbtYTxmHhua7kewUbR1-PCHlGXyS_WP1jP-vjoLkJ_JoB69fuR8XO8hfmv-_r8VekBIkuFqhhJaHSnlKIJVRaeCAE7Mu682ffZ4MASC2O_v0QR--pQOqY3Anm7Q8liEh4Sg6f9"
  ],
  "notification": {
    "body": "Example Message",
    "title": "GROUP TEST 1",
    "icon": "@drawable/ic_stat_notification_logo",
    "sound": "mySound",
    "priority": "high",
    "show_in_foreground": true,
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
  },
  "data": {
    "type": "chat",
    "openScreen": true,
    "useHomeScreen": true,
    "homeScreenBottomIndex": 1,
    "group_id": 1,
    "group_name": "GROUP 1",
    "chat_index": 1
  }
}

From my script above i get this result

enter image description here

Any solution for this ?

Upvotes: 3

Views: 2977

Answers (2)

Ben Butterworth
Ben Butterworth

Reputation: 29008

Are you making sure that the chat_index in your payload (payload.data.chat_index) are unique between notifications in the same group. You're using this as the id passed to FlutterLocalNotificationsPlugin.show. If they all have the same chat_index (ID), that might be a problem. You can also just randomly generate a number locally when the notification comes in, it doesn't need to be in the payload.

Also, you might want to upgrade to 6.0.0 since you are 2 major versions behind. There might have been bug fixes you could make use of, but these new releases are probably all related to null safety.

Upvotes: 2

anirudh
anirudh

Reputation: 1466

Check out awesome_notifications : 0.0.6+9

  1. Using Group Key you can determine the common key used to group notifications in a compact form.

  2. You can even use Group Sort to determines the notifications sort order inside the grouping

OR

You can check out Grouping-Notifications of local notifications.

The following code is a "translation to flutter" of the sample available at https://developer.android.com/training/notify-user/group.html

Code -

const String groupKey = 'com.android.example.WORK_EMAIL';
const String groupChannelId = 'grouped channel id';
const String groupChannelName = 'grouped channel name';
const String groupChannelDescription = 'grouped channel description';
// example based on https://developer.android.com/training/notify-user/group.html
const AndroidNotificationDetails firstNotificationAndroidSpecifics =
    AndroidNotificationDetails(
        groupChannelId, groupChannelName, groupChannelDescription,
        importance: Importance.max,
        priority: Priority.high,
        groupKey: groupKey);
const NotificationDetails firstNotificationPlatformSpecifics =
    NotificationDetails(android: firstNotificationAndroidSpecifics);
await flutterLocalNotificationsPlugin.show(1, 'Alex Faarborg',
    'You will not believe...', firstNotificationPlatformSpecifics);
const AndroidNotificationDetails secondNotificationAndroidSpecifics =
    AndroidNotificationDetails(
        groupChannelId, groupChannelName, groupChannelDescription,
        importance: Importance.max,
        priority: Priority.high,
        groupKey: groupKey);
const NotificationDetails secondNotificationPlatformSpecifics =
    NotificationDetails(android: secondNotificationAndroidSpecifics);
await flutterLocalNotificationsPlugin.show(
    2,
    'Jeff Chang',
    'Please join us to celebrate the...',
    secondNotificationPlatformSpecifics);

// Create the summary notification to support older devices that pre-date
/// Android 7.0 (API level 24).
///
/// Recommended to create this regardless as the behaviour may vary as
/// mentioned in https://developer.android.com/training/notify-user/group
const List<String> lines = <String>[
    'Alex Faarborg  Check this out',
    'Jeff Chang    Launch Party'
];
const InboxStyleInformation inboxStyleInformation = InboxStyleInformation(
    lines,
    contentTitle: '2 messages',
    summaryText: 'janedoe@example.com');
const AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
        groupChannelId, groupChannelName, groupChannelDescription,
        styleInformation: inboxStyleInformation,
        groupKey: groupKey,
        setAsGroupSummary: true);
const NotificationDetails platformChannelSpecifics =
    NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
    3, 'Attention', 'Two messages', platformChannelSpecifics);

Upvotes: 7

Related Questions