当前位置: 首页 > 知识库问答 >
问题:

Flutter:如何从Firebase通知负载中获取Json数据

琴俊人
2023-03-14

当我收到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}"));
  }

这是我在控制台的有效载荷

flatter:notification message{gcm.message_id:1578573749312232,id:30,google.c.a.e:1,type:test,aps:{alert:{title:notification title,body:message of Push notification},sound:1}

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

共有1个答案

邰勇军
2023-03-14

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

如果您想从通知负载中获取数据,则需要在消息的“data”-字段中发送数据。

示例代码从消息的“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);
  }

有关更多信息,请阅读带有附加数据的通知消息

 类似资料:
  • 我在实时数据库中有以下数据: 实时数据库 我想获得 当我用这个的时候 错误: E/flutter(27654):[ERROR: flutter/lib/ui/ui_dart_state.cc(166)]未处理的异常:类型_InternalLinkedHashMap 我的范例: 我的问卷模型:

  • 我正在尝试打开一个特定的活动,当应用程序在后台时,用户点击通知。从文档中,我发现必须在有效负载中添加click_action,并在应用程序中添加一个意图过滤器来处理它。但是,如何通过Firebase控制台在Firebase通知中添加click_action?我也愿意做其他的工作。提前道谢。

  • 我想获取日期字段,但下面的代码没有这样做。Toast显示null(字符串日期的值)。 公共字符串日期;

  • 我正在测试XCode 12.2中的通知。测试警报样式的通知非常容易。你只需在模拟器中将它们拖到你的应用程序上,有效载荷就会显示在: func应用程序(application:UIApplication,DidReceiveEmotentification用户信息:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping

  • 有没有办法从"GCM通知"获取数据。这是我用gcm发送的json字符串的一部分:。我需要在我的应用程序中获取id的值,但我不知道如何...非常感谢。

  • 有办法看到所有通过Firebase发送的通知吗? 我在文件或其他地方找不到任何东西。 如果可以在控制台上实现,那就太棒了,但是HTTP也可以!有什么想法吗?