首页 > 解决方案 > 如何在 Flutter 中折叠通知?使用本地推送通知插件和 CloudFunctions

问题描述

我正在使用云功能通过设备令牌发送通知。

    const payload: admin.messaging.MessagingPayload = {
        notification: {
            title: `updatedBy: ${updatedBy}`,
            body: `nova messagem ${increment}`,
        },

    }
    return fcm.sendToDevice(userToken, payload, options).then((value) => {
        console.log(value.results);
    }).catch((error) => {
        console.log(error);
    })

但是,所有通知都被删除了,我非常想管理这些卡在系统托盘中的通知,无论如何我可以使用这些插件来调节具有如此“复杂性”的通知吗?(本地推送通知、FCM、cloudfunctions)不使用本机代码(kotlin、android)?因为我不知道android结构是如何工作的......

我可以使用 cancelAll (本地推送通知)

FlutterLocalNotificationsPlugin.cancelAll()

来自本地推送通知:https ://pub.dev/documentation/flutter_local_notifications/latest/

但是太不灵活了

我也可以使用特定的“ID”使用取消通知,但我不知道如何获取该通知“id”

FlutterLocalNotificationsPlugin.cancel(notificationId)

那么,如何解决这个问题呢?

这样,我可以明智地删除我想要的任何通知,但基本上,我只想折叠相同类型的通知,例如在 Whatsapp 上,当有人向您发送超过 1 条消息时,系统托盘中只有一个通知说“他”发送了不止一条消息(而不是一大堆通知)。

我刚刚看了https://github.com/chetdeva/collapsing-notifications/tree/a5e8867e24f53ff8caca761c7faa13242c607dba

此 github 代码适用于文章https://medium.com/fueled-engineering/collapsing-fcm-notification-like-a-pro-102a4946b350

如果在颤振项目中实施起来不太困难,那将是完美的,我还在stackoverflow中查看了这里的每一篇文章,看到没有人回答过这个问题

[编辑]:我不能使用的原因

FlutterLocalNotificationsPlugin.cancelAll()

这是因为我使用 fcm 方法来监听通知,我试图将该方法放在一些 fcm 监听器中但没有成功。

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on message $message');
      },
      onResume: (Map<String, dynamic> message) async {
        print('on resume $message');

      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on launch $message');
      },
    );

标签: androidfirebaseflutterpush-notificationfirebase-cloud-messaging

解决方案


推荐阅读