arleyandrada / PushClient

FREE and Open Source - Titanium PushClient Module
Other
44 stars 17 forks source link

JSON.stringify(event) #95

Closed somefakeuser closed 9 years ago

somefakeuser commented 9 years ago

This is for android.

so I looked at what Ti.API.info('eventCallback:' + JSON.stringify(event)); outputs and a get this:

{"type":"PushClient_Callback","source":{"bubbleParent":true,"apiName":"Ti.Module","invocationAPIs":[],"_events":{"PushClient_Success":{},"PushClient_Error":{},"PushClient_Callback":{}}},"mode":3,"data":{"from":"1076345567071","gcm":{"messageType":"gcm","handlerId":0},"collapse_key":"do_not_collapse","data":"{\"alert\":\"test\",\"push_hash\":\"098f6bcd4621d373cade4e832627b4f6\"}"},"bubbles":false,"success":true,"code":0,"cancelBubble":false}

Now when i use iOS, i simple open an alertbox and display JSON.stringify(event.data.alert); and it works. BUT with android and looking at the JSON output above, it seems that i will always get undefined because event.data.alert does not exist.

So my question is, what do I need to call to get Android's alert message from the event?

Hope I made sense. Thanks very much.

arleyandrada commented 9 years ago

To securely get the "decoded" JSON payload, try to use something like:

var dataContainer = (OS_ANDROID) ? event.data.data : event.data;
var payload = (typeof dataContainer === "object") ? dataContainer : JSON.parse(dataContainer);
alert('Notification: ' + payload.alert);

Regards,

Arley