Flutter中的FCM通知分组

3

我想实现类似这样的效果

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个回答

7

请查看awesome_notifications0.0.6+9

  1. 使用Group Key可以确定用于以简洁形式分组通知的公共键。

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

或者

你可以查看本地通知的通知分组

以下代码是可在Flutter中使用的示例版本,它是https://developer.android.com/training/notify-user/group.html提供的示例的“翻译”。

代码 -

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

2
你需要确保负载中的chat_indexpayload.data.chat_index)在同一组通知中是唯一的,这是你传递给FlutterLocalNotificationsPlugin.showid。如果它们都具有相同的chat_index(ID),那可能会有问题。当通知到达时,您也可以在本地随机生成一个数字,不必将其包含在负载中。
此外,您可能想升级到6.0.0,因为您落后了2个主要版本。可能已经修复了一些错误,但这些新版本可能都与空值安全有关。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接