Google Cloud Messaging (GCM)

GCM Intro
To begin using GCM, we must have a Google ID. Please refer to the following Getting Started guide link below.


Android App and Registration ID

The Android app will need to initiate a registration intent to the C2DM server with the email address of our Google ID and the app ID to enable C2DM for the device. If successful, the C2DM server will return the app a Registration ID which should be passed to our application server to store to the database and use it for notifications later

We need to configure the following in AndroidManifest.xml to enable the Intents for registration and receiving registrations.

<!-- Only this application can receive the messages and registration result -->
<permission android:name="com.yourapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.yourapp.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="" />

<application android:icon=”@drawable/icon” android:label=”@string/app_name” android:debuggable=”true”>
<!– Only C2DM servers can send messages for the app. If permission is not set – any other app can generate it –>
<receiver android:name=”.App$MyGcmReceiver” android:permission=””>
<!– Receive the actual message –>
<action android:name=”” />
<category android:name=”com.yourapp” />
<!– Receive the registration id –>
<action android:name=”” />
<category android:name=”com.yourapp” />


The following codes in will register the device for push notifications and receive a unique Registration ID such as:

public void onCreate(Bundle savedInstanceState) {

mc = new MyClass(this, super.appView);
super.appView.addJavascriptInterface(mc, “MyClass”);
Intent registrationIntent = new Intent(“”);
registrationIntent.putExtra(“app”, PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra(“sender”, [value of Project ID]);
String deviceId = Settings.System.getString(getContentResolver(),Settings.System.ANDROID_ID);

public static class MyGcmReceiver extends BroadcastReceiver {
private Context context;
public void onReceive(Context context, Intent intent) {
this.context = context;
if (intent.getAction().equals(“”)) {
handleRegistration(context, intent);
} else if (intent.getAction().equals(“”)) {
handleMessage(context, intent);

private void handleRegistration(Context context, Intent intent) {
String registration = intent.getStringExtra(“registration_id”);
if (intent.getStringExtra(“error”) != null) {
// Registration failed, should try again later.
Log.d(“gcm”, “registration failed”);
String error = intent.getStringExtra(“error”);
}else if(error == “ACCOUNT_MISSING”){
Log.d(“gcm”, “ACCOUNT_MISSING”);
}else if(error == “AUTHENTICATION_FAILED”){
}else if(error == “TOO_MANY_REGISTRATIONS”){
}else if(error == “INVALID_SENDER”){
Log.d(“gcm”, “INVALID_SENDER”);
}else if(error == “PHONE_REGISTRATION_ERROR”){
} else if (intent.getStringExtra(“unregistered”) != null) {
// unregistration done, new messages from the authorized sender will be rejected
Log.d(“gcm”, “unregistered”);
} else if (registration != null) {
//Log.d(“gcm”, “registration: “+registration);

private void handleMessage(Context context, Intent intent)
//Do whatever you want with the message
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.notificationicon; // icon from resources
CharSequence tickerText = “Alert from Your App”; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = mContext.getApplicationContext(); // application Context
CharSequence contentTitle = “Your App Name”; // text
CharSequence contentText = intent.getStringExtra(“payload”); // expanded message

Intent notificationIntent = new Intent(context, App.class); // call App class, ie. launch this App
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

// the next two lines initialize the Notification, using the
// configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(10001, notification);

The handleMessage() method will do the necessary to display a notification message on the device and when the notification is clicked, it will launch your app.

The MyClass class holds the Device ID and Registration ID and it is exposed to the HTML5 app using this line of code “super.appView.addJavascriptInterface(mc, “MyClass”)”. In the HTML5 app, we simply call “window.MyClass.getDeviceid()” and “window.MyClass.getRegistrationid()” to get the Device ID and Registration ID. So the HTML5 app will be able to call another web service to our app server with the account username, Device ID and Registration ID to be stored into our system. As the Registration ID might be changed periodically by the GCM server, we need to keep updating the Registration ID tied to each Device ID.


Sending a Message from the App Server

Follow this GCM Demo guide from Google – and integrate the codes into your Java app server.


Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: