42
Android Cloud to Device Messaging Framework

FOSS STHLM Android Cloud to Device Messaging

Embed Size (px)

DESCRIPTION

foss-sthlm #4 meeting, 26 October.

Citation preview

Page 1: FOSS STHLM Android Cloud to Device Messaging

Android Cloud to Device

Messaging Framework

Page 2: FOSS STHLM Android Cloud to Device Messaging
Page 3: FOSS STHLM Android Cloud to Device Messaging
Page 5: FOSS STHLM Android Cloud to Device Messaging

How to keep data fresh?

Page 6: FOSS STHLM Android Cloud to Device Messaging
Page 7: FOSS STHLM Android Cloud to Device Messaging

PollingSimple to implementAsk for data periodicallyRadio draws a lot of power

Page 8: FOSS STHLM Android Cloud to Device Messaging
Page 9: FOSS STHLM Android Cloud to Device Messaging

PushingHarder to implementLess battery consumption

Only uses network when neededConstant connection

Page 10: FOSS STHLM Android Cloud to Device Messaging

ComponentsIntent

Description of an action to performBroadcastReceiver

Recieve intentsIntentService

Handles asynchronous request, started when neededWakeLock

Wake up the device at desired levelAlarmManager

Schedule the app to run in the futureSynchronize polls with INTERVAL_*

Page 11: FOSS STHLM Android Cloud to Device Messaging

Some interesting projectsCouchOne Mobile for Androidhttp://www.couch.io/android

Ericsson Labs - Mobile Pushhttps://labs.ericsson.com/apis/mobile-push/

Whoopingkof - Evented Socketshttp://github.com/voltron/whoopingkof/

Page 12: FOSS STHLM Android Cloud to Device Messaging
Page 13: FOSS STHLM Android Cloud to Device Messaging

"It allows third-party application servers to send lightweight messages to their Android applications"

Page 14: FOSS STHLM Android Cloud to Device Messaging

"C2DM makes no guarantees about delivery or the order of messages"

Page 15: FOSS STHLM Android Cloud to Device Messaging

"An application on an Android device doesn’t need to be running to receive messages"

Page 16: FOSS STHLM Android Cloud to Device Messaging

"It does not provide any built-in user interface or other handling for message data"

Page 17: FOSS STHLM Android Cloud to Device Messaging

"It requires devices running Android 2.2 or higher that also have the Market application installed"

Page 18: FOSS STHLM Android Cloud to Device Messaging

"It uses an existing connection for Google services"

Page 19: FOSS STHLM Android Cloud to Device Messaging

http://code.google.com/events/io/2010/sessions/push-applications-android.html

Page 20: FOSS STHLM Android Cloud to Device Messaging

Lifecycle

Register deviceApp server send messageDevice receives messageUnregister device

Page 21: FOSS STHLM Android Cloud to Device Messaging
Page 22: FOSS STHLM Android Cloud to Device Messaging

Register deviceApp fires off Intent to register with Googlecom.google.android.c2dm.intent.REGISTER App receives Intent with registration ID from Googlecom.google.android.c2dm.intent.REGISTRATIONApp sends registration ID to app server

Page 23: FOSS STHLM Android Cloud to Device Messaging

Register device// Intent to register Intent regIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); // Identifies the app regIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0)); // Identifies the role account, same as used when sendingregIntent.putExtra("sender", senderEmail); // Start registration process context.startService(regIntent);

Page 24: FOSS STHLM Android Cloud to Device Messaging

Register devicepublic void onReceive(Context context, Intent intent) { if (intent.getAction().equals("...REGISTRATION")) { handleRegistration(context, intent); }}

private void handleRegistration(Context context, Intent intent) { String registration = intent.getStringExtra("registration_id"); if (intent.getStringExtra("error") != null) { // Registration failed, should try again later. } else if (intent.getStringExtra("unregistered") != null) { // unregistration done } else if (registration != null) { // Send the registration ID to app server // This should be done in a separate thread. // When done, remember that all registration is done. }}

Page 25: FOSS STHLM Android Cloud to Device Messaging

Unregister device

App fires off a unregister Intent to register with Google com.google.android.c2dm.intent.UNREGISTERApp tells app server to remove the stored registration ID

Page 26: FOSS STHLM Android Cloud to Device Messaging

<manifest ... <application ... <service android:name=".C2DMReceiver" /> <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.markupartist.sthlmtraveling" /> </intent-filter> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.markupartist.sthlmtraveling" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" /> <permission android:name="com.markupartist.sthlmtraveling.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.markupartist.sthlmtraveling.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /></manifest>

Page 27: FOSS STHLM Android Cloud to Device Messaging

<manifest ... <application ... <service android:name=".C2DMReceiver" /> <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name=" com.markupartist.sthlmtraveling " /> </intent-filter> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name=" com.markupartist.sthlmtraveling " /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" /> <permission android:name=" com.markupartist.sthlmtraveling .permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name=" com.markupartist.sthlmtraveling .permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /></manifest>

Page 28: FOSS STHLM Android Cloud to Device Messaging

Send Message

Page 29: FOSS STHLM Android Cloud to Device Messaging

Send messageFor an application server to send a message, the following things must be in place:

The application has a registration ID that allows it to receive messages for a particular device.The third-party application server has stored the registration ID.

http://code.google.com/android/c2dm/index.html#lifecycle

There is one more thing that needs to be in place for the application server to send messages: a ClientLogin authorization token. This is something that the developer must have already set up on the application server for the application (for more discussion, see Role of the Third-Party Application Server). Now it will get used to send messages to the device.

From the documentation:

Page 30: FOSS STHLM Android Cloud to Device Messaging

Send messageRetrieve ac2dm auth token (put on app server)Send authenticated POST - GoogleLogin auth=<TOKEN>- URL Encoded parameters

registration_idcollapse_keydelay_while_idle - optionaldata.<KEY> - optional

Using cURL to interact with Google Data services:

http://code.google.com/apis/gdata/articles/using_cURL.html

Page 31: FOSS STHLM Android Cloud to Device Messaging

curl https://www.google.com/accounts/ClientLogin \ -d Email=<ACCOUNT_EMAIL> -d Passwd=<PASSWORD> \ -d accountType=HOSTED_OR_GOOGLE \ -d source=markupartist-sthlmtraveling-1 \ -d service=ac2dm

ac2dm authorization token

SID=DQAAA...LSID=DQAAA...Auth=DQAAA...

Service name for

C2DMAuthorizatio

n token

Page 32: FOSS STHLM Android Cloud to Device Messaging

Token can changeURL url = new URL(serverConfig.getC2DMUrl()); HttpURLConnection conn = (HttpURLConnection) url.openConnection();...// Check for updated token headerString updatedAuthToken = conn.getHeaderField("Update-Client-Auth");if (updatedAuthToken != null && !authToken.equals(updatedAuthToken)) { serverConfig.updateToken(updatedAuthToken);}

Page 33: FOSS STHLM Android Cloud to Device Messaging

curl https://android.apis.google.com/c2dm/send \-d registration_id=<REGISTRATION ID> \-d collapse_key=foo \-d data.key1=bar \-d delay_while_idle=1 \-H "Authorization: GoogleLogin auth=<AUTH TOKEN>"

Send message

Response codes200 OK- id=<MESSAGE ID> of sent message, success- Error=<ERROR CODE>, failed to send401 Not Authorized503 Service Unavailable, retry

Page 34: FOSS STHLM Android Cloud to Device Messaging

collapse_keyOnly the latest message with the same key will be deliveredAvoids multiple messages of the same type to be delivered to an offline device State should be in app server and not in the messageEg. could be the URL to fetch data from

Page 35: FOSS STHLM Android Cloud to Device Messaging

delay_while_idleMessage is not immediately sent to the device if it is idle

Page 36: FOSS STHLM Android Cloud to Device Messaging

Receive a message

Page 37: FOSS STHLM Android Cloud to Device Messaging

Receive a message

curl https://android.apis.google.com/c2dm/send \...-d data.key1=bar \...

protected void onReceive(Context context, Intent intent) { if (intent.getAction().equals("...RECEIVE")) { String key1 = intent.getExtras().getString("key1"); // If starting another intent here remember to set the // flag FLAG_ACTIVITY_NEW_TASK }}

Device receives the message and converts it to Intent - com.google.android.c2dm.intent.RECEIVEApp wakes up to handle Intent- data.<KEY> is translated to extras

Page 38: FOSS STHLM Android Cloud to Device Messaging

Chrome to phonehttp://code.google.com/p/chrometophone/source/browse/#svn/trunk/android/c2dm/com/google/android/c2dmC2DMessaging.register(context, "Role account email");

C2DMessaging.unregister(context);

Page 39: FOSS STHLM Android Cloud to Device Messaging

public class C2DMReceiver extends C2DMBaseReceiver { public C2DMReceiver() { super("Role account email"); } @Override public void onRegistrered(Context context, String registration) { // Handle registrations } @Override public void onUnregistered(Context context) { // Handle unregistered } @Override public void onError(Context context, String errorId) { // Handle errors } @Override public void onMessage(Context context, Intent intent) { // Handle received message. }}

Page 40: FOSS STHLM Android Cloud to Device Messaging

ProblemsCan't send message to device if logged in with the role account 401

Page 41: FOSS STHLM Android Cloud to Device Messaging

References

http://code.google.com/android/c2dm/ http://code.google.com/events/io/2010/sessions/push-applications-android.htmlhttp://code.google.com/p/chrometophone/

Page 42: FOSS STHLM Android Cloud to Device Messaging

Demo

http://screenr.com/QDn - http://screenr.com/ovn