tdc 2014 - tudo sobre gcm push notifications

48
Tudo sobre GCM Push Notifications SuelenGC

Upload: suelen-goularte-carvalho

Post on 11-Jun-2015

870 views

Category:

Mobile


0 download

DESCRIPTION

Palestra apresentada no TDC 2014 na trilha de Android sobre GCM Push Notifications, o serviço do Google para envio de mensagens do server para o app.

TRANSCRIPT

Page 1: TDC 2014 - Tudo sobre GCM Push Notifications

Tudo sobre GCM Push Notifications

SuelenGC

Page 2: TDC 2014 - Tudo sobre GCM Push Notifications

Suelen GC

Desenvolvedora & Instrutora

github.com/suelengcsuelengc.com

Page 3: TDC 2014 - Tudo sobre GCM Push Notifications

GCMServiço do Google que permite enviar dados do servidor para o aplicativo android.

http://developer.android.com/google/gcm/index.html

Page 4: TDC 2014 - Tudo sobre GCM Push Notifications
Page 5: TDC 2014 - Tudo sobre GCM Push Notifications
Page 6: TDC 2014 - Tudo sobre GCM Push Notifications

novis?novis? novis?

novis? novis?

Page 7: TDC 2014 - Tudo sobre GCM Push Notifications

novis?novis? novis?

novis? novis?

Qual a periodicidade para buscar informações no servidor?

Page 8: TDC 2014 - Tudo sobre GCM Push Notifications

Já chegou a informação??

chegou?agora chegou??

e agora?

e agora??

chegou???

Page 9: TDC 2014 - Tudo sobre GCM Push Notifications

C A L A D O O O ! ! ! !

Page 10: TDC 2014 - Tudo sobre GCM Push Notifications

novis?novis? novis?

novis? novis?

Como fazer isso de forma performática?

Page 11: TDC 2014 - Tudo sobre GCM Push Notifications

novis?novis? novis?

novis? novis?

Meu servidor está preparado para todas as request’s que irá receber?

Page 12: TDC 2014 - Tudo sobre GCM Push Notifications

Ok, o inverso seria…?

Page 13: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Vem pegar.Chegou algo! !Vem buscar.

Page 14: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Vem pegar.

Como o servidor saberá quais são os devices que tem o app instalado?

Page 15: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Vem pegar.

Seu servidor vai aguentar fazer este monte de conexão direta com devices?

Page 16: TDC 2014 - Tudo sobre GCM Push Notifications
Page 17: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Vem pegar.

Ao invés disso ocorrer…

Page 18: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Vem pegar.

Google Cloud Message

Colocaram uma camada a mais…

Page 19: TDC 2014 - Tudo sobre GCM Push Notifications

Podemos enviar de 2 formas as informações do

server para o device/app

Page 20: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou um email! Vem pegar.

Chegou + posts! Vem pegar.

Chegou + posts! Vem pegar.

Outros app aqui…

send-to-sync

Page 21: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Avise aos app’s.

Page 22: TDC 2014 - Tudo sobre GCM Push Notifications

Chegou algo! !Vão buscar.

Page 23: TDC 2014 - Tudo sobre GCM Push Notifications

Me dá! Me dá! Me dá! Me dá!Me dá!

Page 24: TDC 2014 - Tudo sobre GCM Push Notifications

Outros app aqui…

payload

Chegou nova mensagem. Tó!

Chegou nova mensagem. Tó!

Chegou nova mensagem. Tó!

Page 25: TDC 2014 - Tudo sobre GCM Push Notifications

Entregue aos !app’s.

Page 26: TDC 2014 - Tudo sobre GCM Push Notifications
Page 27: TDC 2014 - Tudo sobre GCM Push Notifications

send-to-sync payloadUpstream/

Downstream de mensagens

Apenas downstream (cloud-to-device)

Ambos. (device-to-cloud, cloud-to-device)

Síncrono Sim (http post)

Não. (Conexão XMPP)

Comparativo de formas de envio

Page 28: TDC 2014 - Tudo sobre GCM Push Notifications

Como implementa?

Page 29: TDC 2014 - Tudo sobre GCM Push Notifications

ServidorAplicativo

Page 30: TDC 2014 - Tudo sobre GCM Push Notifications

ServidorAplicativo

Page 31: TDC 2014 - Tudo sobre GCM Push Notifications

https://code.google.com/apis/console

Passo 1: Habilitar e criar um projeto do GCM

Page 32: TDC 2014 - Tudo sobre GCM Push Notifications

https://code.google.com/apis/console

Passo 1: Habilitar e criar um projeto do GCM

Page 33: TDC 2014 - Tudo sobre GCM Push Notifications

http://developer.android.com/google/gcm/client.html#manifest

Passo 2: Configurar o AndroidManifest.xml

Page 34: TDC 2014 - Tudo sobre GCM Push Notifications

Passo 3: Implementar!

Page 35: TDC 2014 - Tudo sobre GCM Push Notifications

Quando iniciar o app ele se registrar para receber mensagens de determinado servidor

register

Page 36: TDC 2014 - Tudo sobre GCM Push Notifications

Quando iniciar o app ele se registrar para receber mensagens de determinado servidor

registration_id

(device + app)

Page 37: TDC 2014 - Tudo sobre GCM Push Notifications

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(app);

String registrationId = gcm.register(SERVER_ID);

Page 38: TDC 2014 - Tudo sobre GCM Push Notifications

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(app);

String registrationId = gcm.register(SERVER_ID);

dependencies { compile "com.google.android.gms:play-services:3.1.+" }

Context

Projeto criado no Console API's

Page 39: TDC 2014 - Tudo sobre GCM Push Notifications

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(app);

String registrationId = gcm.register(SERVER_ID);

Deve ser em outra thread!

Page 40: TDC 2014 - Tudo sobre GCM Push Notifications

Guarde seu registration_id  para não precisar buscar novamente.

registration_id

(device + app)

Page 41: TDC 2014 - Tudo sobre GCM Push Notifications

registration_id

O device passa seu registration_id para o servidor

Page 42: TDC 2014 - Tudo sobre GCM Push Notifications

WebClient web = new WebClient(url_do_servidor);

web.post(registration_id)

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(app);

String registrationId = gcm.register(SERVER_ID);

Page 43: TDC 2014 - Tudo sobre GCM Push Notifications

registration_id’s  1,  2  e  3  

Page 44: TDC 2014 - Tudo sobre GCM Push Notifications

regist

ration

_id  =  

1

registration_id  =  2

registration_id  =  3

registration_id’s  1,  2  e  3  

Page 45: TDC 2014 - Tudo sobre GCM Push Notifications

regist

ration

_id  =  

1

registration_id  =  2

registration_id  =  3

registration_id’s  1,  2  e  3  

BroadcastReceiver!para receber a mensagem

Page 46: TDC 2014 - Tudo sobre GCM Push Notifications

Configurando o Receiver

Page 47: TDC 2014 - Tudo sobre GCM Push Notifications

Mensagens ficam no GCM por até 4 semanas. É configurável. !

"De gratis"! Sem limite de uso. !

Suporte a partir da versão 2.2 do Android.

Detalhes

Page 48: TDC 2014 - Tudo sobre GCM Push Notifications

Suelen GCgithub.com/suelengcwww.suelengc.com.br

Obrigada!