分组 FCM 通知 Flutter

Grouping FCM Notification Flutter

我正在努力实现这样的目标

我正在使用 flutter_local_notifications: ^4.0.1+2 。这是我目前所做的

        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);

这是我发送给 fcm 服务器的内容

{
  "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
  }
}

从我上面的脚本我得到了这个结果

有什么解决办法吗?

查看 awesome_notifications0.0.6+9

  1. 使用 Group Key 您可以确定用于以紧凑形式对通知进行分组的公用键。

  2. 您甚至可以使用Group Sort来确定分组内的通知排序顺序

您可以查看 Grouping-Notifications 个本地通知。

以下代码是 https://developer.android.com/training/notify-user/group.html

中可用示例的“flutter 翻译”

代码-

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);

您是否确保您的负载 (payload.data.chat_index) 中的 chat_index 在同一组中的通知之间是 唯一的。您将其用作传递给 FlutterLocalNotificationsPlugin.showid。如果它们都具有相同的 chat_index (ID),则可能会出现问题。您也可以在收到通知时在本地随机生成一个数字,它不需要在有效负载中。

此外,您可能想要升级到 6.0.0,因为您落后了 2 个主要版本。 可能 修复了您可以利用的错误,但这些新版本可能都与 null 安全相关。