Google Cloud Messaging 3.0(GCM) on Android


What is Push Notification & How it Works? :

  • GCM is used by 600,000 applications
  • GCM processes 1.1 million requests per second
  • GCM reaches 1.5 billion devices
  • If the targeted device is online, delivering  a message takes on average 50 ms. 95% of all messages are delivered in 173ms
  • Message Multicasting: Sending a message to 1,000 devices, (providing 1,000 Registration-Tokens)
  • Device Group Messaging: Sending a message to all devices (up to 20) in the group using a notification-key.
    • A device can also send a message to the other devices in the same group
  • Topic Messaging: send the same message to up to 1 million devices that have subscribed to the topic.

 

A very basic Architecture of GCM in a very simple words:

  1. First android device sends sender id, application id to GCM server for registration.
  2. Upon successful registration, GCM server issues registration id to android device.
  3. After receiving registration id, device will send registration id to our server.
  4. Our server will store registration id in the database for later usage.
  5. Whenever push notification is needed, our server sends a message to GCM server along with device registration id(which is stored earlier at server database).
  6. GCM server will delivers that message to respected mobile device using device registration id.

gcm_architecture

 

Registering with Google Cloud Messaging for Push Notification:

  1. Open the Google Cloud Console.
  2. If you haven’t created the API project yet, click CREATE PROJECT. Give the name of the project and click Create.
    Select Project
    Create New Project
  3. Note down the project number. You will use the project number as sender ID in the registration process.
  4. In the sidebar on the left, click APIs and auth. In the displayed list of APIs, turn the Google Cloud Messaging for Android toggle to ON.
    turn the Google Cloud Messaging API
  5. In the sidebar on the left, click APIs and auth and Credentials.
    Select Credentials
  6. Click CREATE NEW KEY and select Server Key.
    Creating New Server Key
  7. Provide the list of IP address from which the GCM server accepts the request. Left blank if you want to allow any IP. (Its recommended to provide IP address, so that anyone won’t access to it but for this tutorial purpose i left it blank.).
    gcm_demo7
  8. Copy down the Server Key, you will need this later.
    gcm_demo8

 

How does Registration works? :

  1. The end user installs the application.
  2. The App first sends a request for Instance ID to Google Cloud Messaging from the InstanceID API. This Instance ID provides a unique ID per instance of your Android and iOS apps.

     
  3. After successfully getting InstanceID, now we can request tokens from the Instance ID service as needed using the getToken() method. All tokens issued to your app belong to the app’s InstanceId.

     
  4. On successful on getToken(), Send this token to the App Server along with Device Unique ID.
  5. Why need of Device Unique ID?
    1. We do this to check whether this phone is registered already.
    2. We don’t want people signing up with multiple accounts with the same device.
    3. Using Device Unique ID helps some times we end up with duplicate registration.
    4. Device Unique ID can also be an Instance ID on this case or you can choose Device Hardware.
  6. On successful on uploading data’s to App Server, we can send message from server to the device.

 

How does Push Notification works? :

1. Sending Downstream Messaging/Push Notification from Server :

  • To address or “target” a downstream message, the app server sets to with the receiving client app’s registration token.

     

2. Receiving a Push Notification on Device :

  • To receive  a Push Notification on device, use a service that extends GcmListenerService to handle messages captured by GcmReceiver. GcmReceiver extends WakefulBroadcastReceiver, guaranteeing that the CPU is awake so that your listener service can complete its task.
  • By overriding the method GcmListenerService.onMessageReceived, you can perform actions based on the received message.

     

How does Unregistration works? :

An application can be automatically unregistered after it is uninstalled from the device. However, this process does not happens right away in GCM, as Android does not provide an uninstall callback. What happens in this scenario is as follows:

  1. The end user uninstalls the application.
  2. The App server sends a message to GCM server.
  3. The GCM server sends the message to the device.
  4. The GCM client receives the message and queries Package Manager about whether there are broadcast receivers configured to receive it, which returns false.
  5. The GCM client informs the GCM server that the application was uninstalled.
  6. The GCM server marks the registration ID for deletion.
  7. The App server sends a message to GCM.
  8. The GCM returns a NotRegistered error message to the 3rd-party server.
  9. The App deletes the registration ID.

Beyond this, Our App can Unregister a device using deleteToken().

 

What is Canonical IDs? :

  • If a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.
  • GCM provides a facility called “canonical registration IDs” to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.
  • If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you’re using will stop working.

The response from the serve will be similar to this:

If you notice, push notification successfully delivered to 0, 3 and 4 and failed to 1, 2 and 5 of registered ids but upon successful on registered id 4, it provides a new registration id which is called canonical id which has to be updated on our App server. Note: For updating canonical ids, no need to do anything on App side, Server side can handle everything.

 

Complete Source code(Android app code & PHP server code) available here.

References:

  1. Try Cloud Messaging for Android
  2. Simple Downstream Messaging
  3. Google APIs for Mobile: Quickstarts – code i’m using for this tutorial.

About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.