You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

Google Cloud Messaging and its use for pushing notifications on Android App

0.00 avg. rating (0% score) - 0 votes android mobile app for jobseekers enables users to get notifications anytime anywhere using Google Cloud Messaging or GCM service. In this post we will walk you through the details of GCM.

GCM.. What is it?

Google Cloud Messaging (GCM 3.0) is a free service that helps developers send messages across multiple platforms: Android, iOS, and Chrome. A server can send messages directly to single devices, groups of devices, or devices subscribed to particular topics. Using this service you can send data to your application whenever new data is available instead of making requests to server in timely fashion. GCM first launched as Google’s Android Cloud to Device Messaging (C2DM) service. Now, C2DM is deprecated.

Each notification can carry a payload up to 4KB. It is used by 600,000 applications and processes 1.1 million requests per second. It has a user base of over 1.5 Billion devices.

Why GCM?

Integrating GCM in your application enhances user experience and saves both battery power and data usage as the application no longer needs to poll the server. Google now also allows developers to set ‘topics’ to their apps in which users can choose the subjects they are interested in getting notified. i.e. Topic Messaging. This allows your app server to send messages to those devices which have subscribed to a particular topic. For example, users of sports app could opt for “score” updates and can receive the notifications of matches.

Welcome to GCM world 🙂 Have a look at some frequently used terms

  • REG_ID or Registration token – When the client app registers with the GCM, it sends the reg token back to the client which is later on used to send the GCM message to the device. There is one difference when messaging to Apple devices. For iOS one needs to connect to an APNS server to get a token which is then used to get the GCM token.

  • MESSAGE_ID – When a message is received by the GCM, it sends a unique GCM message identifier in response message to the client server. When the message id is received by the server from GCM, it does not mean that the message was delivered to the device. It simply indicates that it was accepted by the GCM for delivery.

  • COLLAPSE_KEY – This is basically an arbitrary string (such as “Updates Available”) that is used to collapse a group of like messages when the device is offline, so that only the last message gets sent to the client. This is intended to avoid sending too many messages to the phone when it comes back online.

  • TIME_TO_LIVE – How long the message should be kept on GCM storage if the device is offline.

  • DELAY_WHILE_IDLE – If included, indicates that the message should not be sent immediately if the device is idle. The server will wait for the device to become active.

  • CANONICAL_ID – If for some reason, the client app triggers multiple registrations for the same device then, GCM will entertain any request made by old registration tokens but it will include the canonical ID in the REGISTRAION_ID field of the response. It is basically a registration token of the the last registration requested by the client app.

  • THROTTLING – If the server sends a large number of messages within short period of time, then to optimize the network efficiency and battery life of devices GCM implements throttling of messages.

Is GCM Battery Efficient?

The android device keeps a single connection open to GCM servers to listen for notifications for all apps on the device, and then routes messages to the appropriate applications they are intended for. This is much more scalable and efficient than keeping a network connection open for every single application wanting to have some sort of push notifications. The connection itself is likely a TCP connection that’s left in an open state, even when the phone’s goes idle. It can wake the device when data is received.

How stuff actually works ?

  1. You need to generate API key for your app
Screen Shot 2015-07-30 at 1.32.24 am.png

2. Next select the Cloud Messaging and select your platform
Screen Shot 2015-07-30 at 1.32.38 am.png

3. Then create your app
Screen Shot 2015-07-30 at 1.33.08 am.png

4. Choose your desired services Screen Shot 2015-07-30 at 1.33.56 am.png



5. Finally, copy the API key and use it in your code



function _send_notification($registration_ids , $message) {
   $groups = array_chunk($registration_ids, 1000);

   foreach($groups as $group) {
       $fields = array(
               ‘registration_ids’ => $group,
               ‘data’ => $message,
       $headers = array(
               ‘Authorization: key=’ . YOUR_API_KEY,
               ‘Content-Type: application/json’,
               ‘Connection: keep-alive’,
               ‘Keep-Alive: 300’
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);
       if ($result === FALSE) {
           die(‘Curl failed: ‘ . curl_error($ch));
       return $result;

No one is Perfect…. So is GCM

  • The main area of concern in GCM is Security. Researchers from various antivirus companies have identified threats like Trojan-SMS.AnroidOS.FakeInst.a etc which can delete incoming text messages, generate shortcuts to malicious sites, can display malicious notifications etc.

  • There is a limit on how many messages can be stored without collapsing. Currently, it is 100. But if the limit exceeds, all stored messages are discarded and the device receives a special message which indicates that the limit was reached.

  • Also, the payload length is just 4KB.


Overall, GCM is not suitable for real time application scenarios as its message delivery time is unpredictable. But it performs fairly well in random multicasting of messages. So, over-relying on GCM is not advisable.

3 thoughts on “Google Cloud Messaging and its use for pushing notifications on Android App

Comments are closed.