Reputation: 71
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.
Upvotes: 2
Views: 1357
Reputation: 971
I had the same problem. Notification data is parsed in String format (I don't know why).
Just use JSON.parse:
JSON.parse(notification.data);
Upvotes: 0
Reputation: 271
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'];
Upvotes: 3