Saturday, February 1, 2014

Google Cloud Messaging (GCM)

I am back after long time but with tons of mobility knowledge to share with you. I am going to start with the cloud related technology as you know cloud computing and wearable technologies are the future technologies.(Microsoft also thinks so... that's why their cloud head Satya Nadella is front runner to become next Microsoft CEO.)
Let's start with Google Cloud Messaging (GCM)...

Introduction

GCM is advanced push technology developed by Google for Android platform. It replaces Android’s old push technology C2DM (Cloud to Device Messaging) which has many limitations. Google has deprecated C2DM and stopped manual sign-ups for the C2DM service officially on June 26, 2012 at the Google I/O 2012 event. Current C2DM developers can continue using the service, but without increase in quota. For more details on C2DM, read my previous blog here.
Android developers can use the GCM service to send notifications to Android clients running on the Android devices. GCM service takes care of all aspects of queuing and delivering messages to these target devices.
The paramount factor which differentiates GCM from other push notification services is the bi-directional messaging. To supplement this aspect, GCM has introduced Cloud Connection Server (CCS), which supports XMPP protocol.

Salient Features of GCM

  • It is a free service.
  • Supports payload data up to 4KB.
  • Compatible with Android 2.2+ (Devices with Google play store application installed OR Emulator with Google APIs).
  • Apps need not be deployed through Google Play Store.
  • Upstream messaging (device-to-cloud) using GCM Cloud Connection Server.
  • Multi-device messaging – send message to multiple devices owned by a single user.
  • Applications are free to provide their own UI to handle raw data received from GCM. No built-in interfaces for handling notification data.
  • No additional burden on network as it uses existing connection to Google services.
  • Application need not be running in background to receive GCM messages as they get delivered to  the app by the system using Intent broadcast.
Notes:
  • GCM generally uses 5228 port, and occasionally, the 5229 and 5230 ports. Therefore, ensure these ports are opened in your organization’s firewall. In addition, firewall should accept all outgoing connections to all IP addresses contained in the IP block, listed under Google’s ASN* of 15169 (as there is no specific IP address for GCM).
  • Microsoft’s Windows Azure Mobile Service has notification hub, which supports sending push notifications to Windows 8, Windows Phone 8, Android and iOS. It supports broadcasting notification to Android devices via GCM.
  • Visit Google Developer console (not API console) to view the GCM statistics.
*ASN – Autonomous System Number

Workflow - Below diagram shows the entire GCM workflow starting from registration to message delivery. GCM workflow has major 3 steps such as GCM registration, sending message and receiving message. 

Before adding support for GCM in your Android application, make sure below prerequisites are fulfilled
  • Project Number – Used as sender ID for GCM registration. To obtain the project number, create an API project using Google API.
  • Enable GCM – Go to Service tab in Google API Console and enable the GCM service
  • API Key – In Google API Console, click API Access tab. In API access screen, select the Create New Server Key option to create new key. All third party applications will use this API key as part of header while sending message to the GCM server.

GCM vs C2DM


Core functionality of GCM is similar to C2DM. However, it has added many powerful features to provide simple and efficient way of pushing messages to the device. Let's explore the differences between GCM and C2DM and gather information which will help smooth migration from C2DM to GCM.

Differences - GCM differs from C2DM by having additional useful features and enhancing existing ones. The features that differentiate it are as follows:

  • Simple API Key – this API key can be created from Google API console page.
  • Sender ID – GCM uses project number acquired from API console instead of an email address as used in C2DM.
  • JSON format – JSON support in addition to plain text.
  • Multicast messages – supports simultaneous sending of a single message to multiple devices.
  • Multiple senders – App can receive message from multiple parties using a single registration ID.
  • Time-to-live messages (Expiry) – Expiry can be set between 0 to 4 weeks.
  • Messages with payload – supports up to 4KB of messages

Migration - If you are migrating from C2DM to GCM, then you need to make below mentioned changes

  • Client changes – a client app needs to use project number instead of email account as sender in registration intent. If a server needs to support existing C2DM registrations, then client app has to send some flag (indicating its GCM) along with the registration ID. This will enable the server to differentiate it from requests coming from existing C2DM apps.
  • Server changes – Perform the following simple modifications to ensure successful migration for server apps.
    • Change GCM request endpoint to https://android.googleapis.com/gcm/send
    • Use API key instead of authentication token in authorization header of the request

Conclusion

Applications can integrate GCM and take advantage of Google’s powerful push infrastructure. Most of the Google apps available on Android devices make use of this existing infrastructure and so you can be sure that this service is stable and reliable. GCM also provides better user experience for end-user without any additional cost as this service is free while other alternatives like SMS or polling mechanism can cost more to the user in terms of network usage and battery consumption.

If you want to know more about GCM, you can download GCM whitepaper from here