I'm sending push notifications from my own server via Urban Airship to a PhoneGap-based Android app. The notifications are sent successfully - however, I would like to be able to send additional data that does not show up in the notification bar in Android, but that I can still extract within the app. As I've understood it, this is supposed to be stored as key-value pairs in the "extra" field of the push notification. I have troubles encoding and extracting this in the right way.
The server is a simple ASP.NET web interface with a code-behind page that sends the message to Urban Airship (based on this https://stackoverflow.com/a/2528151/1822286). I save the data from the user's interaction (which consists of filled in forms and the coordinates of an imported Google Maps instance) using JavaScript and JSON into a hidden field. This field is then read by the code-behind page in ASP.NET and used to construct the push notification.
JavaScript to hidden field stringData (the event variable is from the Google Maps instance which I've left out here since that part works):
var allDataToString = {
"alert": "\"" + document.getElementById("alertTextBox").value + "\"",
"data": "\"" + document.getElementById("dataTextBox").value + "\"",
"lat": "\"" + event.latLng.lat() + "\"",
"lng": "\"" + event.latLng.lng() + "\""
};
document.getElementById("stringData").value = JSON.stringify(allDataToString);
The push notification message in the C# code-behind page:
string postData = "{\"aliases\": [],\"tags\": [],\"android\": {\"alert\": \"alerttest\",\"extra\": {\"data\": \"" + stringData.Value + "\", \"notificationtype\": \"1\"}},\"apids\": [\"APID\"]}";
A more readable version:
string postData =
"{
\"aliases\": [],
\"tags\": [],
\"android\":
{
\"alert\": \"alerttest\",
\"extra\":
{
\"data\": \"" + stringData.Value + "\",
\"notificationtype\": \"1\"
}
},
\"apids\": [\"APID\"]
}";
The message is successfully sent via Urban Airship to my phone. In the app, I want to extract the stringData.Value part, which contains the alert, data, lat and lng variables. I'm unsure of whether the error lies in the way I choose to encode it server-side, or in the way I parse it client-side. It's confusing since the terminology differs between Urban Airship's documentation and web dashboard, using "alert" and "extra", while other things on their PhoneGap plugin side are called "incoming" and "incoming.message".
In PhoneGap JavaScript:
push.getIncoming(function(incoming) {
if(incoming.extra!=undefined){
var data = JSON.parse(incoming.extra.data);
var alert = data.alert;
var data = data.data;
var lat = data.lat;
var lng = data.lng;
}
});
Thanks in advance.