OneSignal通知,可通过点击

时间:2019-06-25 11:30:03

标签: android flutter

我检查了Open application on notification click in OneSignalOneSignal Push Notification Click to open actiivty,但仍然没有答案。因此,我正在构建一个新闻应用程序并将其与OneSignal集成,我能够收到通知,当我单击它时,我会收到以下JSON代码

OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
     this.setState(() {
  print(
 "Opened notification:
     ${result.notification.jsonRepresentation().replaceAll("\\n","\n")}");
   });
});

JSON响应

Opened notification: {
"payload": "{
"google.delivered_priority": "normal",
"google.sent_time": 1561415365754,
"google.ttl": 259200,
"google.original_priority": "normal",
"custom": "{"a":{"id":"43682"},"i":"b0f4ef57-9556-4163-9e5c-    fbcea08b4ce8"}",
"from": "800826514709",
"alert": "Test Alert",
"title": "Test Title",
"google.message_id": "0:1561415365759264%e192c215f9fd7ecd",
"notificationId": -655945385
I/flutter (30970): }",
"displayType": 2,
"shown": true,
"appInFocus": true,
"silent": null
I/flutter (30970): }

下面的代码捕获有效载荷密钥

print(result.notification.payload.jsonRepresentation());

JSON响应

{
  "google.delivered_priority": "normal",
  "google.sent_time": 1561415365754,
  "google.ttl": 259200,
  "google.original_priority": "normal",
  "custom": "{"rec":{"id":"43682"},"i":"b0f4ef57-9556-4163-9e5c-fbcea08b4ce8"}",
  "from": "800826514709",
  "alert": "Test Alert",
  "title": "Test Title",
  "google.message_id": "0:1561415365759264%e192c215f9fd7ecd",
  "notificationId": -655945385
}

我的问题是我如何读取密钥“ notificationId”和“ custom”,我尝试了类似下面的操作,但是却得到了空值

Map<String, dynamic> data =
        json.decode(result.notification.jsonRepresentation());

print(data['custom']);

2 个答案:

答案 0 :(得分:0)

我为您准备了这个解决方案。

var notificationId = result.notification.payload.rawPayload["notificationId"];

Map<String, dynamic> custom = result.notification.payload.additionalData;

祝你好运:)

答案 1 :(得分:0)

您可以通过此获取其他数据:

OneSignal.shared.setNotificationOpenedHandler((openedResult) async{
var additionalData = openedResult.notification.payload.additionalData;
if(additionalData != null){
  if(additionalData.containsKey("myKey")){
    var myValue= additionalData["myKey"];
  }
 }
});