user2000150 user2000150 - 2 months ago 19
Android Question

(New) GCM message received, but how to parse?

I am working with the "new" GCM, part of Google Play Services, that does not use jars for both Android and server. Android uses Google Play apk to register, receive and even send messages. At server side all we need to communicate with GCM is to do a POST to their server (if not using XMPP). I have all set: my Android app registers and waits for messages coming from GCM. My server can communicate with GCM and I'm able to deliver messages to my Android app.

The problem: I'm using a common jar (Android and server) with POJO objects that I transform to and from JSON. Since POST to GCM from my server uses JSON and received messages in my Android uses too (at least I believe it does), I thought it would make my life easier. But when receiving a message from GCM in Android I don't know how to get message's payload and transform to my objects. I can see the informations there but it's not my object (there are some information GCM push into it).

My question is: Is there a way to get, in Android end, payload information the way I've sent to GCM server? Or should I parse received data in Android without JSON?

Message I sent ("data" is payload)


{"registration_ids":["APA91bFdV..."],"collapse_key":null,"delay_while_idle":false,"time_to_live":2419200,"data":{"type":10,"targetVersion":-1,"message":"test msg","url":"test url"}}


What I got in Android intent.getExtras().toString()


Bundle[{message=test msg, collapse_key=do_not_collapse,
targetVersion=-1, from=483833987130, type=10, url=test url}]


I'd appreciate your help. Thanks!

Answer

Is there a way to get, in Android end, payload information the way I've sent to GCM server?

Rather than thinking "JSON", think "key/value pairs". Your server sends data in key/value pairs. Your app receives that data as key/value pairs in the extras in the Intent. You know what your keys are, so just retrieve the values out of the extras that are tied to those keys (e.g., getStringExtra("message"), getStringExtra("url")).

Or should I parse received data in Android without JSON?

The received data is not in JSON.