Search code examples
androidparsingpush-notificationtitaniumpayload

Can't parse push payload message in Titanium


In my app, i couldn't parse the "payload" object coming from push notification. There is automatically added '/' in each property in payload after push sent from server. How can i parse the payload property/object and get the notification data in my code?

Here is the payload object :

"payload":"{\"android\":{\"badge\":\"2\",\"alert\":\"Microfinaa_new_ne\",\"sound\":\"door_bell\",\"icon\":\"little_star\",\"vibrate\":true,\"title\":\"Mahboob Zaman\"}}"

And here is the full notification message coming from fcm server:

{"type":"callback","source":{"showTrayNotification":true,"pushType":"gcm","enabled":false,"showTrayNotificationsWhenFocused":false,"singleCallback":false,"focusAppOnPush":false,"showAppOnTrayClick":true,"debug":false,"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"__propertiesDefined__":true,"_events":{"callback":{}}},"payload":"{\"android\":{\"badge\":\"2\",\"alert\":\"Microfinaa_new_ne\",\"sound\":\"door_bell\",\"icon\":\"little_star\",\"vibrate\":true,\"title\":\"Mahboob Zaman\"}}","bubbles":false,"cancelBubble":false}

And here is my code -

CloudPush.addEventListener('callback', function(evt) {

    var json = JSON.stringify(evt.payload);

    Ti.API.info("datos = " + json.android);// This line shows undefined

});

Solution

  • Payload is already string you need to parse it and use inverse function

    var json = JSON.stringify(evt.payload);
    

    JSON.stringify(Object) -> return String JSON.parse(StringOject) -> return Object