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

[su_note note_color=”#e6f7f3" radius=”5"]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. [/su[/su_note]

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

[su_[su_button url=”http://cs2guru.com/samples/web_server_demo_gcm.zip” target=”blank” style=”soft” background=”#51d461" color=”#ffffff” size=”6" center=”yes” icon=”icon: arrow-circle-o-down”]load Complete Source Code[/su_but[/su_button] 

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.

You may also like...

8 Responses

  1. Blair Doyle says:

    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 says:

      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!

  2. Sandeep Autade says:

    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 🙂

  3. Dharmesh atDuma says:

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

  4. Karthick B says:

    which or what url want set there……

    private static final String WEB_SERVER_URL = “YOUR_WER_SERVER_URL”;

  5. gunjan says:

    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

  1. January 30, 2015

    […] with Google API Console Registration and WebServer Configuration. If not, it is suggested to refer Google API Console Registration and WebServer Configuration before proceeding […]

  2. January 27, 2018

    […] recommend to read the articles Android Push Notification using Google Cloud Messaging (GCM) – Part 1 and Part 2. They are a very good explanation and tutorial how GCM works. Even in this article a […]

Leave a Reply

Your email address will not be published. Required fields are marked *