Developing android c2_dm_applications(android c2dm開発に関して)

1,664 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,664
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Developing android c2_dm_applications(android c2dm開発に関して)

  1. 1. Android C2DM開発に関して Developing Android C2DM applications. Overview and implementation. 2011/10 Keishi Hosoba @hosopy 株式会社ガラパゴス
  2. 2. Overview
  3. 3. What is C2DM? • Cloud to Device Messaging • PUSH notification framework and service for android applications • It allows third-party application servers to send lightweight messages to their Android applications.Galapagos Inc. http://www.glpgs.com/
  4. 4. PUSH Android Application serverGalapagos Inc. http://www.glpgs.com/
  5. 5. Target GoogleAccount(device) × Application PUSH(hoge, rearge) hoge@gmail.com PUSH(fuga, rearge) Registration ID fuga@gmail.comGalapagos Inc. http://www.glpgs.com/
  6. 6. Flow overview 1. New data 5. Request data 6. Receive data 4. Event 2. Send (RegistrationID) 3. PUSH NotificationGalapagos Inc. http://www.glpgs.com/ C2DM Servers
  7. 7. Flow overview 1. New data 5. Request data 6. Receive data 4. Event 2. Send (RegistrationID) 3. PUSH Notification Don’t care! C2DM ServiceGalapagos Inc. http://www.glpgs.com/
  8. 8. Without C2DM • WebSocket • Comet • Polling • Other(Google Channel API...)Galapagos Inc. http://www.glpgs.com/
  9. 9. Many things to care • Designed for realtime web application • WebSocket,Comet • Infrastructure • server and network • Message queueing • if target device is offline ... • Security • Connection managementGalapagos Inc. http://www.glpgs.com/
  10. 10. Flow overview 1. New data 5. Request data 6. Receive data 4. Event 2. Send (RegistrationID) Don’t care! 3. PUSH Notification Infrastructure / Queueing / SecurityC2DM / Connection Management ServiceGalapagos Inc. http://www.glpgs.com/
  11. 11. Other feature • Designed for lightweight message • No gurantee for message sequenceGalapagos Inc. http://www.glpgs.com/
  12. 12. Detail Flow
  13. 13. Lifecycle Flow • Enabling C2DM • Sending a message • Receiving a messageGalapagos Inc. http://www.glpgs.com/
  14. 14. 1. Enabling C2DM Application AndroidOS C2DM Server Server Intent com.google.android.c2dm.int ent.REGISTER ? data[Sender ID] Registration ID Broadcast Intent REGISTRATION With permission data[Registration ID] hoge : RegistrationID Registration IDGalapagos Inc. http://www.glpgs.com/
  15. 15. 2,3. Sending & Receiving a message Application AndroidOS C2DM Server Server Message BroadcatReceiver Registration ID AuthToken Message RegistrationID Broadcast Intent Queueing hoge : com.google.android.c2dm.int RegistrationID ent.RECEIVE data[Message] With permissionGalapagos Inc. http://www.glpgs.com/
  16. 16. Implementation
  17. 17. Requirements • Android Application • OS 2.2 • Application Server • SSL enabledGalapagos Inc. http://www.glpgs.com/
  18. 18. Sign Upas C2DM Sender
  19. 19. http://code.google.com/intl/ja/android/c2dm/signup.htmlGalapagos Inc. http://www.glpgs.com/
  20. 20. Manifest
  21. 21. SDK & Permission <!-- 他のアプリのメッセージ登録・受信防止 --> <permission android:name="com.glpgs.android.study.c2dm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.glpgs.android.study.c2dm.permission.C2D_MESSAGE" /> <!-- その他必要なパーミッション --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" />Galapagos Inc. http://www.glpgs.com/
  22. 22. BroadcastReceiver <application android:icon="@drawable/icon" android:label="@string/app_name"> <!-- Only C2DM servers can send messages for the app. --> <!-- If permission is not set - any other app can generate it --> <receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <!-- Receive the actual message --> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.glpgs.android.study.c2dm" /> </intent-filter> <!-- Receive the registration id --> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.glpgs.android.study.c2dm" /> </intent-filter> </receiver>Galapagos Inc. http://www.glpgs.com/
  23. 23. ImplementEnabling C2DM Flow
  24. 24. 1. Enabling C2DM Application AndroidOS C2DM Server Server Intent com.google.android.c2d m.intent.REGISTER ? data[Sender ID] Registration ID Broadcast Intent hoge : REGISTRATION With permission RegistrationI data[Registration ID] D Registration IDGalapagos Inc. http://www.glpgs.com/
  25. 25. Register, Unregister OnClick private void registerC2DM(){ Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER"); intent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); intent.putExtra("sender", "hoge@example.com"); startService(intent); } Sender ID private void unregisterC2DM(){ Intent intent = new Intent("com.google.android.c2dm.intent.UNREGISTER"); intent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); startService(intent); }Galapagos Inc. http://www.glpgs.com/
  26. 26. 1. Enabling C2DM Application AndroidOS C2DM Server Server Intent com.google.android.c2dm.int ent.REGISTER ? data[Sender ID] Registration ID Broadcast Intent REGISTRATION With permission data[Registration ID] hoge : RegistrationID Registration IDGalapagos Inc. http://www.glpgs.com/
  27. 27. Receive Intent public class C2DMReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( "com.google.android.c2dm.intent.REGISTRATION")) { // Registration IDの発行が完了した handleRegistration(context, intent); } }Galapagos Inc. http://www.glpgs.com/
  28. 28. Retrieve RegistrationID private void handleRegistration(Context context, Intent intent) { String registration = intent.getStringExtra("registration_id"); if (intent.getStringExtra("error") != null) { // エラーだよ, リトライとか検討しましょう } else if (intent.getStringExtra("unregistered") != null) { // 登録解除が完了したよ } else if (registration != null) { // Registration IDの発行が完了したので、保存して自分のサーバに登録します // ここからは各アプリの仕様に応じてお好きに saveRegistrationId(registration); postRegistrationId(registration); } }Galapagos Inc. http://www.glpgs.com/
  29. 29. ImplementSending message Flow
  30. 30. 2,3. Sending & Receiving a message Application AndroidOS C2DM Server Server Message BroadcatReceiver Registration ID AuthToken Message RegistrationID Broadcast Intent Queueing hoge : com.google.android.c2dm.int RegistrationID ent.RECEIVE data[Message] With permissionGalapagos Inc. http://www.glpgs.com/
  31. 31. [API]POST:https://android.apis.google.com/c2dm/send Field Content registration_id Registration ID An arbitrary string that is used to collapse a group of like messages collapse_key when the device is offline, so that only the last message gets sent to the client. Message content (key-value pair) data.<key> 1024 bytes limit If included, indicates that the message should not be sent delay_while_idle immediately if the device is idle. Authorization: GoogleLogin Header with a ClientLogin Auth token. The cookie must be associated auth=[AUTH_TOKEN] with the ac2dm service.Galapagos Inc. http://www.glpgs.com/
  32. 32. collapse_key message ckey=hoge message ckey=hoge message ckey=hoge Only the last message is sent to the device when the device is online.Galapagos Inc. http://www.glpgs.com/
  33. 33. AuthToken • Authentication token required to use Goolge API. • http://code.google.com/intl/ja/apis/acc ounts/docs/AuthForInstalledApps.htmlGalapagos Inc. http://www.glpgs.com/
  34. 34. Send message @Server def send_c2dm(message) if auth_token = get_auth_token uri = URI.parse("http://android.apis.google.com/c2dm/send") body = URI.encode_www_form({ "registration_id" => message.registration_id, "collapse_key" => rand(100000).to_s, "data.content" => message.content}) header = { Content-Type => "application/x-www-form-urlencoded;charset=UTF-8", Content-Length => body.bytesize.to_s, Authorization => "GoogleLogin auth=#{auth_token}"} Net::HTTP.start(uri.host, uri.port) do |http| http.post(uri.path, body, header) end end endGalapagos Inc. http://www.glpgs.com/
  35. 35. ImplementReceiving message Flow
  36. 36. 2,3. Sending & Receiving a message Application AndroidOS C2DM Server Server Message BroadcatReceiver Registration ID AuthToken Message RegistrationID Broadcast Intent Queueing hoge : com.google.android.c2dm.int RegistrationID ent.RECEIVE data[Message] With permissionGalapagos Inc. http://www.glpgs.com/
  37. 37. Receive Intent public class C2DMReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( "com.google.android.c2dm.intent.REGISTRATION")) { Log.d("C2DMReceiver", "REGISTRATION"); // Registration IDの発行が完了した handleRegistration(context, intent); } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { // Messageを受信した Log.d("C2DMReceiver", "RECEIVE"); handleMessage(context, intent); } }Galapagos Inc. http://www.glpgs.com/
  38. 38. Demo
  39. 39. Other • Spec • http://code.google.com/intl/ja/android/c2dm/ • Memo about server-side • http://d.hatena.ne.jp/hosopy/20111030Galapagos Inc. http://www.glpgs.com/
  40. 40. PREntrust Galapagos your C2DM enabled Android applications development.C2DMを使ったアプリの開発承ります! http://www.glpgs.com
  41. 41. ENDGalapagos Inc. http://www.glpgs.com/

×