Flutter:如何从 firebase 通知负载中获取 Json 数据
Flutter : How get Json data from firebase notification payload
我需要在收到来自 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: notification message {gcm.message_id: 1578573493122232, id: 30, google.c.a.e: 1, type: test, aps: {alert: {title: notication title, body: Message of Push notification}, sound: 1}}
任何人都可以帮助从通知负载中获取数据吗?
终于可以自己解决这个问题了
如果您想从通知负载中获取数据,那么您需要在消息的 "data"
字段中发送数据。
SAMPLE CODE read data from "data"
-field of the message.
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);
}
我需要在收到来自 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: notification message {gcm.message_id: 1578573493122232, id: 30, google.c.a.e: 1, type: test, aps: {alert: {title: notication title, body: Message of Push notification}, sound: 1}}
任何人都可以帮助从通知负载中获取数据吗?
终于可以自己解决这个问题了
如果您想从通知负载中获取数据,那么您需要在消息的 "data"
字段中发送数据。
SAMPLE CODE read data from
"data"
-field of the message.
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);
}