cordova-plugin-local-notifications 和数据

cordova-plugin-local-notifications and data

我使用一个名为 cordova-plugin-local-notifications 的插件 cordova 来接收我设备上的通知,除了我无法检索数据上的键的值,这是我的代码:

cordova.plugins.notification.local.schedule({
      title : "Test notif",
      text: "un profil a été modifié",
      data: {profilId:"somevalue"}
});

您可以看到数据有一个设置为 someValue 的 profilId,这是我的通知点击代码

cordova.plugins.notification.local.on("click", function(notification){
      sessionStorage.setItem("myIndex", notification.data.profilId);
      window.location.href='details.html';
});

我在这里遇到问题,因为 notification.data 设置为:“{"profilId":"somevalue"}”,但 profilId 未定义。

如果有人能向我解释我哪里做错了,那就太好了。

感谢您的宝贵时间。

构建通知时将 profilId 放在引号中——数据需要 JSON 格式。

{"profilID":"somevalue"}

解压(未测试!)

var unpackedData = JSON.parse(notification.data); var notificationProfilID = unpackedData['profilID'];

我遇到了同样的问题。 Notification数据解析成String格式(不知道为什么)。

只需使用JSON.parse:

JSON.parse(notification.data);