cloud messaging in android

63
Cloud Messaging in Android Francesco Nerieri Engineering Manager

Upload: others

Post on 12-Sep-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cloud Messaging in Android

Cloud Messaging in Android

Francesco NerieriEngineering Manager

Page 2: Cloud Messaging in Android

Android Cloud To Device Messaging

2

NewsServer

C2DM

NewsApp

Page 3: Cloud Messaging in Android

#1#2

Android Cloud To Device Messaging

3

NewsApp

NewsServer

C2DM#3

Page 4: Cloud Messaging in Android

Android Cloud To Device Messaging

4

Google AccountClient Login Token

Sign u

p for

m

Activation EmailInitial Quota

Request More Quota

Page 5: Cloud Messaging in Android

PowerfulFreeSimpleNo Client Login TokenNo Sign Up FormNo QuotaSimplePowerfulMessage MulticastingFastExpiring Messages 95ms Global Average LatencyMessages With PayloadFast4.7 milliseconds

5

Page 6: Cloud Messaging in Android

6

Page 7: Cloud Messaging in Android

FootballApp

GCM - Registration

7

GCM

RegID

RegID

RegID

Football Server

Page 8: Cloud Messaging in Android

FootballApp

Football Server

GCM - Unregistration

8

GCM

Page 9: Cloud Messaging in Android

9

Auth

Page 10: Cloud Messaging in Android

Football Application

GCM Framework

10

GCM

Register

Page 11: Cloud Messaging in Android

11

import com.google.android.gcm.GCMRegistrar;

@Overrideprotected void onCreate(Bundle savedInstanceState) { GCMRegistrar.register(this, “968350041068”);

Android

Page 12: Cloud Messaging in Android

12

import com.google.android.gcm.GCMRegistrar;

@Overrideprotected void onCreate(Bundle savedInstanceState) { if (GCMRegistrar.getRegistrationId(this).equals(“”)) { GCMRegistrar.register(this, “968350041068”); }

Android

Page 13: Cloud Messaging in Android

Receive data

13

GCM

Football Application

GCM Framework

RegID

Page 14: Cloud Messaging in Android

14

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

@Overrideprotected void onRegistered(Context ctx, String regId) { sendToFootballServer(regId);

Android

Page 15: Cloud Messaging in Android

15

Android

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

@Overrideprotected void onMessage(Context ctx, Intent intent) { final Bundle bundle = intent.getExtras(); doSomeMagic(bundle);

Page 16: Cloud Messaging in Android

Declare permissions

16

Page 17: Cloud Messaging in Android

17

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

Android

Page 18: Cloud Messaging in Android

Football application

18

Register with GCM

Receive real time updates!

Declare permissions

Page 19: Cloud Messaging in Android

Server

19

GCM Auth

RegID, API Key RegID, API KeyAPI Key

Project ID

FootballApp

Football Server

Page 20: Cloud Messaging in Android

20

JSON

Content-Type:application/jsonAuthorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA

{ “registration_ids” : [“APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...”], “data” : { “Team” : “Portugal”, “Score” : “3”, “Player” : “Varela”, },}

Page 21: Cloud Messaging in Android

3

21

Portugal vs Denmark

Pepe 24Postiga 36

Bendtner 41Bendtner 80

2 2:

GOAL!!!Varela 87

Page 22: Cloud Messaging in Android

USA

22

Born: May 2, 1975 Leytonstone

Height: 6' 0" (1.83 m)

Team: LA Galaxy

David BeckhamNews

Page 23: Cloud Messaging in Android

FootballApp

DEVICE C

FootballApp

DEVICE B

FootballApp

DEVICE A

Message Multicasting

23

GCMFootball Server

Page 24: Cloud Messaging in Android

24

JSON

{ “collapse_key” : “Beckham-News”, “data” : { “Team” : “LA Galaxy”, “Player” : “David Beckham”, } “registration_ids”:[ “APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...”, “APQ23XFer5MtP0retKMfe1KSFWaFUH-1EWab...”, ]},

Page 25: Cloud Messaging in Android

25

JSON

{ “multicast_id” : “5814378600346514436”, "success":1, "failure":1, "results":[ { "message_id":"0:1337639984251701%921c249af9fd7ecd", }, { "error":"DeviceNotRegistered", } ],}

Page 26: Cloud Messaging in Android

Message Multicasting

26

One Request Many Devices

Up to 1000 devices simultaneously

Page 27: Cloud Messaging in Android

USA1

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 GalaxyBorn: May 2, 1975 Leytonstone

Height: 6' 0" (1.83 m)

Team: LA Galaxy

NewsNews

Page 28: Cloud Messaging in Android

USA

Born: May 2, 1975 Leytonstone

Height: 6' 0" (1.83 m)

Team: LA Galaxy

David BeckhamNews

28

Page 29: Cloud Messaging in Android

FootballApp

Multiple Senders

29

GCMFootball Server

Social UpdatesServer

Page 30: Cloud Messaging in Android

30

import com.google.android.gcm.GCMRegistrar;

@Overrideprotected void onCreate(Bundle savedInstanceState) { if (GCMRegistrar.getRegistrationId(this).equals(“”)) { GCMRegistrar.register(this, “968350041068”, “652183961211”); }

Android

Page 31: Cloud Messaging in Android

31

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

@Overrideprotected void onRegistered(Context ctx, String regId) { sendToFootballServer(regId); sendToSocialUpdatesServer(regId);

Android

Page 32: Cloud Messaging in Android

Multiple Senders

32

One common RegistrationID

Multiple parties can sendmessages to one application

Up to 100 project IDs

Page 33: Cloud Messaging in Android

USA

Born: May 2, 1975 Leytonstone

Height: 6' 0" (1.83 m)

Team: LA Galaxy

David BeckhamNews

33

1

David Beckham lifts Stanley Cup

Me with the (pretty big) Stanley...

Page 34: Cloud Messaging in Android

34

San Francisco

25% OFF

KICK OFF!

Page 35: Cloud Messaging in Android

FootballApp

Time To Live

35

GCM

123

Football Server

Page 36: Cloud Messaging in Android

36

JSON

{ “collapse_key” : “Food-Promo”, “time_to_live” : 3600, “delay_while_idle” : “true”, “data” : { “Category” : “FOOD”, } “registration_ids”: [“APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...”],},

Page 37: Cloud Messaging in Android

Time To Live

37

From 0 seconds to 4 weeks

Messages with TTL value of 0are not stored on GCM

Default TTL time is 4 weeks

Page 38: Cloud Messaging in Android

38

VS

2 2:

Match Chat

Mario says: great match!!!

Herculez says: I agree!!

Cristiano says: Go Portugal!

Page 39: Cloud Messaging in Android

FootballApp

Messages with Payload

39

GCMFootball Server

Page 40: Cloud Messaging in Android

40

JSON

{ “registration_ids” : [ “APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...”], “data” : { “Nick” : “Mario”, “Text” : “great match!”, “Room” : “PortugalVSDenmark”, },}

Page 41: Cloud Messaging in Android

128k256k512k

Messages with Payload

41

GCM

4k8k16k32k

FootballApp

Football Server

Page 42: Cloud Messaging in Android

42

{ “message_type” : “deleted_messages”, “total_deleted” : “115”,},

Android

Page 43: Cloud Messaging in Android

43

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

@Overrideprotected void onDeletedMessages(Context ctx, int total) { fullSyncWithFootballServer(total);}

Android

Page 44: Cloud Messaging in 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

Page 45: Cloud Messaging in Android

Summary

45

Message Multicasting for News

Time To Live for Events and Promos

Multiple Senders for Social Updates

Messages with Payload for IM

Page 46: Cloud Messaging in Android

3

46

Portugal vs Denmark

Pepe 24Postiga 36

Bendtner 41Bendtner 80

2:

Varela 87

Page 47: Cloud Messaging in Android

47

JSON

{ “collapse_key” : “PortugalDenmark”, “time_to_live” : 4400, “data” : { “Team” : “Portugal”, “Score” : “3”, } “registration_ids”:[ “APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...”, “APQ23XFer5MtP0retKMfe1KSFWaFUH-1EWab...”, ]},

Page 48: Cloud Messaging in Android

FootballApp

GCM - Reliability

48

GCM

Page 49: Cloud Messaging in Android

Optimized for battery life

Provide fresh data all the time

GCM Optimization

49

Page 50: Cloud Messaging in Android

GCM Optimization

50

GCM

Social UpdatesServer

Throttlequeue

Football Server

FootballApp

Page 51: Cloud Messaging in Android

GCM Optimization

51

GCM

Throttlequeue

Football Server

FootballApp

Page 52: Cloud Messaging in Android

Throttled 54.6%41.6%

2.6%

1.2%

52

GCM Messages Metrics

Sent

Stored

Collapsed

Page 53: Cloud Messaging in Android

53

Sample C2DM App

Page 54: Cloud Messaging in Android

54

Sample C2DM App

Page 55: Cloud Messaging in Android

55

Sample C2DM App

Page 56: Cloud Messaging in Android

56

Sample C2DM App

Page 57: Cloud Messaging in Android

57

Sample C2DM App

Page 58: Cloud Messaging in Android

58

Sample C2DM App

Page 59: Cloud Messaging in Android

59

Sample C2DM App

Page 60: Cloud Messaging in Android

C2DM to GCM Migration

60

C2DM sign ups and quota requestsare now closed!

C2DM servers will keep running!

GCM is now LIVE!

Page 61: Cloud Messaging in Android

C2DM to GCM Migration

61

Enable GCM

Point your server to:http://android.googleapis.com/gcm/send

New Client App version with projectIdas authorized sender

Page 62: Cloud Messaging in Android

GCM Documentation

62

Getting started guide with astep by step tutorial

Demo application (client and server)

Getting started for experiencedC2DM developers

GCM Advanced topics

http://developer.android.com

Page 63: Cloud Messaging in Android

63

?developer.android.com