Android Push Notification using Google Cloud Messaging (GCM) Part 1

Google Cloud Messaging enables android application to easily communicate with server for sending and receiving the data. Every time when new data is available to server it will deliver it to application in device as soon as device is found online.

We will cover push notification in two parts.

1. Registering with Google API Console and WebServer Configuration

2. Developing GCM supported Android Application

Overview of GCM Communication between Client and Server

1. Android device sends SENDER_ID and APPLICATION_ID to GCM server for registration.

2. After registration success, GCM server issues a REGISTRATION_ID to android device

3. On receiving REGISTRATION_ID, android device will send this id to web server.

4. Web Server will store GCM registration id in it database for later usage.

5. Whenever a push notification is required to send, web server sends a message to GCM server along with registration id.

6. GCM server will deliver this message to android device using registration id.

Registration with Google APIs console

1. For enabling GCM support in an android application, We need to obtain api key from Google APIs Console page.   Login using your Google id, you will get option to create a new project. 

 

step1_registration

 

2. After creating project in above step, you will be redirected to new project. Note down project number here for later use. We will use this id as SENDER_ID in our android application. You can check it later on in project overview and project id and project number can be retrieved.

 

step2_projectId

 

3. In API Project -> APIs & Auth section find Google Cloud Messaging for Android. Accept terms and conditions and toggle it’s status to ON.

 

step3b_enaleGCMAndroid

 

4. After enabling GCM for Android, Go to APIs & auth -> Credentials and click create new key and select server key to generate API KEY.

 

step4_createKey

 

In details, fill your allowed IP’s or just mention 0.0.0.0/0 to allow all.

 

step4c_serverKey

If you have completed above steps, you managed to get

1. Project Number

2. API KEY

Configuring Web Server

 

1. Download and install WampServer if not already installed. Run WampServer, click and start phpMyAdmin or navigate to http://localhost/phpmyadmin

2. Create a new database named gcm_demo. Navigate to SQL query run console and execute below query to create table users.

3. For sending POST request to GCM server, we will use PHP Client URL Library. Click wamp system tray icon and navigate to PHP -> PHP extensions and check if php_curl is enabled or not. If not, enable it.

4. Open www directory. Navigate wamp system tray icon -> www directory. Create a new directory name web_server_demo_gcm

5. Create a file named config.php. It will have info about database configuration and Google API KEY.

6. Create another php file named db_connect.php for creating and closing database connection. Please refer php mysql functions for better understanding.

 

7. Create another file db_functions.php for performing mysql operations of adding and listing users.

 

8. For sending push notification request to GCM server, create another file named gcm_sendmsg.php

 

9. Create yet another file register_user.php for saving user registration request from android device.

 

10. Create file named device_sendmsg.php, it will be used to send push message to android device by requesting to GCM server.

 

11. Create file index.php, it will list all the registered user so far and it will provide simple interface to send push message to registered user.

We have accomplished Google API console registration and web server configuration. Let move to part 2 for Developing GCM supported Android Application.

You may also like...