javascript - 通过 toastr 访问 FCM 参数

标签 javascript firebase firebase-cloud-messaging toastr

我通过 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.Nickpayload.data.Room 访问数据属性。

关于javascript - 通过 toastr 访问 FCM 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54076094/

相关文章:

javascript - Firebase JS v9 中 Google Auth Provider 的 select_account 提示

android - 无法更新 Firebase 依赖项 (com.google.firebase)

php - GCM 与 PHP(谷歌云消息)

javascript - Map 和 Set 有什么区别?

javascript - 在哪里托管用于分发的 javascript 闭源前端库?

javascript - 如何制作坐标图螺旋

javascript - 如何将数据从 Google 表格发送到 Firestore Cloud?

android - Fcm 集成得到异常

javascript - 静态 html 页面错误行为

javascript - 使用 javascript 和 Sharepoint 按标题查找元素