从Web推送通知有效负载中获取价值

时间:2018-04-06 19:22:01

标签: javascript payload web-push

我通过网络推送通知收到了这个有效负载。

{
  "data": {
    "notification": "{\"icon\":\"\\/static\\/assets\\/notifications\\/loader-100x100.png\",\"vibrate\":[200,100,200,100,200,100,400],\"tag\":\"request\",\"body\":\"12:30\",\"title\":\"HELLO\",\"actions\":[{\"icon\":\"\\/static\\/assets\\/notifications\\/loader-100x100.png\",\"action\":\"https:\\/\\/example.com\\/#done\",\"title\":\"OK\"},{\"icon\":\"\\/static\\/assets\\/notifications\\/loader-100x100.png\",\"action\":\"https:\\/\\/example.com\\/#open\",\"title\":\"Edit\"}]}"
  },
  "collapse_key": "do_not_collapse",
  "from": "1111111"
}

如何获得标题的价值? 我没试好就试过这个:

var parsed = JSON.parse(payload);
const notificationTitle = parsed["notification"]["title"];

1 个答案:

答案 0 :(得分:1)

使用该对象只需

var parsedNotification = JSON.parse(payload.data.notification);
var title = parsedNotification.title;
console.log(title);