Sei sulla pagina 1di 63

Cloud Messaging in Android

Francesco Nerieri
Engineering Manager
Android Cloud To Device Messaging
2
News
Server
C2DM
News
App
#1 #2
Android Cloud To Device Messaging
3
News
App
News
Server
C2DM
#3
Android Cloud To Device Messaging
4
G
oogle A
ccount
Client Login Token
S
i
g
n

u
p

f
o
r
m
A
c
tiv
a
tio
n
E
m
a
il
I
n
i
t
i
a
l

Q
u
o
t
a
Request More Quota
Powerful Free Simple No Client Login Token No Sign Up Form No Quota Simple Powerful Message Multicasting Fast Expiring Messages 95ms Global Average Latency Messages With Payload Fast 4.7 milliseconds
5
6
Football
App
GCM - Registration
7
GCM
RegID
RegID
RegID
Football
Server
Football
App
Football
Server
GCM - Unregistration
8
GCM
9
Auth
Football Application
GCM
Framework
10
GCM
Register
11
import com.google.android.gcm.GCMRegistrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
GCMRegistrar.register(this, 968350041068);
Android
12
import com.google.android.gcm.GCMRegistrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (GCMRegistrar.getRegistrationId(this).equals()) {
GCMRegistrar.register(this, 968350041068);
}
Android
Receive data
13
GCM
Football Application
GCM
Framework
RegID
14
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends
GCMBaseIntentService {
@Override
protected void onRegistered(Context ctx, String regId) {
sendToFootballServer(regId);
Android
15
Android
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends
GCMBaseIntentService {
@Override
protected void onMessage(Context ctx, Intent intent) {
final Bundle bundle = intent.getExtras();
doSomeMagic(bundle);
Declare permissions
16
17
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE"
/>
Android
Football application
18
Register with GCM
Receive real time updates!
Declare permissions
Server
19
GCM Auth
RegID, API Key RegID, API Key
API Key
Project ID
Football
App
Football
Server
20
JSON
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
registration_ids :
[APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...],
data : {
Team : Portugal,
Score : 3,
Player : Varela,
},
}
3
21
Portugal vs Denmark
Pepe 24
Postiga 36
Bendtner 41
Bendtner 80
2 2 :
GOAL!!!
Varela 87
USA
22
Born: May 2, 1975
Leytonstone
Height: 6' 0" (1.83 m)
Team: LA Galaxy
David Beckham
News
Football
App
DEVICE C
Football
App
DEVICE B
Football
App
DEVICE A
Message Multicasting
23
GCM
Football
Server
24
JSON
{
collapse_key : Beckham-News,
data : {
Team : LA Galaxy,
Player : David Beckham,
}
registration_ids:[
APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...,
APQ23XFer5MtP0retKMfe1KSFWaFUH-1EWab...,
]
},
25
JSON
{
multicast_id : 5814378600346514436,
"success":1,
"failure":1,
"results":[
{ "message_id":"0:1337639984251701%921c249af9fd7ecd", },
{ "error":"DeviceNotRegistered", }
],
}
Message Multicasting
26
One Request Many Devices
Up to 1000 devices simultaneously
USA
1
27
David Beckham
David Beckham lifts Stanley Cup
USA
1
David Beckham
David Beckham lifts Stanley Cup
Born: May 2, 1975
Leytonstone
Height: 6' 0" (1.83 m)
Team: LA Galaxy
Born: May 2, 1975
Leytonstone
Height: 6' 0" (1.83
m)
Team: LA Galaxy
News
News
USA
Born: May 2, 1975
Leytonstone
Height: 6' 0" (1.83 m)
Team: LA Galaxy
David Beckham
News
28
Football
App
Multiple Senders
29
GCM
Football
Server
Social Updates
Server
30
import com.google.android.gcm.GCMRegistrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (GCMRegistrar.getRegistrationId(this).equals()) {
GCMRegistrar.register(this, 968350041068,
652183961211);
}
Android
31
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends
GCMBaseIntentService {
@Override
protected void onRegistered(Context ctx, String regId) {
sendToFootballServer(regId);
sendToSocialUpdatesServer(regId);
Android
Multiple Senders
32
One common RegistrationID
Multiple parties can send
messages to one application
Up to 100 project IDs
USA
Born: May 2, 1975
Leytonstone
Height: 6' 0" (1.83 m)
Team: LA Galaxy
David Beckham
News
33
1
David Beckham lifts Stanley Cup
Me with the (pretty big) Stanley...
34
San Francisco
25% OFF
KICK OFF!
Football
App
Time To Live
35
GCM
12 3
Football
Server
36
JSON
{
collapse_key : Food-Promo,
time_to_live : 3600,
delay_while_idle : true,
data : {
Category : FOOD,
}
registration_ids:
[APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...],
},
Time To Live
37
From 0 seconds to 4 weeks
Messages with TTL value of 0
are not stored on GCM
Default TTL time is 4 weeks
38
VS
2 2 :
Match Chat
Mario says: great match!!!
Herculez says: I agree!!
Cristiano says: Go Portugal!
Football
App
Messages with Payload
39
GCM
Football
Server
40
JSON
{
registration_ids :
[ APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...],
data : {
Nick : Mario,
Text : great match!,
Room : PortugalVSDenmark,
},
}
128k 256k 512k
Messages with Payload
41
GCM
4k 8k 16k 32k
Football
App
Football
Server
42
{
message_type : deleted_messages,
total_deleted : 115,
},
Android
43
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends
GCMBaseIntentService {
@Override
protected void onDeletedMessages(Context ctx, int total) {
fullSyncWithFootballServer(total);
}
Android
Messages with Payload
44
Payload up to 4K per message
Up to 100 messages are
stored by GCM
Message to trigger sync after that
Summary
45
Message Multicasting for News
Time To Live for Events and Promos
Multiple Senders for Social Updates
Messages with Payload for IM
3
46
Portugal vs Denmark
Pepe 24
Postiga 36
Bendtner 41
Bendtner 80
2 :
Varela 87
47
JSON
{
collapse_key : PortugalDenmark,
time_to_live : 4400,
data : {
Team : Portugal,
Score : 3,
}
registration_ids:[
APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...,
APQ23XFer5MtP0retKMfe1KSFWaFUH-1EWab...,
]
},
Football
App
GCM - Reliability
48
GCM
Optimized for battery life
Provide fresh data all the time
GCM Optimization
49
GCM Optimization
50
GCM
Social Updates
Server
Throttle
queue
Football
Server
Football
App
GCM Optimization
51
GCM
Throttle
queue
Football
Server
Football
App
Throttled
54.6%
41.6%
2.6%
1.2%
52
GCM Messages Metrics
Sent
Stored
Collapsed
53
Sample C2DM App
54
Sample C2DM App
55
Sample C2DM App
56
Sample C2DM App
57
Sample C2DM App
58
Sample C2DM App
59
Sample C2DM App
C2DM to GCM Migration
60
C2DM sign ups and quota requests
are now closed!
C2DM servers will keep running!
GCM is now LIVE!
C2DM to GCM Migration
61
Enable GCM
Point your server to:
http://android.googleapis.com/gcm/send
New Client App version with projectId
as authorized sender
GCM Documentation
62
Getting started guide with a
step by step tutorial
Demo application (client and server)
Getting started for experienced
C2DM developers
GCM Advanced topics
http://developer.android.com
63
?
developer.android.com

Potrebbero piacerti anche