This tutorial is about the new Android push notification using GCM. Android Push notification is the service provided by GCM. The old  c2dm and “Google Cloud Messaging for Android library” are depreciated as of now. Now it’s recommended to implement Google Cloud Messaging(GCM) using the Google Play Services library.
Note : – In the end of the tutorial you can find a download link for this GCM project (server side as well as the Android application) , a video tutorial for GCM theory and the second part of the push notification using GCM tutorial.

Setting up GCM

google-console-api-cloud-messaging-GCM

  • Open the Google API Console page and click on create project.
  • Go to Google Cloud Messaging for Android and set its status as ON.
  • Get the Project Number, which will be used as the sender ID.
  • Go to API Access and get the API Key.

Installing Play service

In Eclipse, go to Window and then Android SDK Manager. Scroll down and install Google Play Services.

android-play-service-sdk-for-GCM

After downloading Google Play Service, a new folder will be created inside your Android SDK folder and you need to import it.

Adding the Google Play library project into an Eclipse workspace

After sucessfull installation, go to the location where the Android SDK is installed on your computer.

Now copy the google-play-services_lib folder to your desktop.

google-play-service-sdk-path

Now we will import this library into an Eclipse workspace. Again go back to Eclipse.

Creating a new Android push notification using a GCM application project

For using the Android push notifications in your project, you need to add the Play Service library. So, create a new project and right click on it. Go to Properties, then go to Android. Select “Add”  which is inside the library form and select “google-play-services_lib” and click “ok ” and then “apply”. Now everything is set up. I know setting up the project is the annoying part, but we have to do this if we want to implement our code. Now let’s get going towards implementation.

Connecting an Android device to GCM and receiving a registration ID.

 Explanation

  • EXTRA_MESSAGE is used for debugging.
  • registration_id will be storing the ID with which an Android device gets registered with GCM.
  • SENDER_ID is the project number which you get from the Google Code website.

  • gcm is an instance of GoogleCloudMessaging which provides access to Google Cloud.
  • The TextView will display the registered ID of a device in the user interface and “regid” is a string holding the device registation ID.
  • Context tells about the current state of the application. We call it to get information about our application. For example, when we are working with a background thread, we use context.

Now we will get the instance from GoogleCloudMessaging and assign it to gcm. And we will call RegisterBackground, which is our AsyncTask that helps to connect to Google Cloud using a background thread.

But why do we want another thread?

Each Android application runs in its own process and in one single thread which is called Main thread or UI thread. Any operation which is heavy or which will block our main thread, should be implemented in a separate thread. For example, Networking is a blocking operation. Here, we are receiving data from the internet. That’s why we are implementing it in a background thread. I hope things are getting much clear now.

AsyncTask helps us to implement a background thread. It has few methods which you can override. “doInBackground” is the method which will have the main code. “onPostExecute” will contain stuff to update the data of the TextView which we have received from the internet.

  • Context represents our application.
  • The Register method is used to register the device.
  • The Log class is used for debugging.

The device will be registered and the GCM server will have the registered ID of the device. Now we will send this registration ID to our web server.

Here is the code which displays “device registration id”.

Registration with GCM using Google Play Services

Make sure to add the following entry in the Android manifest file. To see the complete manifest file, kindly go to the second part of this Android push notification using GCM tutorial.

Now let’s implement the sendRegistrationIdToBackend() method.

Sending an Android device registration ID to our server.

Explanation

In this method, we will use HTTP to send our data to our server. If you want to learn about HTTP and JSON parsing, please go to the Android HTTP and JSON parsing tutorial.

Receiving the Android device registeration ID on the server.

getdevice.php – A PHP script to receive the ID from a device so that we can save it in our MySQL database.

Implementing Server side using PHP

Next in this Android GCM tutorial, we need to create a PHP script which will be used to send the data from our server to GCM using CURL. Then we will implement a broadcast receiver and IntentService to receive the data from GCM. After that, we will display our data as a notification. Then we will create one more activity to display data using a JSON parser.

If you need to buy web hosting and a domain name for your Android GCM application to host PHP,  you can check this list of the best web hosting India providers.

Explanation

  • api_key  holds the key which we got from the Google Console API.
  • registrationIDs is an array which will be storing the registration ID of Android devices on which you want to send the message.
  • message a variable that stores the string which we want to send.
  • fields is an associative array which will be storing the registration ID’s and the data which is to be sent.
  • headers will store the API Key which we got from the Google Console API. It will also determine that our PHP script is sending JSON data.

Now the communication between GCM and our server will be done using curl. curl is a command line tool for transferring data with URL syntax. Now let’s see the steps for setting up curl.

  1. Create a new curl resource using curl_init().
  2. We will set the URL.
  3. CURL_POST means that the packet will be sent via HTTP POST.
  4. CURLOPT_RETURNTRANSFER helps us to get the result back to our server.
  5. CURLOPT_POSTFIELDS sets the data which we are sending through HTTP POST.

Now let’s see how to process the notification received from GCM on an Android device.

Second Part – Implementing a broadcast receiver and IntentService to receive JSON from GCM.

How to get data from GCM (Google Cloud Messaging) notification in Android

Download Android push notifications using GCM and server side in PHP.

Download GCM project

How GCM works (theory)

Feel free to post if you have any errors or suggestions.