flutter_local_notification分组通知不起作用

时间:2019-05-09 07:28:57

标签: android dart flutter flutter-dependencies

我是flutter的新手,并且对来自Firebase Push Notification的通知分组感到厌倦。我正在使用flutter_local_notification插件在通知栏中显示通知。

每个通知的标签都不同。因此,我已使用此插件的分组功能对通知进行分组。

但是它似乎仍然无法正常工作,因为它在通知栏中收到多个通知。任何帮助,将不胜感激。正如他们的文档所述,我在24级以上的api级别上尝试过此操作。

我尝试的示例代码如下。

String groupKey = 'com.android.example.WORK_EMAIL';

String groupChannelId = 'grouped channel id';

String groupChannelName = 'grouped channel name';

String groupChannelDescription = 'grouped channel description';

AndroidNotificationDetails firstNotificationAndroidSpecifics =
    new AndroidNotificationDetails(
        groupChannelId, groupChannelName, groupChannelDescription,
        importance: Importance.Max,
        priority: Priority.High,
        groupKey: groupKey);

NotificationDetails firstNotificationPlatformSpecifics =
    new NotificationDetails(firstNotificationAndroidSpecifics, null);

await flutterLocalNotificationsPlugin.show(1, 'Alex Faarborg',
    'You will not believe...', firstNotificationPlatformSpecifics);

AndroidNotificationDetails secondNotificationAndroidSpecifics =
    new AndroidNotificationDetails(
        groupChannelId, groupChannelName, groupChannelDescription,
        importance: Importance.Max,
        priority: Priority.High,
        groupKey: groupKey);

NotificationDetails secondNotificationPlatformSpecifics =
    new NotificationDetails(secondNotificationAndroidSpecifics, null);

await flutterLocalNotificationsPlugin.show(
    2,
    'Jeff Chang',
    'Please join us to celebrate the...',
    secondNotificationPlatformSpecifics);

2 个答案:

答案 0 :(得分:0)

添加

setAsGroupSummary: true

在您的 AndroidNotificationDetails 中

答案 1 :(得分:0)

经过多次修改,下面的解决方案对我有用。您只需要 setAsGroupSummary: true 到第一个通知。我对 AndroidNotificationDetails 的实现如下(我使用 BigTextStyleInformation 顺便说一句):

AndroidNotificationDetails _androidDetails(_NotificationType type, bool isGroupContainer) =>
      AndroidNotificationDetails(
          _NotificationPreference.GROUP_CHANNEL_ID,
          _NotificationPreference.GROUP_CHANNEL_NAME,
          _NotificationPreference.GROUP_CHANNEL_DESCRIPTION,
          groupKey: _NotificationPreference.GROUP_CHANNEL,
          icon: _NotificationPreference.APP_ICON,
          priority: Priority.high,
          importance: Importance.max,
          setAsGroupSummary: isGroupContainer,
          styleInformation: BigTextStyleInformation(type.getDetails.body,
              contentTitle: type.getDetails.title));

NotificationDetails _flutterNotificationDetails(_NotificationType type, bool isGroupContainer) =>
      NotificationDetails(android: _androidDetails(type, isGroupContainer), iOS: _iosDetails);

所以要在一个组中显示多个通知:

  @override
  Future<void> showNotification() async {
    await plugin.show(0, '', '',
        _flutterNotificationDetails(_NotificationType.Container,true), payload: '1');
    await plugin.show(_NotificationType.Type1.getDetails.id, _NotificationType.Type1.getDetails.title, _NotificationType.Type1.getDetails.body,
        _flutterNotificationDetails(_NotificationType.Type1,false));
    await plugin.show(_NotificationType.Type2.getDetails.id, _NotificationType.Type2.getDetails.title, _NotificationType.Type2.getDetails.body,
        _flutterNotificationDetails(_NotificationType.Type2,false));
  }

这将显示一个包含 2 个孩子的群组通知。