我通过 FCM 收到了以下 JSON 格式的通知:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
我可以使用 payload.notification.title
访问通知标题和正文,如以下代码所示。
但我如何访问 data
部分中的其他参数?
// Handle incoming messages
messaging.onMessage(function(payload) {
console.log("Notification received: ", payload);
toastr["success"](payload.notification.body, payload.notification.title, {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"positionClass": "toast-top-left",
"onclick": payload.notification.click_action, // I also tried removing this
"preventDuplicates": false,
"showDuration": 30000,
"hideDuration": 1000,
"timeOut": 0,
"extendedTimeOut": 0,
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
});
});
最佳答案
这只是一个 JSON 对象,因此您可以使用 payload.data.Nick
和 payload.data.Room
访问数据属性。
关于javascript - 通过 toastr 访问 FCM 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54076094/