1. “Application Server” sends the JSON to “GCM Server”
2. “GCM Server” sends a message to “Android Client app” including what it gets from “Application Server”.
3. “Android Client app” receives this message as an intent.
(“Intent” in Android:- is a message that is passed between components like Activities, Services, Receivers in Android)
4. “BroadcastReceiver” in Android listens for this “Intent”.
5. This Intent has an attribute called “extras”. “extras”is a Bundle of additional information.
6. This “extras” contains the message details.
public class ExternalReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if(intent!=null){ Bundle extras = intent.getExtras(); } }
7.This “extras” include details as key/value pair.
JSON:
{
“delay_while_idle”: true,
“collapse_key”: “demo”,
“data”: {
“message”: “GCM Tests”
},
“time_to_live”: 125,
“dry_run”: false
}
@Android Client App:
{ String messageDetails = extras.getString("message"); }
8. This way if the server included key-pair values in the “data” parameter in JSON, they are also available as “extras” in this “intent”, with the keys being the “extra” names.
EX:-
JSON:
{
“delay_while_idle”: true,
“collapse_key”: “demo”,
“data” : {
“message”: “GCM Tests”
“sound” : “value for sound”,
“image” : “url for image”,
},
“time_to_live”: 125,
“dry_run”: false
},
@Android Client App:
{ String soundName = intent.getExtra("sound"); String imageUrl = intent.getExtra("image"); }
9. We can set these values extracted from intent as content text of a Notification.
@Android Client App:
{ final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Message Received!") .setContentText(messageDetails) .setContentIntent(pendingIntent) .setAutoCancel(true) .getNotification(); mNotificationManager.notify(0, notification); }
Thank You 🙂
Hello, I already could use Google GCM to send text message from server to android client, do you the way to attach image url to message? thanks!
LikeLike
Hi lizhong, I have stated that in the above post 🙂
When sending data to the client, in your JSON you include the data as key:value pairs.
message details send with “message” key.
“data” : {
“message”: “GCM Tests”
},
Same way you can define your own key:value pairs in the data section of JSON. Thus you can define a new key called “image” and image url as the value.
“data” : {
“message”: “GCM Tests”
“image” : “url for image”,
},
From the client app you can retrieve the “image” in the same way you retrieve “message” separately as follows;
String messageDetails = extras.getString(“message”);
String imageUrl = intent.getExtra(“image”);
ImageLoader.getInstance().displayImage(imageUrl ,messageImageView, options,
new ImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
}
});
Hope this is what you expected or else let me know 🙂
LikeLike