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...

  • Pingback: Android Push Notification using Google Cloud Messaging GCM()

  • Blair Doyle

    I followed this tutorial, and substituted the windows web server with an ubuntu nginx LEMP setup.
    I am running nginx on ubuntu for my webserver w/ php5-fpm.
    confirm PHP is running fine, other php scripts work a-ok on the web server.

    When my android app attempts to register with GCM / Web server , I see “Successfully Registered with GCM” , my web server receives the call to register_user.php , but my web server reports the following error:

    “PHP message: PHP Fatal error: Cannot use isset() on the result of an expression (you can use “null !== expression” instead) in /usr/share/nginx/html/register_user.php on line 9″ while reading response header from upstream, client: 10.16.224.158, server: localhost, request: “POST /register_user.php HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php5-fpm.sock:”

    Code in question per the tutorial is as follows:

    line 9 —> if (isset(isset($_POST[“regId”])) {
    $gcm_regid = $_POST[“regId”];

    • Blair Doyle

      Of course moments after posting I realize that the example code in this tutorial is flawed:

      if (isset(isset($_POST[“regId”])) {
      $gcm_regid = $_POST[“regId”];

      should be:
      if (isset($_POST[“regId”])) {
      $gcm_regid = $_POST[“regId”];

      Not sure how this got missed and more people havn’t raised a flag!.

      Now on to the next issue.. after successful registration, No push notification is sent to the device! doh!

  • Sandeep Autade

    on db_functions.php page having error that table name not matching please correct that
    // insert user into database
    INSERT INTO reg_users

    // check for successful store
    SELECT * FROM users

    thanks for tutorial 🙂

  • Dharmesh atDuma

    returns only “to” value when i execute send push notification

  • Karthick B

    which or what url want set there……

    private static final String WEB_SERVER_URL = “YOUR_WER_SERVER_URL”;

  • gunjan

    Nice tutorial but it runs on android 4.4 when I m tring to execute it on lollypop or marshmallow it will not receive the notification if the app is not running on the background.Pls help me how can I resolve that issue