Android Cloud
to Device
Messaging
Framework
How to keep data fresh?
Polling
Simple to implement
Ask for data periodically
Radio draws a lot of power
Pushing
Harder to implement
Less battery consumption
Only uses network when needed
Constant connection
Components
Intent
Description of an action to perform
BroadcastReceiver
Recieve intents
IntentService
Handles asynchronous...
Some interesting projects
CouchOne Mobile for Android
http://www.couch.io/android
Ericsson Labs - Mobile Push
https://labs...
"It allows third-party
application servers to
send lightweight messages
to their Android
applications"
"C2DM makes no
guarantees about delivery
or the order of messages"
"An application on an
Android device doesn’t
need to be running to
receive messages"
"It does not provide any
built-in user interface or
other handling for
message data"
"It requires devices
running Android 2.2 or
higher that also have the
Market application
installed"
"It uses an existing
connection for Google
services"
http://code.google.com/events/io/2010/sessions/push-applications-android.html
Lifecycle
Register device
App server send message
Device receives message
Unregister device
Register device
App fires off Intent to register with Google
com.google.android.c2dm.intent.REGISTER
App receives Intent w...
Register device
// Intent to register
Intent regIntent = new
Intent("com.google.android.c2dm.intent.REGISTER");
// Identif...
Register device
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("...REGISTRATION")) ...
Unregister device
App fires off a unregister Intent to register with Google com.
google.android.c2dm.intent.UNREGISTER
App...
<manifest ...
<application ...
<service android:name=".C2DMReceiver" />
<receiver android:name="com.google.android.c2dm.C2...
<manifest ...
<application ...
<service android:name=".C2DMReceiver" />
<receiver android:name="com.google.android.c2dm.C2...
Send Message
Send message
For an application server to send a message, the following
things must be in place:
The application has a reg...
Send message
Retrieve ac2dm auth token (put on app server)
Send authenticated POST
- GoogleLogin auth=<TOKEN>
- URL Encode...
curl https://www.google.com/accounts/ClientLogin 
-d Email=<ACCOUNT_EMAIL> -d Passwd=<PASSWORD> 
-d accountType=HOSTED_OR_...
Token can change
URL url = new URL(serverConfig.getC2DMUrl());
HttpURLConnection conn =
(HttpURLConnection) url.openConnec...
curl https://android.apis.google.com/c2dm/send 
-d registration_id=<REGISTRATION ID> 
-d collapse_key=foo 
-d data.key1=ba...
collapse_key
Only the latest message with the same key will be delivered
Avoids multiple messages of the same type to be d...
delay_while_idle
Message is not immediately sent to the device if it is idle
Receive a message
Receive a message
curl https://android.apis.google.com/c2dm/send 
...
-d data.key1=bar 
...
protected void onReceive(Conte...
Chrome to phone
http://code.google.
com/p/chrometophone/source/browse/#svn/trunk/android/c2dm
/com/google/android/c2dm
C2D...
public class C2DMReceiver extends C2DMBaseReceiver {
public C2DMReceiver() {
super("Role account email");
}
@Override
publ...
Problems
Can't send message to device if logged in with the role
account
401
References
http://code.google.com/android/c2dm/
http://code.google.com/events/io/2010/sessions/push-
applications-android....
Demo
http://screenr.com/QDn - http://screenr.com/ovn
FOSS STHLM Android Cloud to Device Messaging
FOSS STHLM Android Cloud to Device Messaging
FOSS STHLM Android Cloud to Device Messaging
FOSS STHLM Android Cloud to Device Messaging
FOSS STHLM Android Cloud to Device Messaging
FOSS STHLM Android Cloud to Device Messaging
FOSS STHLM Android Cloud to Device Messaging
Upcoming SlideShare
Loading in...5
×

FOSS STHLM Android Cloud to Device Messaging

2,985

Published on

foss-sthlm #4 meeting, 26 October.

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,985
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
79
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

FOSS STHLM Android Cloud to Device Messaging

  1. 1. Android Cloud to Device Messaging Framework
  2. 2. How to keep data fresh?
  3. 3. Polling Simple to implement Ask for data periodically Radio draws a lot of power
  4. 4. Pushing Harder to implement Less battery consumption Only uses network when needed Constant connection
  5. 5. Components Intent Description of an action to perform BroadcastReceiver Recieve intents IntentService Handles asynchronous request, started when needed WakeLock Wake up the device at desired level AlarmManager Schedule the app to run in the future Synchronize polls with INTERVAL_*
  6. 6. Some interesting projects CouchOne Mobile for Android http://www.couch.io/android Ericsson Labs - Mobile Push https://labs.ericsson.com/apis/mobile-push/ Whoopingkof - Evented Sockets http://github.com/voltron/whoopingkof/
  7. 7. "It allows third-party application servers to send lightweight messages to their Android applications"
  8. 8. "C2DM makes no guarantees about delivery or the order of messages"
  9. 9. "An application on an Android device doesn’t need to be running to receive messages"
  10. 10. "It does not provide any built-in user interface or other handling for message data"
  11. 11. "It requires devices running Android 2.2 or higher that also have the Market application installed"
  12. 12. "It uses an existing connection for Google services"
  13. 13. http://code.google.com/events/io/2010/sessions/push-applications-android.html
  14. 14. Lifecycle Register device App server send message Device receives message Unregister device
  15. 15. Register device App fires off Intent to register with Google com.google.android.c2dm.intent.REGISTER App receives Intent with registration ID from Google com.google.android.c2dm.intent.REGISTRATION App sends registration ID to app server
  16. 16. 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 sending regIntent.putExtra("sender", senderEmail); // Start registration process context.startService(regIntent);
  17. 17. Register device public 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. } }
  18. 18. Unregister device App fires off a unregister Intent to register with Google com. google.android.c2dm.intent.UNREGISTER App tells app server to remove the stored registration ID
  19. 19. <manifest ... <application ... <service android:name=".C2DMReceiver" /> <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiv <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.REGISTRATI <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. android:protectionLevel="signature" /> <uses-permission android:name="com.markupartist.sthlmtraveling.permis C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.REC <uses-permission android:name="android.permission.INTERNET" /> </manifest>
  20. 20. <manifest ... <application ... <service android:name=".C2DMReceiver" /> <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceive <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.REGISTRATIO <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. android:protectionLevel="signature" /> <uses-permission android:name=" com.markupartist.sthlmtraveling .permis C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECE <uses-permission android:name="android.permission.INTERNET" /> </manifest>
  21. 21. Send Message
  22. 22. Send message For 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:
  23. 23. Send message Retrieve ac2dm auth token (put on app server) Send authenticated POST - GoogleLogin auth=<TOKEN> - URL Encoded parameters registration_id collapse_key delay_while_idle - optional data.<KEY> - optional Using cURL to interact with Google Data services: http://code.google.com/apis/gdata/articles/using_cURL.html
  24. 24. 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 C2DM Authorizatio n token
  25. 25. Token can change URL url = new URL(serverConfig.getC2DMUrl()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); ... // Check for updated token header String updatedAuthToken = conn.getHeaderField("Update-Client-Auth"); if (updatedAuthToken != null && !authToken.equals(updatedAuthToken)) { serverConfig.updateToken(updatedAuthToken); }
  26. 26. 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 codes 200 OK - id=<MESSAGE ID> of sent message, success - Error=<ERROR CODE>, failed to send 401 Not Authorized 503 Service Unavailable, retry
  27. 27. collapse_key Only the latest message with the same key will be delivered Avoids multiple messages of the same type to be delivered to an offline device State should be in app server and not in the message Eg. could be the URL to fetch data from
  28. 28. delay_while_idle Message is not immediately sent to the device if it is idle
  29. 29. Receive a message
  30. 30. 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.RECEIVE App wakes up to handle Intent - data.<KEY> is translated to extras
  31. 31. Chrome to phone http://code.google. com/p/chrometophone/source/browse/#svn/trunk/android/c2dm /com/google/android/c2dm C2DMessaging.register(context, "Role account email"); C2DMessaging.unregister(context);
  32. 32. 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. } }
  33. 33. Problems Can't send message to device if logged in with the role account 401
  34. 34. References http://code.google.com/android/c2dm/ http://code.google.com/events/io/2010/sessions/push- applications-android.html http://code.google.com/p/chrometophone/
  35. 35. Demo http://screenr.com/QDn - http://screenr.com/ovn
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×