Blog Post

Android, JAVA, Mobile, PHP

Push Notifications Using Google Cloud Messaging (GCM) 

We had to implement in some of our android apps the ability to send push notifications in order to send a message or a notification of a new event to the devices, request data synchronization, etc. In this case we will be showing a scenario using PHP and MySQL on the backend, which will serve downstream messages through HTTP to our app.

As you might know the worflow in which Google Cloud Message works is as follows:

  1. First android device sends sender id and application id to the GCM server for registration.
  2. Upon successful registration GCM server issues registration id to android device
  3. After receiving registration id, device will send registration id to our server
  4. Our server will store registration id in the database for later usage.
  5. After this, whenever a push notification is required, our server sends a message to GCM server along with the device registration id
  6. GCM server will deliver that message to the right mobile device using that registration id.

In order to register your project into the Google Cloud Messaging you will need to access to Google APIs Console page

The project ID you get will then be the one used as the sender id in your android app. You must then go and click on Services on the left pane and turn on Google Cloud Messaging for Android.

Finally, click on API Access and note down the API Key, as this API key will be used when sending requests to GCM server.

Ok now let’s jump to the code and see how it is done!

Components

In order to get this to work we’ll need to create a few components in our android app.

GcmBroadcastReceiver

This is the Receiver that receives notifications from GCM servers and starts a service to process the message in a background thread. It basically forwards the intent to the background service, in our case will be using an IntentService. If you noticed, we are using a WakefulBroadcastReceiver which is a helper class for implementing a Receiver that gets a device wakeup event (holding a wake lock) and starts a service to do the work, all this ensuring that the device doesn’t go back to sleep while the service is setup and starts. The service then notifies the receiver to release the wake lock once it’s work is done.

We then have to define the receiver in the manifest, providing the right intent filter to be able to respond to GCM messages. Consequently, we set the intent filter action to be com.google.android.c2dm.intent.Receive

GcmIntentService

This is the IntentService that handles the intent that comes from GcmBroadcastReceiver and sends a notification with the message content once it’s done

As well as we did with the receiver, we need to define the IntentService in the application manifest in order to have it available in our app.

Permissions

Your application must request some permissions to the Android system.

 

Example code

The following method will check if the Google Play Services APK is installed and available on the device. If not, it will attempt to fix the error showing an error dialog which will lead the user to Google Play app so they can download and install the required APK.

The following method registers the application with GCM servers asynchronously and stores the registration ID and app versionCode in the application’s
 shared preferences.

Once you have the Registration Id from GCM servers, you need to send it to your backed server which will include the id when triggering a notifcation. The following example shows a Volley request, sending the registration id to our server. Further reading about Volley here

On the backend, you’ll need to have a place to store this registration Id (usually in a table)  and associate it with your users in order to attach the Id to the push notification requests you send to GCM servers.

This is the snippet of code on the php backend that sends the notification to the device through the GCM servers.

And that’s pretty much it. We encourage you to try introducing push notifications in your app, as it not only introduces a mechanism to send push notifications from your backend and get them displayed them in your android status bar, but it also allows you to have your server begin a communication with your app. This is useful to prevent your app from having to poll your server for some new event or data, which means avoiding the cell radio draining your battery and have your app use fresh data instantly. Moreover, you can have your server trigger periodic sync updates at any given time or when some new data is available and eligible to be updated on your client apps.

(Visited 3,076 times, 1 visits today)

Related posts

Leave a Reply

Required fields are marked *

en_USEnglish
en_USEnglish