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. PUSH
Android Application server
Galapagos Inc. http://www.glpgs.com/
5. Target
GoogleAccount(device) × Application
PUSH(hoge, rearge)
hoge@gmail.com
PUSH(fuga, rearge)
Registration ID
fuga@gmail.com
Galapagos Inc. http://www.glpgs.com/
6. Flow overview
1. New data
5. Request data
6. Receive data
4. Event 2. Send
(RegistrationID)
3. PUSH Notification
Galapagos Inc. http://www.glpgs.com/ C2DM Servers
7. Flow overview
1. New data
5. Request data
6. Receive data
4. Event 2. Send
(RegistrationID)
3. PUSH Notification
Don’t care! C2DM
Service
Galapagos Inc. http://www.glpgs.com/
8. Without C2DM
• WebSocket
• Comet
• Polling
• Other(Google Channel API...)
Galapagos Inc. http://www.glpgs.com/
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 management
Galapagos Inc. http://www.glpgs.com/
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 Service
Galapagos Inc. http://www.glpgs.com/
11. Other feature
• Designed for lightweight message
• No gurantee for message sequence
Galapagos Inc. http://www.glpgs.com/
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/
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 ID
Galapagos Inc. http://www.glpgs.com/
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. 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 ID
Galapagos Inc. http://www.glpgs.com/
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/
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 permission
Galapagos Inc. http://www.glpgs.com/
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. 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. AuthToken
• Authentication token required to use
Goolge API.
• http://code.google.com/intl/ja/apis/acc
ounts/docs/AuthForInstalledApps.html
Galapagos Inc. http://www.glpgs.com/
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
end
Galapagos Inc. http://www.glpgs.com/
39. Other
• Spec
• http://code.google.com/intl/ja/android/c2dm/
• Memo about server-side
• http://d.hatena.ne.jp/hosopy/20111030
Galapagos Inc. http://www.glpgs.com/