Reputation: 71
I am trying to send and receive a JSON GCM message but on the client it seems to always come through as UTF8.
protected HttpURLConnection post(String url, String contentType, String body)
throws IOException {
Print.logInfo("In HttpURLConnection: " + contentType + body);
byte[] bytes = body.getBytes();
HttpURLConnection conn = getConnection(url);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Authorization", "key=" + key);
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
return conn;
}
On the server the UTF8 message is: In HttpURLConnection: application/x-www-form-urlencoded;charset=UTF-8registration_id=APA91...&delay_while_idle=0&collapse_key=Test&time_to_live=2419200&data.Data1=Value+1&data.Data2=Value+2&data.Data3=Value+3
and the JSON message is: In HttpURLConnection: application/json{"delay_while_idle":false,"collapse_key":"Test","data":{"Data1":"Value 1","Data2":"Value 2","Data3":"Value 3"},"time_to_live":2419200,"registration_ids":["APA91..."]}
with multicast result MulticastResult(multicast_id=5036...,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1362.. ]]:
Both messages are successfully received on the client but I can not get the json format to be recognised.
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String inAction = intent.getAction();
// check to see if it is a message
if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {
// if your key/value is a JSON string, extract and parse it using JSONObject
String json_info = intent.getExtras().getString("data");
if (json_info != null) {
try {
JSONObject jsonObj = new JSONObject(json_info);
payload = jsonObj.get("Data1") + "\n"
+ jsonObj.get("Data2")+ "\n"
+ jsonObj.get("Data3");
}
catch (JSONException e) {
// do nothing
return;
}
}
else {
payload = intent.getStringExtra("Data1") + "\n"
+ intent.getStringExtra("Data2")+ "\n"
+ intent.getStringExtra("Data3");
}
}
}
In Both cases json_info is null. I am using a java servlet on the server running apache/tomcat.
Please Help. Many Thanks
Upvotes: 1
Views: 2743
Reputation: 393856
The JSON object is only used in the communication of your server with the GCM server. The app never gets a JSON object. The data of the notification is passed as key/value pairs. If for some reason you want to pass a JSON payload, you should encode it as a String and pass that String as the value of one of the key/value pairs inside the data
JSON object. Then you'll have to read that value from the extras in the intent and parse the JSON.
onMessage(Context context, Intent intent): Called when your server sends a message to GCM, and GCM delivers it to the device. If the message has a payload, its contents are available as extras in the intent.
Actually this quote is more relevant :
data
A JSON object whose fields represents the key-value pairs of the message's payload data. If present, the payload data it will be included in the Intent as application data, with the key being the extra's name. For instance, "data":{"score":"3x1"} would result in an intent extra named score whose value is the string 3x1. There is no limit on the number of key/value pairs, though there is a limit on the total size of the message (4kb). The values could be any JSON object, but we recommend using strings, since the values will be converted to strings in the GCM server anyway. If you want to include objects or other non-string data types (such as integers or booleans), you have to do the conversion to string yourself. Also note that the key cannot be a reserved word (from or any word starting with google.). To complicate things slightly, there are some reserved words (such as collapse_key) that are technically allowed in payload data. However, if the request also contains the word, the value in the request will overwrite the value in the payload data. Hence using words that are defined as field names in this table is not recommended, even in cases where they are technically allowed. Optional.
Upvotes: 5