Google Cloud Messaging GCM: Push Service

By | June 5, 2015

Google Cloud Messaging GCM: Push Service

Today’s internet technology is still strongly challenged by the thought of how to push data from the server to a client or a user instead of pulling data from the server, which involves constantly polling the server to detect changes and then pull the data changes.
Many articles have been published on different ways to “simulate” push services on the web. Here is the good news for android developers. Here is a “real” push service from Google that works great.
I am going to take a step by step approach on explaining how it is achieved and how we can make use of it. But let me quickly explain why you need it.

Why Push or GCM

Having a working push service has ever been the dream of developers who want to get information to clients without them requesting for it. That is, you are developing an application which needs to respond to changes on the server. Then if you send reoccurring requests to know this you causing performance on the server, you consuming band width of both client and user, then you consuming the clients power.

Push notifications allow you to send notification updates to the users of your apps. You can deliver relevant information to your users, such as event broadcasting, sport results or breaking news.


A GCM implementation includes a Google connection server, an app server in your environment that interacts with the connection server via HTTP or XMPP protocol, and a client app.

Figure 1. GCM Architecture.

This is how these components interact:

  • Google GCM Connection Servers take messages from your app server and send them to a client app.

  • On your app server, you implement either the HTTP and XMPP protocol to communicate with the GCM connection server(s). App servers send messages to a GCM connection server; the connection server enqueues and stores the message, and then sends it to the client app. For more information, see About GCM Connection Server.

  • The Client App is a GCM-enabled client app. To receive GCM messages, this app must register with GCM and get a unique identifier called a registration token. If you are using the XMPP connection server, the client app can send “upstream” messages back to the app server. For more information on how to implement the client app, see the documentation for your platform.

    From – Google Developers

The Google Developers articles on GCM explains into full detail the concepts and structure of implementing the Google Cloud Messaging (GCM). But I will just give a working example along side explanations on how to implement on Android with Java (for the client which is an Android device) and PHP (Server) using HTTP.

As we see in the previous quote, to send a notification to an android device:

  • A third party server (the app server – maybe your website) sends message to the Google GCM Server, and
  • The Google GCM Server delivers the message to the android app.

So therefore you need to set up the:

  • API Key for authentication from Google (This is like registering your self to be able to use GCM).
  • Client (The Java code to drive the android app), and
  • App Server (the server-side code to send messages to the app)

Note: We are going to be using PHP for the App Server via HTTP. For other languages or XMPP please check the GCM on Google Developers

Setting up the Client

To Set up we are going to follow these following steps:

  1. Register the App for the use of GCM – this will return an App Id by which  your server will recognize the specific app
  2. Send the App Id to the server
  3. Then Listen for messages from the server by a BroadcastReceiver, and
  4. Utilize the information received by a Service.

No we are going to take the steps one by one. You should set up a new Android application project or use an existing one. I will only focus on implementing the GCM so the general Application works and the use of the information is left to you.

1. Registering the App for the use of GCM

This could be done probably at every start of your application (onCreate of an Activity) or at every start of the service that needs the information.

This needs the Google Play service installed on the device and you need to add the library to your project. See How to add Google Play services to your Android Project here.

Firstly we will check if the app is registered, then if not we will register it. See code below:

In the above code we check if the device is supported and if not we ask the user to download. If not, we display a Toast that the device is not supported and we exit the application. You can update the code to do as you like.

Now Check and register the App:

Note: I did this in a service that I start even before the application starts. So there is need that I perform another check for the Google Play services but in this case I do not have to display any dialog if it is not installed because it is from a service, so I just call stopSelf(); on the service if it is not installed without saying anything to the app user.

See the full service code below:

I hope the above code is quite explaining and simple. What we did is we checked if it is registered, if not, the we register it and store the App Id in a SharedPreference and also send it to the server (App server) for the server to store it so we can send messages through the app Id later. Once you are registered you are registered so you don’t need to re-register.

In my own case this service is woken up at boot and the onStartCommand returns Service.START_STICKY so it is always alive.

Okay now! Where getting there. We have successfully registered the App for Google Cloud Messaging.

To avoid writing a too long article the rest of this article is found in this Part Two.

You might want to drop comments on this, feel free.

Leave a Reply

Your email address will not be published. Required fields are marked *