【问题标题】:Flutter : How get Json data from firebase notification payloadFlutter:如何从 Firebase 通知负载中获取 Json 数据
【发布时间】:2020-04-27 01:58:30
【问题描述】:

当我收到来自 firebase 的通知时,我需要显示自定义对话框。但我不知道如何从通知负载中获取我的数据

我已经尝试过下面的代码,但它是机器人为我工作

  Future<void> _handleNotification(
    Map<dynamic, dynamic> message, bool dialog) async {
    final dynamic data = message['data'] ?? message;
    final dynamic notification = message['notification']['body'] ?? message;

    final String itemId = data['body'];

    debugPrint("DATA ->> $data");
    debugPrint("notification ->> $notification");
    message.forEach((k, v) => debugPrint("${k} - ${v}"));
  }

这是我在控制台中的有效负载

flutter: 通知消息 {gcm.message_id: 1578573493122232, id: 30, google.cae: 1, type: test, aps: {alert: {title: notification title, body: Message of Push notification}, sound: 1 }}

任何机构都可以帮助从通知负载中获取数据吗?

【问题讨论】:

    标签: flutter dart firebase-cloud-messaging


    【解决方案1】:

    我终于可以自己解决这个问题了

    如果您想从通知负载中获取数据,那么您需要将数据发送到消息的 "data" - 字段中。

    SAMPLE CODE 从消息的 "data" 字段读取数据。

    Future<void> _handleNotification(
        Map<dynamic, dynamic> message, bool dialog) async {
        var data = message['data'] ?? message;
        String notificationTitle = data['YOUR_KEY']; // here you need to replace YOUR_KEY with the actual key that you are sending in notification  **`"data"`** -field of the message. 
        String notificationMessage = data['YOUR_KEY'];// here you need to replace YOUR_KEY with the actual key that you are sending in notification  **`"data"`** -field of the message. 
    
        // now show the Dialog
        Utils().showMessageDialog(context, notificationTitle,notificationMessage);
      }
    

    更多信息请阅读Notification messages with additional data

    【讨论】:

    • 否则,您可以通过将 pub lib 添加为本机并更改接收器来在本机代码中执行此操作。
    • 如何使用消息“数据”字段中传递的数据显示在通知中?例如,由于某些限制,我在“数据”对象中发送标题和正文。现在我想在它被触发时在通知中显示它。
    猜你喜欢
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多