SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
Android Cloud
  to Device
 Messaging
 Framework
"It allows third-party application servers
to send lightweight messages to their
Android applications"
Not designed to send a lot of content via messages. Instead
use C2DM to tell the application that there is new data on the
server to fetch.
"C2DM makes no guarantees about
delivery or the order of messages"
Eg. not suitable for instant messaging, instead use C2DM to
notify the user that there is new messages.
"An application on an Android device
doesn’t need to be running to receive
messages"
Intent broadcast wake up the app
"It does not provide any built-in user
interface or other handling for message
data"
Passes raw message data. You are free to do whatever you
want with the data. Fire a notification, send SMS, start another
application etc.
"It requires devices running Android 2.2
or higher that also have the Market
application installed"
Market is needed for technical reasons. No need to distribute
via Market.
"It uses an existing connection for
Google services"
Requires the user to be logged in with a Google Account on the
device.
Establish connection
                   with conn servers



                                                   Send to conn
                               Send to             servers
                               device
                                                                  Stores message




                                                       App server sends
                                                       HTTP POST
                                                       Google




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 with registration ID from Google
  com.google.android.c2dm.intent.REGISTRATION
  App sends registration ID to app server
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);
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.
  }
}
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
<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>
<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>
Send message
From the documentation:
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.
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.
http://code.google.com/android/c2dm/index.html#lifecycle
Send message
   Retrieve ac2dm auth token (put on app server)
   Send authenticated POST
   - GoogleLogin auth=<TOKEN>
   - URL Encoded parameters
      registration_id
      collapse_key
      deplay_while_idle - optional
      data.<KEY> - optional


Using cURL to interact with Google Data services:

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

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

SID=DQAAA...
LSID=DQAAA...
Auth=DQAAA...
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);
}
Send message
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>"


Response codes
   200 OK
   - id=<MESSAGE ID> of sent message, success
   - Error=<ERROR CODE>, failed to send
   401 Not Authorized
   503 Service Unavailable, retry
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
delay_while_idle

  Message is not immediately sent to the device if it is idle
Receive a message
  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

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
  }
}
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);
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.
   }
}
Problems

 Can't send message to device if logged in with the role
 account
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/
Demo

http://screenr.com/QDn

http://screenr.com/ovn

Weitere Àhnliche Inhalte

Was ist angesagt?

Jasigsakai12 columbia-customizes-cas
Jasigsakai12 columbia-customizes-casJasigsakai12 columbia-customizes-cas
Jasigsakai12 columbia-customizes-casellentuck
 
An Introduction to OAuth2
An Introduction to OAuth2An Introduction to OAuth2
An Introduction to OAuth2Aaron Parecki
 
Oauth 2.0 security
Oauth 2.0 securityOauth 2.0 security
Oauth 2.0 securityvinoth kumar
 
Android securitybyexample
Android securitybyexampleAndroid securitybyexample
Android securitybyexamplePragati Rai
 
16 interacting with user data contacts and appointments
16   interacting with user data contacts and appointments16   interacting with user data contacts and appointments
16 interacting with user data contacts and appointmentsWindowsPhoneRocks
 
24032022 Zero Trust for Developers Pub.pdf
24032022 Zero Trust for Developers Pub.pdf24032022 Zero Trust for Developers Pub.pdf
24032022 Zero Trust for Developers Pub.pdfTomasz Kopacz
 
OAuth2 - Introduction
OAuth2 - IntroductionOAuth2 - Introduction
OAuth2 - IntroductionKnoldus Inc.
 
Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...
Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...
Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...Lviv Startup Club
 
OAuth2 and Spring Security
OAuth2 and Spring SecurityOAuth2 and Spring Security
OAuth2 and Spring SecurityOrest Ivasiv
 
Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...
Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...
Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...BizTalk360
 
Intro to API Security with Oauth 2.0
Intro to API Security with Oauth 2.0Intro to API Security with Oauth 2.0
Intro to API Security with Oauth 2.0Functional Imperative
 
EmberConf 2015 – Ambitious UX for Ambitious Apps
EmberConf 2015 – Ambitious UX for Ambitious AppsEmberConf 2015 – Ambitious UX for Ambitious Apps
EmberConf 2015 – Ambitious UX for Ambitious AppsLauren Elizabeth Tan
 
Securing the Web@VoxxedDays2017
Securing the Web@VoxxedDays2017Securing the Web@VoxxedDays2017
Securing the Web@VoxxedDays2017Sumanth Damarla
 
OpenSocial Intro
OpenSocial IntroOpenSocial Intro
OpenSocial IntroPamela Fox
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservicesAlvaro Sanchez-Mariscal
 
OAuth 2.0 - The fundamentals, the good , the bad, technical primer and commo...
OAuth 2.0  - The fundamentals, the good , the bad, technical primer and commo...OAuth 2.0  - The fundamentals, the good , the bad, technical primer and commo...
OAuth 2.0 - The fundamentals, the good , the bad, technical primer and commo...Good Dog Labs, Inc.
 

Was ist angesagt? (20)

Android+ax+app+wcf
Android+ax+app+wcfAndroid+ax+app+wcf
Android+ax+app+wcf
 
Jasigsakai12 columbia-customizes-cas
Jasigsakai12 columbia-customizes-casJasigsakai12 columbia-customizes-cas
Jasigsakai12 columbia-customizes-cas
 
Android ax app wcf
Android ax app wcfAndroid ax app wcf
Android ax app wcf
 
An Introduction to OAuth2
An Introduction to OAuth2An Introduction to OAuth2
An Introduction to OAuth2
 
OAuth 2 Presentation
OAuth 2 PresentationOAuth 2 Presentation
OAuth 2 Presentation
 
Oauth 2.0 security
Oauth 2.0 securityOauth 2.0 security
Oauth 2.0 security
 
Android securitybyexample
Android securitybyexampleAndroid securitybyexample
Android securitybyexample
 
16 interacting with user data contacts and appointments
16   interacting with user data contacts and appointments16   interacting with user data contacts and appointments
16 interacting with user data contacts and appointments
 
24032022 Zero Trust for Developers Pub.pdf
24032022 Zero Trust for Developers Pub.pdf24032022 Zero Trust for Developers Pub.pdf
24032022 Zero Trust for Developers Pub.pdf
 
OAuth2 - Introduction
OAuth2 - IntroductionOAuth2 - Introduction
OAuth2 - Introduction
 
Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...
Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...
Lviv MDDay 2014. ХДргіĐč ĐšĐŸĐŒĐ»Đ°Ń‡ “ВоĐșĐŸŃ€ĐžŃŃ‚Đ°ĐœĐœŃ accessibility api ĐŽĐ»Ń ĐŽĐŸŃŃ‚ŃƒĐżŃƒ ĐŽĐŸ...
 
OAuth2 and Spring Security
OAuth2 and Spring SecurityOAuth2 and Spring Security
OAuth2 and Spring Security
 
Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...
Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...
Use Windows Azure Service Bus, BizTalk Services, Mobile Services, and BizTalk...
 
OAuth 2
OAuth 2OAuth 2
OAuth 2
 
Intro to API Security with Oauth 2.0
Intro to API Security with Oauth 2.0Intro to API Security with Oauth 2.0
Intro to API Security with Oauth 2.0
 
EmberConf 2015 – Ambitious UX for Ambitious Apps
EmberConf 2015 – Ambitious UX for Ambitious AppsEmberConf 2015 – Ambitious UX for Ambitious Apps
EmberConf 2015 – Ambitious UX for Ambitious Apps
 
Securing the Web@VoxxedDays2017
Securing the Web@VoxxedDays2017Securing the Web@VoxxedDays2017
Securing the Web@VoxxedDays2017
 
OpenSocial Intro
OpenSocial IntroOpenSocial Intro
OpenSocial Intro
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservices
 
OAuth 2.0 - The fundamentals, the good , the bad, technical primer and commo...
OAuth 2.0  - The fundamentals, the good , the bad, technical primer and commo...OAuth 2.0  - The fundamentals, the good , the bad, technical primer and commo...
OAuth 2.0 - The fundamentals, the good , the bad, technical primer and commo...
 

Ähnlich wie Android Cloud to Device Messaging Framework at GTUG Stockholm

Android chat in the cloud
Android chat in the cloudAndroid chat in the cloud
Android chat in the cloudfirenze-gtug
 
Android Cloud To Device Messaging
Android Cloud To Device MessagingAndroid Cloud To Device Messaging
Android Cloud To Device MessagingFernando Cejas
 
Android cloud to device messaging
Android cloud to device messagingAndroid cloud to device messaging
Android cloud to device messagingFe
 
AC2DM For Security
AC2DM For SecurityAC2DM For Security
AC2DM For SecurityJason Ross
 
GCM aperitivo Android
GCM aperitivo AndroidGCM aperitivo Android
GCM aperitivo AndroidLuca Morettoni
 
Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...
Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...
Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...99X Technology
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Zeeshan Rahman
 
Urban Airship & Android Application Integration Document
Urban Airship & Android Application Integration DocumentUrban Airship & Android Application Integration Document
Urban Airship & Android Application Integration Documentmobi fly
 
Android app development basics
Android app development basicsAndroid app development basics
Android app development basicsAnton Narusberg
 
Google Cloud Messaging
Google Cloud MessagingGoogle Cloud Messaging
Google Cloud MessagingAshiq Uz Zoha
 
Push Notification in IBM MobileFirst Xamarin SDK
Push Notification in IBM MobileFirst Xamarin SDKPush Notification in IBM MobileFirst Xamarin SDK
Push Notification in IBM MobileFirst Xamarin SDKAjay Chebbi
 
Android broadcast receiver tutorial
Android broadcast receiver  tutorialAndroid broadcast receiver  tutorial
Android broadcast receiver tutorialmaamir farooq
 
Android broadcast receiver tutorial
Android broadcast receiver   tutorialAndroid broadcast receiver   tutorial
Android broadcast receiver tutorialmaamir farooq
 
Androidćș”ç”šćŒ€ć‘çź€ä»‹
Androidćș”ç”šćŒ€ć‘çź€ä»‹Androidćș”ç”šćŒ€ć‘çź€ä»‹
Androidćș”ç”šćŒ€ć‘çź€ä»‹easychen
 
Gcm presentation
Gcm presentationGcm presentation
Gcm presentationNiraj Singh
 
MongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDBMongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDBMongoDB
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxxMAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx34ShreyaChauhan
 
Cross-Platform Authentication with Google+ Sign-In
Cross-Platform Authentication with Google+ Sign-InCross-Platform Authentication with Google+ Sign-In
Cross-Platform Authentication with Google+ Sign-InPeter Friese
 

Ähnlich wie Android Cloud to Device Messaging Framework at GTUG Stockholm (20)

Android chat in the cloud
Android chat in the cloudAndroid chat in the cloud
Android chat in the cloud
 
Workshop: Android
Workshop: AndroidWorkshop: Android
Workshop: Android
 
Android Cloud To Device Messaging
Android Cloud To Device MessagingAndroid Cloud To Device Messaging
Android Cloud To Device Messaging
 
Android cloud to device messaging
Android cloud to device messagingAndroid cloud to device messaging
Android cloud to device messaging
 
AC2DM For Security
AC2DM For SecurityAC2DM For Security
AC2DM For Security
 
Android Froyo
Android FroyoAndroid Froyo
Android Froyo
 
GCM aperitivo Android
GCM aperitivo AndroidGCM aperitivo Android
GCM aperitivo Android
 
Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...
Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...
Colombo Mobile Developer MeetUp - Building Scalable Cloud Connected Mobile Ap...
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...
 
Urban Airship & Android Application Integration Document
Urban Airship & Android Application Integration DocumentUrban Airship & Android Application Integration Document
Urban Airship & Android Application Integration Document
 
Android app development basics
Android app development basicsAndroid app development basics
Android app development basics
 
Google Cloud Messaging
Google Cloud MessagingGoogle Cloud Messaging
Google Cloud Messaging
 
Push Notification in IBM MobileFirst Xamarin SDK
Push Notification in IBM MobileFirst Xamarin SDKPush Notification in IBM MobileFirst Xamarin SDK
Push Notification in IBM MobileFirst Xamarin SDK
 
Android broadcast receiver tutorial
Android broadcast receiver  tutorialAndroid broadcast receiver  tutorial
Android broadcast receiver tutorial
 
Android broadcast receiver tutorial
Android broadcast receiver   tutorialAndroid broadcast receiver   tutorial
Android broadcast receiver tutorial
 
Androidćș”ç”šćŒ€ć‘çź€ä»‹
Androidćș”ç”šćŒ€ć‘çź€ä»‹Androidćș”ç”šćŒ€ć‘çź€ä»‹
Androidćș”ç”šćŒ€ć‘çź€ä»‹
 
Gcm presentation
Gcm presentationGcm presentation
Gcm presentation
 
MongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDBMongoDB.local Atlanta: Introduction to Serverless MongoDB
MongoDB.local Atlanta: Introduction to Serverless MongoDB
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxxMAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
 
Cross-Platform Authentication with Google+ Sign-In
Cross-Platform Authentication with Google+ Sign-InCross-Platform Authentication with Google+ Sign-In
Cross-Platform Authentication with Google+ Sign-In
 

Mehr von Johan Nilsson

Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...
Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...
Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...Johan Nilsson
 
GTFS & OSM in STHLM Traveling at Trafiklab
GTFS & OSM in STHLM Traveling at Trafiklab GTFS & OSM in STHLM Traveling at Trafiklab
GTFS & OSM in STHLM Traveling at Trafiklab Johan Nilsson
 
STHLM Traveling Trafiklab
STHLM Traveling TrafiklabSTHLM Traveling Trafiklab
STHLM Traveling TrafiklabJohan Nilsson
 
JavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & BrowserifyJavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & BrowserifyJohan Nilsson
 
Spacebrew & Arduino YĂșn
Spacebrew & Arduino YĂșnSpacebrew & Arduino YĂșn
Spacebrew & Arduino YĂșnJohan Nilsson
 
Trafiklab 1206
Trafiklab 1206Trafiklab 1206
Trafiklab 1206Johan Nilsson
 
Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Johan Nilsson
 
new Android UI Patterns
new Android UI Patternsnew Android UI Patterns
new Android UI PatternsJohan Nilsson
 
Android swedroid
Android swedroidAndroid swedroid
Android swedroidJohan Nilsson
 
GTUG Android iglaset Presentation 1 Oct
GTUG Android iglaset Presentation 1 OctGTUG Android iglaset Presentation 1 Oct
GTUG Android iglaset Presentation 1 OctJohan Nilsson
 

Mehr von Johan Nilsson (10)

Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...
Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...
Utmaningar som tredjepartsutvecklare för kollektivtrafikbranchen - Kollektivt...
 
GTFS & OSM in STHLM Traveling at Trafiklab
GTFS & OSM in STHLM Traveling at Trafiklab GTFS & OSM in STHLM Traveling at Trafiklab
GTFS & OSM in STHLM Traveling at Trafiklab
 
STHLM Traveling Trafiklab
STHLM Traveling TrafiklabSTHLM Traveling Trafiklab
STHLM Traveling Trafiklab
 
JavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & BrowserifyJavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & Browserify
 
Spacebrew & Arduino YĂșn
Spacebrew & Arduino YĂșnSpacebrew & Arduino YĂșn
Spacebrew & Arduino YĂșn
 
Trafiklab 1206
Trafiklab 1206Trafiklab 1206
Trafiklab 1206
 
Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011
 
new Android UI Patterns
new Android UI Patternsnew Android UI Patterns
new Android UI Patterns
 
Android swedroid
Android swedroidAndroid swedroid
Android swedroid
 
GTUG Android iglaset Presentation 1 Oct
GTUG Android iglaset Presentation 1 OctGTUG Android iglaset Presentation 1 Oct
GTUG Android iglaset Presentation 1 Oct
 

KĂŒrzlich hochgeladen

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂșjo
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

KĂŒrzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Android Cloud to Device Messaging Framework at GTUG Stockholm

  • 1. Android Cloud to Device Messaging Framework
  • 2.
  • 3. "It allows third-party application servers to send lightweight messages to their Android applications" Not designed to send a lot of content via messages. Instead use C2DM to tell the application that there is new data on the server to fetch.
  • 4. "C2DM makes no guarantees about delivery or the order of messages" Eg. not suitable for instant messaging, instead use C2DM to notify the user that there is new messages.
  • 5. "An application on an Android device doesn’t need to be running to receive messages" Intent broadcast wake up the app
  • 6. "It does not provide any built-in user interface or other handling for message data" Passes raw message data. You are free to do whatever you want with the data. Fire a notification, send SMS, start another application etc.
  • 7. "It requires devices running Android 2.2 or higher that also have the Market application installed" Market is needed for technical reasons. No need to distribute via Market.
  • 8. "It uses an existing connection for Google services" Requires the user to be logged in with a Google Account on the device.
  • 9. Establish connection with conn servers Send to conn Send to servers device Stores message App server sends HTTP POST Google http://code.google.com/events/io/2010/sessions/push-applications-android.html
  • 10. Lifecycle Register device App server send message Device receives message Unregister device
  • 11. 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
  • 12. 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);
  • 13. 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. } }
  • 14. 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
  • 15. <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>
  • 16. <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>
  • 17. Send message From the documentation: 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. 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. http://code.google.com/android/c2dm/index.html#lifecycle
  • 18. Send message Retrieve ac2dm auth token (put on app server) Send authenticated POST - GoogleLogin auth=<TOKEN> - URL Encoded parameters registration_id collapse_key deplay_while_idle - optional data.<KEY> - optional Using cURL to interact with Google Data services: http://code.google.com/apis/gdata/articles/using_cURL.html
  • 19. ac2dm authorization token 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 SID=DQAAA... LSID=DQAAA... Auth=DQAAA...
  • 20. 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); }
  • 21. Send message 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>" Response codes 200 OK - id=<MESSAGE ID> of sent message, success - Error=<ERROR CODE>, failed to send 401 Not Authorized 503 Service Unavailable, retry
  • 22. 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
  • 23. delay_while_idle Message is not immediately sent to the device if it is idle
  • 24. Receive a message 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 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 } }
  • 26. 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. } }
  • 27. Problems Can't send message to device if logged in with the role account
  • 28. 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/