Extracting title and message from push notification payload.

Please i’ve been having issue extracting title and message from a push payload.

My push payload is this

{"google.sent_time":1509962407640,"grp_msg":"","title":"Thank you","alert":"Thank you so much","vis":"1","custom":"{\"i\":\"7715aff8-4fca-4d5d-afc3-57dfdacc1574\"}","pri":"5","google.message_id":"0:1509962407645917%7f7d296df9fd7ecd"}

and my code to notifying user is below

push.on("receivedMessage", function(payload) {
	        console.log("Recieved Push Notification: " + payload);
	        LocalNotify.now(payload.title, payload.alert, payload, true);

However the i noticed payload.title and payload.alert returns null.

Please what are mine doing wrong?

Thank you.

To see exactly what payload object looks like, you can do this:

push.on("receivedMessage", function(payload) {

That should give you a good enough idea on what you receive and how to access properties of that object.

Thanks Uldis. I’ve already posted the content of the payload in my previous thread and it baffles me why i cannot extract the content of the payload for example payload.title

Below is the output of the console.log(JSON.stringify(payload));

"{\"google.sent_time\":1509964250115,\"grp_msg\":\"\",\"title\":\"This messg\",\"alert\":\"This is content\",\"vis\":\"1\",\"custom\":\"{\\\"i\\\":\\\"ff1f9d2e-48ad-49f5-9d5d-7d873a7560cd\\\"}\",\"pri\":\"5\",\"google.message_id\":\"0:1509964250123641%7f7d296df9fd7ecd\"}"

I still get a null output when i try payload.title

No idea then. You’ll need to share a complete, minimal example that we can copy-paste-and-run to reproduce the issue.


Oh, I got it.

The payload you get there is a string. You need to JSON.parse(payload) to turn it into an object.

Thanks so much Uldis.

That was exactly the issue. It’s now working fine. Thank you so much for your help.