Introduction

As per Google documentation,
“Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devices”.
Using this service you can send data to your application whenever new data is available instead of making requests to the server in a timely fashion. Integrating GCM in your Android application enhances user experience and saves a lot of battery power. I will explain how to send push notifications to Android devices from a Java server.
Steps required to do this are as follows:
  1. Register with Google Cloud Messaging from the Google API console to get the sender id and API key for Google cloud messaging.
  2. Set emulator for Google cloud messaging library.
  3. Create an Android project to register with Google Cloud Messaging (GCM).
  4. Create a server-side code that will accept the registration id and will send push messages to the Android devices.
Describing each step in details,
  1. Go to Google API Console.
  2. If you haven’t created a project yet, this page will prompt you to do so.
    create a project
  3. After creating the project you can see the project Id in the URL. Note down the project id which will be used further as SENDER_ID in the Android project, and it will be like the following.
    Sender ID
  4. In the API console page select services and toggle on Google Cloud Messaging for Android.
  5. Click API Access and note down the API Key.
    Api key
Before we start writing the Android code we need to install the helper libraries and make required changes to the emulator. These library methods will be used to register and unregister with GCM.
  1. Go to the Android SDK folder and open SDK Manager and install Google Cloud Messaging for Android library under Extras.
    Insrtall
    Code
  2. After installing the library it will create gcm.jar file in your ANDROID_SDK_FOLDEREXTRASGOOGLEGCM-CLIENTDIST.
  3. Now open your AVD Manager and create a new Google API Emulator and start the emulator.
  4. After launching emulator press menu button, go to Settings, select Accounts & Sync. Then press Add Account button and add a Google account.
    NB: If you not add a google account and run this example then you will get an error account_missing.
Create the Android Application
AndroidManifest.xml
The following permissions are required to make your application support GCM
Read more articles on Android