Android Push Notification using Google Cloud Messaging (GCM) – Part 2

If you are here, you are already done with Google API Console Registration and WebServer Configuration. If not, it is suggested to refer Google API Console Registration and WebServer Configuration before proceeding further.

GCM Enabled Android Application

1. Setting Google Play Service For enabling support, Google Play Services library is required to add in project. Refer to Google Play Services Setup.

If you are eclipse user, Go to menu Window -> Android SDK Manager -> Extras. Install Google Play Service library from there. It will be downloaded to “sdk\extras\google\google_play_services”.

2. Preparing application manifest Add following permissions in your application manifest file. Declare a receiver in your manifest to receive GCM message and registration id. Add meta data for google play services. Add a service for handling GCM messages in background. Replace all occurrences of com.androidsrc.gcmsample with your package name.

 

3. Setting up UI We will keep it simple. Our launcher activity will have simple one button to register it with GCM server. And one text view for showing registration id after successful registration. Modify your layout res/layout/activity_main.xml as below.

 

4. Setting up SRC files

Create GCMBroadcastReceiver which will extend WakefulBroadcastReceiver in order to keep device awake while processing message from GCM.

 

Create service GCMIntentService which will extend IntentService to perform task of showing notification in a background thread.

 

In MainActivity.java, on clicking of register button we will handle task of registering our application with help of GoogleCloudMessaging api’s. After getting registration id from GCM, we will register with our web server for saving registration id.

We are done with Sample GCM Application. After registration, Go to your web server and try sending message to registered devices via GCM. You will receive push notification from GCM server.

You may also like...