Wednesday, July 3, 2013

Push Notification Services with Android, GCM and Google App Engine (Part 1)

 

 

Introduction

In this blog I’ll demonstrate a solution for implementing push notification services for Android applications. It will leverage on the existing technologies that Google provides for the developers: Android platform, Google Cloud Messaging (GCM) and Google App Engine.

As you are probably already familiar, GCM is push notification service for Android platform that delivers notifications to your phone. Notifications are commonly used for notifying user about installed application updates or important status updates, for example, the Twitter application that notifies you about re-tweets. It is important to mention that push notification services (PNS) should be used with precaution since there are intended only to notify user about crucial things at the current moment. For example, if a user bids on an auction on the eBay, he/she should be notified if someone places bigger bid. Also, notifications should not be used if the messages will come too frequently. Too frequent notifications will annoy user and he/she will probably uninstall your app. PNSs are also not intended for delivering any confidential content since they don’t support encryption and they should not be used for these purposes. Instead, make sure you meet requirements as frequency, importance and sending it in a relevant time.

Architectural overview

First let’s get familiar with GCM, in order to better understand how our solutions will look like in the very end.
GCM system consists of three components:

  • Client (Android application) 
  • Backend server (Google App Engine backend)
  • GCM service

GCM provides us services for sending notifications to the Android smart phones. These notifications are messages that are constructed and sent to GCM by backend server who initiates notification sending (in Figure01 step a), i.e. backend server behaves as notification trigger. Backend server also stores all registration_ids of every subscriber (phone with installed application) and with these information server knows which group of subscribers it wants to target (in Figure01 step 3). Mobile app simply asks GCM for registration_id and connects to push service in order to receive notifications (in Figure01 step 1). It can also send registration_id delivered by GCM to the backend server (in Figure01 step 3).
For using GCM notification system several credentials need to be used:

  • sender_id - project number that uniquely identifies the project in Google Application Console,
  • application_id - identifies the application and it’s necessary to be used for receiving messages,
  • registration_id - identifies device with the installed app and it’s also necessary to be used for receiving messages. Device receives it from GCM service during connection process.
  • sender_auth_token - or API_KEY that is used on a server side in order to use GCM services
  • google_user_account - for Android versions lower than 4.0, Google account needs to be used.

GCM

Figure01 – GCM notification cycle

The complete process of notification sending can be explained with the following steps (see Figure01):

  • enabling GCM:
    1. (1) device is sending a request to the GCM. The request contains sender_id and app_id.
    2. (2) GCM sends response containing app_id and registration_id.
    3. (3) mobile app sends received registration_id to backend server, which stores the id
  • sending a message:
    1. (‘a’) backend server with defined API_KEY sends a message content with a set of registration_ids of device it wants to target
    2. (‘b’) GCM receives the message from server and broadcasts it to the devices
  • receiving a message (on a device)
    1. mobile app receives the notification, extracts it and processes data
    2. displays message content

Generating credentials

Now we’ll go through steps of how to generate mentioned credentials that we’ll later use in our implementation.

Step 01: Go to Google API Console web site and log in with your Gmail account.

Google Api Console

Figure02 – Google API Console start page

Step 02: Create project…

Step 03: Enter project name and continue

Step 04: In the left navigation menu go to Services and enable Google Cloud Messaging for Android

GCMEnable

Figure03 – Turn on GCM for Android

Step 05: In the same menu go to API Access. Now you need to create a server key. Click on Create New Server Key… and for this case leave IPs field empty and follow with create (no IP address needs to be specified if you want to be in public domain).

Step 06: In the same menu go to Overview.

So far we generated following credentials:

  • sender_id: in the Overview you can see Project Number and that is your sender_id (will be used in Android application)
  • application_id: as explained before will be received from the GCM when the mobile connects to the GCM
  • registration_id: as explained before will be received from the GCM when the mobile connects to the GCM
  • sender_auth_token: in the API Access you can see under Key for server apps (with IP locking) the API key which is your sender authentication token (will be used in Google App Engine web application)
  • google_user_account: is not necessary since in this tutorial will develop Android 4.0 application

Now we are ready to implement our notification system. Follow this next blog.

4 comments:

  1. Thanks for provide the information.
    For more information please visit here push notification services.

    ReplyDelete
  2. Thankyou for sharing such a nice explanation

    ReplyDelete
  3. I was so confused about how Push Notifications works this blog post cleared all my doubts and confusions thanks for sharing this valuable information with us! And please keep sharing this type of articles. :)
    https://www.ducont.com/

    ReplyDelete