katzer / cordova-plugin-local-notifications

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

Getting the data from notification object in a click event #1320

Closed Rukshar closed 6 years ago

Rukshar commented 7 years ago

Provide a general summary of the issue.

Your Environment

Expected Behavior

this.localNotifications.on("click", (notification) => { console.log(notification.id); });

I should be able to get the id, and other keys from the notification object from a click event.

Actual Behavior

When I type notification.x none of the keys seem to exist. I have tried to use JSON.parse(notification), however, it seems that notification is already an object. When I log the notification and log JSON.stringify(notification), I get the same result. Furthermore, if I copy the log and paste it in a variable, then I am able to get the keys out of it.

Steps to Reproduce

  1. Schedule a local notification
  2. In the constructor put the click event code: this.localNotifications.on("click", (notification) => { console.log(notification); console.log(JSON.stringify(notification); console.log(JSON.parse(notification); // gives an error }); This is what the result of console.log(notification) looks like: {"at":1497123780,"id":1,"title":"Title","sound":"res://platform_default","badge":0,"text":"something"} This is what the result of console.log(JSON.stringify(notification)) looks like:{"at":1497123780,"id":1,"title":"Title","sound":"res://platform_default","badge":0,"text":"something"}

    1. When making a new variable and using the results from above I am able to get the keys (for example in the constructor or any other function): let notificationObject = {"at":1497123780,"id":1,"title":"Title","sound":"res://platform_default","badge":0,"text":"something"} console.log(notificationObject.id)

Context

I am trying to get the keys and their values out of the notification object, so that I can pass it to another function. I hope someone can help me or knows if there is a duplicate of this issue. I am not sure what I am doing wrong and if I am handling this correctly.

Thanks in advance

rwillett commented 7 years ago

We never got this to trigger in Ionic 1, suspect you won't in Ionic 2.

Tawpie commented 7 years ago

I’d try parsing the individual properties…

We get the .data via JSON.parse(notification.data) just fine.

It’s worth trying JSON.parse(notification.id) and so on

On Jun 14, 2017, at 6:57 AM, Rob Willett notifications@github.com wrote:

We never got this to trigger in Ionic 1, suspect you won't in Ionic 2.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

rwillett commented 7 years ago

@tawpie Can you expand on this?

Does this mean you ARE getting the click notifications?

Tawpie commented 7 years ago

Ha! I'll have to check, our architecture obviates the need, but at one point in time I did write and test the click and trigger handlers.

(spelling and grammer powered by autocorrupt)

On Jun 14, 2017, 7:49 AM -0700, Rob Willett notifications@github.com, wrote:

@Tawpie Can you expand on this? Does this mean you ARE getting the click notifications? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

rwillett commented 7 years ago

That would be good. We also changed the way our code works to remove this requirement but I'd love to know if your code managed to trigger (pun intended) this trigger.

Thanks

Rob

Rukshar commented 7 years ago

@Tawpie I tried your method (i.e. JSON.parse(notification.id)) and from the console.log I saw that it works. It actually logs the id of the notification that was clicked. I tried the other keys and that also works.

I tried to log notification.id without parsing the json first, and that also logs the values of the keys. I think I was confused because normally -if you have such an object- you can see the keys as suggestion in your editor. Which is now not the case. Nonetheless, you can use the keys to get the corresponding values.

Thank you both for you responses! @rwillett @Tawpie.

rwillett commented 7 years ago

Mmmm... I'll look at this again as we never got the click event.

Tawpie commented 7 years ago

Sorry for the delay, other projects calling.

This is for iOS 10.3, Android has not been tested (for us, this is a bit of an ugly test to make happen)

The summary is that if the app has been force quit, we don't get either onClick or onTrigger. But the app does launch. We only get onTrigger if the app is in the foreground. When the app is background but NOT force quit, we get onClick when you click the notification in the reminder shade/pull down.

katzer commented 6 years ago

Please reopen if its still a topic with 0.9-beta