katzer / cordova-plugin-local-notifications

Cordova Local-Notification Plugin
Apache License 2.0
2.57k stars 1.75k forks source link

data parameter return undefined in click event #1151

Closed hguldal closed 7 years ago

hguldal commented 7 years ago

cordova.plugins.notification.local.schedule({ id: Math.floor((Math.random() * 1000000) + 1), title:'title', text:'text', data:{value:'key'} });

cordova.plugins.notification.local.on("click", function (notification) { alert(notification.data.value); //print undefined
}); notification.data.value return undefined why ?

SammyIsra commented 7 years ago

notification.data is a JSON string. In your case, that variable looks like this:

'{"value":"key"}' Or something in that style. To be able to access it like an object, you have to use

var data = JSON.parse(notification.data);
console.log(data.value) //This would output 'key'

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Hope that helps!

rwillett commented 7 years ago

This was also reported here #1138. @SammyIsra has the same solution as we used :)

Rob

vjoao commented 7 years ago

This should be in the docs =)

rwillett commented 7 years ago

Can this be closed now?

Rob

SammyIsra commented 7 years ago

I believe so, yes.

rwillett commented 7 years ago

Only you can close it 😃

Rob

wuilmerj24 commented 7 years ago

Hello I have a similar problem but with ionic 2. I have this variable public clickNotification: boolean = false;

Which must be changed to true when clicking on the notification: LocalNotifications.on ("click", function (notification) { Console.info ("After the click" + this.clickNotification); });

And the console displays undefined

rwillett commented 7 years ago

@wuilmerj24

Your issue seems to bear no resemblance to the OP issue. I suggest you fill out the template in #1188 and raise a new issue and lets close this down.

Rob

SammyIsra commented 7 years ago

@rwillett I cannot close this

rwillett commented 7 years ago

You are correct, I should have referred to @hguldal, Apologies.