Start a Project

Android: GCM push notification

Earlier the polling mechanish is used to get the notifications on your android application. But that method has disadvantages like load on network and reduced battery life.

Polling Mechanism: In this method the application contacts (or hits) server or cloud (whatever the case is) to get the new notifications. This increases the network usage and reduces the battery life. And also sometimes there is no new message so all the efforts go in vain.

Push Mechanism: In this approach the server/ cloud itself connect to the application device whenever there is a new notification available. So it had saved the resorces. But the process is extremely typical one have to deal with queues, saving the notifications for resending at the time of offline device. But to made all these things easy we can use GCM.

Google Cloud Messaging:

Google provides an easy way to implement push notifications called GCM. GCM sends data from your server to your users’ devices, and receive messages from devices on the same connection. The GCM service handles all aspects of queueing of messages and delivery to client applications, and it is completely free.

GCM acts as a mediator between your server and client’s application. A full GCM implementation requires both a client implementation and a server implementation. I presume that you had already  implemented the server implementation. The client implementation of GCM is extremely simple, you just have to fallow the below mentioned steps:

Step 1: Go to http://console.developers.google.com and register your project over there. On successful registration you will get

  1.  A API key ( store it securely as it is needed on server side for sending messages to Google cloud).
  2.  A senderID/ ProjectID ( it is required in your app to register it with your server).
  3.  A google-services.json file, which you have to put in the root folder of your project (/app).

Step 2: Now add some support in your gradle files as:

  1. To project level build.gradle
  2. To app level build.gradle

Step 3: Add permissions as well as sevices and recievers to your app manifest file.

Step 4: For each user GCM generates a token (registration id). It is the unique id of the user through which the notification is sent to the clent. So each user have to register itself with GCM using your applications senderID. Add a boolean value to preference to determine that the to0ken is generated or not. If not then the app will generate a token n send it to your server. To do this extend IntentService class and register your app.

Your app or the Instance ID service may need to refresh tokens in the event of a security issue or when a user uninstalls and reinstalls your app during device restoration. Your app must implement a listener to respond to token refresh requests from the Instance ID service. This is implemented by YOUR InstanceIdService.

Step 5: Now just implement the listeners / recievers and you are ready to send or recieve your messages using GCM.

Exit mobile version