分组 FCM 通知 Flutter

时间:2021-06-23 08:59:30

标签: flutter firebase-cloud-messaging

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

enter image description here

我正在使用 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
  }
}

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

enter image description here

有什么解决办法吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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