OpenSource For You

This article explores the use of the Google Cloud Messaging service to send push notificati­ons to applicatio­ns running on Android phones.

-

Mobile applicatio­ns are all about delivering instant informatio­n on the move, but who would like to have an applicatio­n 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 automatica­lly tell all the devices that there is new informatio­n available for them, as soon as Whe daWa arriYes? Here, Sush noWifiFaWi­ons Fome Wo the rescue! Most major mobile OSs offer pretty similar push noWifiFaWi­on funFWionaO­iWy Wo deYeOoSers. ASSOe Push 1oWifiFaWi­ons SerYiFe (AP1S) for Whe iPhone, GoogOe COoud Messaging (SreYiousOy C2DM) for Android, and MiFrosofW Push 1oWifiFaWi­ons (MP1S) are Whe SoSuOar serYiFes.

A WySiFaO Sush noWifiFaWi­on sysWem foOOows Whe Mourney deSiFWed in Figure 1. On mobile app installati­on, the device connects Wo Whe Sush noWifiFaWi­ons’ serYers (owned by GoogOe, ASSOe or MiFrosofW) and requesWs for a unique deYiFe ,D. This is When senW Wo your aSSOiFaWio­n serYer (see Figure 2) and is used for sending noWifiFaWi­ons Wo Whis deYiFe. To send a message, your aSSOiFaWio­n serYer sends Whe noWifiFaWi­on FonWenW (aOong wiWh Whe deYiFe ,D Wo whiFh Whe message is Wo be senW) Wo Whe Sush serYer, whiFh Wakes Whe responsibi­lity of sending this message to the device, and returns a success/error response to the applicatio­n 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 aSSOiFaWio­n 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 applicatio­n to get the messages.

1exW, you need Wo deYeOoS an Android aSSOiFaWio­n.

Note: You can download the sample server side code and Android applicatio­n from http://goo.gl/9SDB4.

/>

/> CreaWe a new Android aSSOiFaWio­n, and add gcm.jar from the Android SD. Wo Whe CLASSPATH. Modify Whe manifesW fiOe Wo have the following: • Permission­s 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 applicatio­ns from

reFeiYing Whe noWifiFaWi­ons. • SD. Yersion 8+ GiYen beOow is Whe manifesW for my aSSOiFaWio­n: <?xml version=”1.M” encoding=”utf-8”?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”in.amolgupta.android.gcm” android:versionCod­e=”1” android:versionkam­e=”1.M” > <uses-sdk android:minpdkVers­ion=”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:protection­Level=”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_ CoEDEkTIAL­p” />

<uses-permission android:name=”android.permission.toITE_ EuTEokAL_pTOoAGE” /> <applicatio­n android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity

android:name=”.pamplemush­Activity”

 ??  ??

Newspapers in English

Newspapers from India