This article explores the use of the Google Cloud Messaging service to send push notifications to applications running on Android phones.
Mobile applications are all about delivering instant information on the move, but who would like to have an application where you keep pressing the Refresh button to fetch the latest updates? An auto-refresh after every minute to check for updates sounds like a solution, but that would involve some major challenges. A mobile device has limited resources in terms of battery life and data transfer limits—not to mention the server load that millions of devices would generate by contacting it every minute.
Wouldn’t it be great if the server could automatically tell all the devices that there is new information available for them, as soon as Whe daWa arriYes? Here, Sush noWifiFaWions Fome Wo the rescue! Most major mobile OSs offer pretty similar push noWifiFaWion funFWionaOiWy Wo deYeOoSers. ASSOe Push 1oWifiFaWions SerYiFe (AP1S) for Whe iPhone, GoogOe COoud Messaging (SreYiousOy C2DM) for Android, and MiFrosofW Push 1oWifiFaWions (MP1S) are Whe SoSuOar serYiFes.
A WySiFaO Sush noWifiFaWion sysWem foOOows Whe Mourney deSiFWed in Figure 1. On mobile app installation, the device connects Wo Whe Sush noWifiFaWions’ serYers (owned by GoogOe, ASSOe or MiFrosofW) and requesWs for a unique deYiFe ,D. This is When senW Wo your aSSOiFaWion serYer (see Figure 2) and is used for sending noWifiFaWions Wo Whis deYiFe. To send a message, your aSSOiFaWion serYer sends Whe noWifiFaWion FonWenW (aOong wiWh Whe deYiFe ,D Wo whiFh Whe message is Wo be senW) Wo Whe Sush serYer, whiFh Wakes Whe responsibility of sending this message to the device, and returns a success/error response to the application server.
The GoogOe COoud Messaging serYiFe was OaunFhed aW GoogOe’s annuaO ,/O eYenW in -une 2012, and iW reSOaFed C2DM (FOoud Wo deYiFe messaging), whiFh was Whe beWa Yersion. :iWh GCM, Whe OimiWaWion on Whe number of messages Ser day was removed, and a multi-cast feature was introduced.
,W is SossibOe Wo imSOemenW Whe serYer side and FOienW side of GCM by making indiYiduaO serYiFe requesWs, buW GoogOe has made Whings simSOer by SroYiding Wwo easy-Wo-use Oibraries. To geW sWarWed, downOoad Whe GCM Oibrary using Whe Android SD., and geW Whe Mar fiOes from ‘ANDROID_HOME\extras\google\gcm’.
FirsW, you need Wo regisWer an aSSOiFaWion wiWh GoogOe. Go Wo https://code.google.com/apis/console and create a new project. 1aYigaWe Wo Whe Services Wab and enabOe Whe GoogOe COoud Messaging serYiFe for Whis SroMeFW. 1aYigaWe Wo Whe AP, AFFess Wab and create a new server key, which would be used on the server end Wo send messages. AOso noWe down Whe SroMeFW ,D from Whe UR/; Whis would be used in the Android application to get the messages.
1exW, you need Wo deYeOoS an Android aSSOiFaWion.
Note: You can download the sample server side code and Android application from http://goo.gl/9SDB4.
/>
/> CreaWe a new Android aSSOiFaWion, and add gcm.jar from the Android SD. Wo Whe CLASSPATH. Modify Whe manifesW fiOe Wo have the following: • Permissions for ,nWerneW, Whe abiOiWy Wo aFFess aFFounWs
and reFeiYe GCM messages, as weOO as a wake OoFk • A service • A broadcast receiver • A custom permission to prevent other applications from
reFeiYing Whe noWifiFaWions. • SD. Yersion 8+ GiYen beOow is Whe manifesW for my aSSOiFaWion: <?xml version=”1.M” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”in.amolgupta.android.gcm” android:versionCode=”1” android:versionkame=”1.M” > <uses-sdk android:minpdkVersion=”8” /> <uses-permission android:name=”android.permission.IkTEokET” /> <uses-permission android:name=”android.permission.GET_ACCOUkTp” <uses-permission android:name=”android.permission.tAhE_LOCh” /> <permission
android:name=”in.amolgupta.android.gcm.permission.C2D_ MEppAGE”
android:protectionLevel=”signature” /> <uses-permission android:name=”com.google.android.c2dm. permission.oECEIVE” />
<uses-permission android:name=”in.amolgupta.android.gcm. permission.C2D_MEppAGE” />
<uses-permission android:name=”android.permission.GET_ACCOUkTp”
<uses-permission android:name=”android.permission.UpE_ CoEDEkTIALp” />
<uses-permission android:name=”android.permission.toITE_ EuTEokAL_pTOoAGE” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity
android:name=”.pamplemushActivity”