Android Push Notifications with GCM – Client Side

Overview of Google Cloud Messaging

As per google’s documentation “Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devicespush-notification-gcm-apns-diagram-featured.png

  1. Send a request to GCM from the device to register (with the google project number).
  2. GCM sends back the device registration ID.
  3. Device sends the device registration ID to the provider.
  4. Provider sends a message to the GCM (with API key and the device registration ID).
  5. GCM sends the message to the device.

Downloading the SDK

Creating Google project

  1. Go to and create an account.
  2. Now create a new project.Selection_003.png
  3. Then click on Enable and manage API’s and enable Google Cloud Messaging for Android API for the project.Selection_004.png
  4. Then click on Go to credentials and you can get your API keys from there. These keys will be needed to send push notifications from the server. The API key will be not needed to register devices for push notifications. But Project number is needed to register devices.
  5. You can get the google Project Number from the project home.Selection_006.png

Creating Android Project

  1. Create a new android project by providing necessary details.
  2. Import the downloaded library into the project
  3. After creating the project open the AndroidManifest.xml and add the following permissions.
    INTERNET – To make your app use internet services
    ACCESS_NETWORK_STATE – To access network state (used to detect internet status)
    GET_ACCOUNTS – Required as GCM needs google account
    WAKE_LOCK – Needed if your app need to wake your device when it sleeps
    VIBRATE – Needed if your support vibration when receiving notification
    Also you need to add some broadcast receivers as mentioned below.
    Final AndroidManifest.xml will look like this.

    In the above code change the org.wso2.pushnotifications with your package name

  4. Now create a class called This class handles all the GCM related services. GCMIntentService class will look like this.

  5. Now all the hard work is done.  You can use the following method provided in the library to do the rest of the work. (Please note that parameters are not mentioned here)
    GCMRegistrar.register – To register the device.
    GCMRegistrar.getRegistrationId – To get the device registration ID.
    GCMRegistrar.isRegisteredOnServer – To check whether the device is registered on your server.

  6.  Now you need to store the devices IDs some where. This library provides a class called and using this class you can do that part very easily. Following methods can be used to register on the server.(Please note that parameters are not mentioned here)
    ServerRegistrationUtilities.register – To register on server.
    ServerRegistrationUtilities.unregister – To unregister from the server.
    *Please note that the url patterns should be like this.
    …………………../register  for registration.(DeviceId will be passed)
    ……………../unregister  for unregistration. (DeviceId will be passed)

Send a test notification

  1. You can send a push notification from your server using the google API key or just use an online tester. 

One Comment Add yours

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.