Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(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!

like image 807
guskuma Avatar asked Jul 20 '13 22:07

guskuma


People also ask

What is GCM push notification?

Google Cloud Messaging (GCM) was a mobile notification service developed by Google that enables third-party application developers to send notification data or information from developer-run servers to applications that target the Google Android Operating System, as well as applications or extensions developed for the ...

Does WhatsApp use GCM?

WhatsApp(and several other Apps) rely upon GCM/FCM(as option 1 - the default) as it is present as a system App on lot of devices and therefore holds a special status where it is very less likely to be killed unlike a normal App. For devices that do not have play services, your custom socket connection is relied upon.

How does GCM work in Android?

GCM stands for Google Cloud Messaging. Every push notification receive on any Android device is sent by the GCM only. when sender sends an push notification then it goes to GCM. GCM receives that push and forward it to particular Android Device by its Unique device id.


2 Answers

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.

like image 97
CommonsWare Avatar answered Sep 21 '22 19:09

CommonsWare


You have to add another level to json data, apparently they do not parse json recursively. Send:

{...., "data":{"payload":{"id":5,"a":5}}}

Then in code:

String json = intent.getExtras().getString("payload");
// json = {"id":5,"a":5}

This can be easily converted to POJO, For me this approach is better if you have classes already ( or just want to work with objects instead of get...("xyz")

like image 28
FDIM Avatar answered Sep 21 '22 19:09

FDIM