extension joomla, template joomla,banner extension joomla,jomla slider,slider joomla
Google Cloud Messaging (GCM) in Android using PHP Server

GCM for Android is a service which is basically used to send the data from the server to the android devices. One use of this GCM is a push notification service. In this tutorial, I am going through all the steps needed to setup the GCM and build a simple but complete android application in Eclipse.

GCM architecture

GCM Architecture – source: official GCM documentation

The GCM architecture contains the following main three components.

  1. GCM connection server: It receives the messages from the application server and sends these messages to the GCM enabled Android devices.
  2. Application server: It sends the message to the GCM connection server. I will use PHP to build the application server in this tutorial.
  3. Android Application: It receives the messages from GCM connection server after application server sends a message to the GCM connection server.

Life Cycle Flow

GCM life cycle
  1. The Android application enables the GCM by registering to the GCM. The application needs Sender ID to get the registration ID.
  2. GCM connection server receives the sender ID from the application and returns the unique registration id.
  3. The application sends the registration ID to the back-end application server for the storage.
  4. The application server stores the registration Id in the database.
  5. When a new message needs to send, the application server fetches the registration ids from a database and send to the GCM connection server along with the message.
  6. The GCM server sends the message to the application.

Basic Library and Tools Installation
Step I: Install Google Play Services SDK
In order to use Google Services like GCM, you need to have Google Play Services SDK. Look at this official documentation to set up the SDK. One important thing you should take care is in referencing the library. You should not reference the library directly from the Android SDK. Instead first copy the library (i.e. google-play-services_lib) into your current workspace and then reference. In Eclipse, you can do this by checking the “Copy projects into workspace” checkbox while importing the project.

Step II: Install Google APIs 
For testing the project in an emulator, you need Google APIs. Install the Google APIs and create a new AVD with Google APIs as the platform target.

Step III: Install GCM for Android library
In SDK Manager.exe, expand the extras, select and install the Google Cloud Messaging for Android. Now you have set up all the library needed to create a GCM application.

Registering with Google Cloud Messaging 
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.

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.
5. In the displayed list of APIs, turn the Google Cloud Messaging for Android toggle to ON.
6. In the sidebar on the left, click APIs and auth > Credentials.
7. Click CREATE NEW KEY and select Server Key.


8. Provide the list of IP address from which the GCM server accepts the request. Left blank if you want to allow any IP.
9. Copy down the Server Key, you will need this later.

Creating a simple PHP application 
So far we have installed necessary libraries and register our account to the Google Cloud Console. Now let us create a simple application server in PHP. Our application server receives the registration id from the application, stores it in the database and sends the message to the application using GCM connection server. To store the registration id, create a simple MySQL table using the following query. [I have created a database named ‘GCMDemo’ for this example]

When the android application runs for the first time, it registers the application to the GCM using sender ID (i.e. project ID mentioned above) and gets the registration Id. The application then sends the registration ID to the application server to store in the database. Here is the PHP file register.php which does the above-mentioned task i.e. it receives the registration id and stores it in the database.


If you want to send the message to the application then you write a message and fetch all the registration ids from the database. You then send the registration ids along with the message to the GCM server. The GCM server gives the response back to the application server. index.php file does the above-mentioned task i.e. it sends the message to the application and echo back the response from the GCM server.


Put the files index.php and register.php inside the folder GCMDemo to run this code.

Creating the android application
The final steps are to create an android application named GCMDemo. The project structure is given below







When you run the above Android application, it displays a layout with a simple button. After clicking the button, the application checks whether it has registration id or not. If not then the application registers the device in the background. Once it gets the registration id, it sends that Id to the application server to store in the database.

The application now can receive the messages sent by the application server. Whenever it sense the new message, then it pushes the message as a notification using notification manager.

Related Article

destination source:https://www.programming-techniques.com/2014/01/google-cloud-messaging-gcm-in-android-using-php-server.html