Search code examples
cordovalocalnotification

cordova-plugin-local-notifications and data


I use a plugin cordova called cordova-plugin-local-notifications in order to receive notifications on my device everyone so far except that I can't retrieve the value of a key on the data here's my code :

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

You can see that data have a profilId which is set to someValue here's my code for the notification clicks

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

And it's here that I have a problem because notification.data is well set to : "{"profilId":"somevalue"}" but profilId is undefined.

If anyone can explain me where a I did wrong that's will be great.

Thanks for your time.


Solution

  • when you build the notification put profilId in quotes--the data wants to be JSON formatted.

    {"profilID":"somevalue"}

    to unpack it (this is not tested!)

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