G OOGLE CLOUD MESSAGING FOR ANDROID Bindu Priya Infosys Limited Image Courtesy: http://developer.android.com/guide/google/gcm
The Challenge Mobile Phone Data in the cloud
H OW TO KEEP DATA ON YOUR DEVICE F RESH ? Polling Pushing
Polling is Simple to Implement Any New Updates? No Any New Updates? No Device Application server
Polling Every 5 min can consume 10% of your battery AND.. Nobody appreciates the apps Nobody appreciates the apps which drains the battery life of the Phone
Pushing Server Informs the phone once the new data is available Data Device Application server
C2DM Framework How many of you have heard about Cloud to Device Messaging Framework
Android Cloud to Device Messaging C2DM Live Score Score Server Live Score App
Android Cloud to Device Messaging • Google Account • Client Login Token • Sign up Form • Activation Email Activation Email • Initial Quota
Google Cloud Messaging • Simple to use • No Client Login Token • No Signup Form • No Quotas • No Quotas • Takes 4.7ms to deliver the messages.
Google Cloud Messaging-Building Blocks • Google account for application registration • Android Application • GCM Server GCM Server • Your Application Server
How GCM Works GCM Server 1.Registers Application with GCM and gets the API Key and Project ID 4. Sends Registration ID to the server Application Server Phone
1. Create Google API Project • Go to https://code.google.com/apis/console • Click on create project and this will generate a Project ID which will be used as GCM Sender ID.
2. Enable GCM Service • In the main Google APIs Console page, select Services . • Turn the Google Cloud Messaging to ON. • In the Terms of Service page, accept the In the Terms of Service page, accept the terms.
3. Obtain API Key • In the main Google APIs Console page, select API Access . • Click Create new Server key . Either a server key or a browser key should work .
4. Get Registration ID for the device import com.google.android.gcm.GCMRegistrar; @Override protected void onCreate(Bundle savedInstanceState) { if (GCMRegistrar.getRegistrationId(this).equals(“”)) { if (GCMRegistrar.getRegistrationId(this).equals(“”)) { GCMRegistrar.register(this, “12345678910”); }
5. Send Registration Id to server import com.google.android.gcm.GCMBaseIntentService; public class GCMIntentService extends GCMBaseIntentService { @Override protected void onRegistered(Context ctx, String regId) { protected void onRegistered(Context ctx, String regId) { sendToLiveScoreServer(regId); } }
6. Server sends messages to GCM import com.google.android.gcm.server.*; Sender sender = new Sender(myApiKey); Message message = new Message.Builder().build(); MulticastResult result = sender.send(message, devices, MulticastResult result = sender.send(message, devices, 5);
7.Declare Permissions <!-- App receives GCM messages. --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- GCM connects to Google Services. --> <uses-permission android:name="android.permission.INTERNET" /> <!-- GCM requires a Google account. --> <uses-permission android:name="android.permission.GET_ACCOUNTS" />
REG ID, API Key REG ID, API Key Live API Key Score GCM Auth Server Project ID Live Score App
Message Multicasting • One Message multiple devices • Allows up to 1000 devices simultaneously.
GCM Time To Live • From 0 to 4 weeks • Messages with TTL as 0 are not stored on GCM • Default value is 4 weeks. Default value is 4 weeks.
Summary • Many Android apps access data in cloud • Push keeps apps up to date, efficiently • Android Google Cloud Messaging makes push simple simple
Q UESTIONS ?
T HANK YOU
Recommend
More recommend