Slide 1

Slide 1 text

16.11.2013 DevFest Central Italy @ Rome Google Developer Group Perugia http://gdg-perugia.org

Slide 2

Slide 2 text

+EmanuelePalazzetti Manager Google Developer Group Perugia @palazzem github.com/emanuele-palazzetti [email protected] Who am I?

Slide 3

Slide 3 text

Wake up your Android with Google Cloud Messaging

Slide 4

Slide 4 text

Google Cloud Messaging? Not yet… ...let’s talk about something else Google Cloud Messaging

Slide 5

Slide 5 text

Conference organization!

Slide 6

Slide 6 text

It’s not easy to organize a conference ● find speakers ● event marketing ● prepare website, flyer, etc… ● communicate with attendees: ○ before ○ during ○ after The problem

Slide 7

Slide 7 text

Hey! This is a DevFest! We should talk about code! What?

Slide 8

Slide 8 text

DevFest Android app (use case) ● all italian DevFest list during the year ● for each DevFest provide talks scheduling ● some notifications Android app

Slide 9

Slide 9 text

DevFest Android app (end-user requirements) ● notifications without any running application ● notifications across all owned devices ● real time notifications (delay, rooms swap, etc...) ● hey! don’t exhaust my battery! Android app

Slide 10

Slide 10 text

Google Cloud Messaging If you have an Android application and a server you should use it! You can finally throw out the “refresh button” Even with your third-party application server Google Cloud Messaging

Slide 11

Slide 11 text

Google Cloud Messaging ● Integrated with Google Play Services ● Available back to Froyo (remember about dropped support) NOTE: old GCM standalone implementation is deprecated! Use the GoogleCloudMessaging API instead! Google Cloud Messaging

Slide 12

Slide 12 text

Pros ● No battery consumption (server push) ● App doesn’t need to be running to receive messages ● Reduces update latency ● Transmit messages to multiple devices Google Cloud Messaging

Slide 13

Slide 13 text

Registration Google Cloud Messaging Device Registration

Slide 14

Slide 14 text

Registration Google Cloud Messaging Registration ID

Slide 15

Slide 15 text

Registration Google Cloud Messaging Registration ID Your server

Slide 16

Slide 16 text

Send a message Google Cloud Messaging Your server Message to: registrationId Message

Slide 17

Slide 17 text

What happen if user devices are offline? ● Google provides automatic queue management ● When device come back online again, all messages are delivered ● We can have 100 stored messages (in the queue) and if you reach that limit, all messages are discarded and a special message is delivered NOTE: use the special message to manage a full sync! Google Cloud Messaging

Slide 18

Slide 18 text

Getting started

Slide 19

Slide 19 text

Application server requirements: ● Able to communicate with your client (endpoints) ● Able to store the API key and client registration IDs ● Able to fire off properly formatted requests to the GCM server ● Able to generate message IDs to identify each message it sends Application server

Slide 20

Slide 20 text

Requests to GCM server ● You can use two different protocols: HTTP or CCS (XMPP) ● JSON message: ○ HTTP: sent as HTTP POST ○ CCS: message is encapsulated in XMPP message Application server

Slide 21

Slide 21 text

Send-to-sync message Send-to-sync message with collapse key

Slide 22

Slide 22 text

Message with payload max 4kb payload useful to avoid further connections!

Slide 23

Slide 23 text

Android client Android manifest

Slide 24

Slide 24 text

Android client Some best practices before continue ● If you are using Play Services SDK, always perform a compatibility check in Activity onCreate() and onResume() ● Write your own wrapper around these methods:

Slide 25

Slide 25 text

Android client Request registrationId

Slide 26

Slide 26 text

Android client About storing your registrationId: ● It’s better to generate a new one if you update your application ● If you use Google Backup Service, avoid to backup registrationId

Slide 27

Slide 27 text

Android client Simple BroadcastReceiver to manager GCM messages

Slide 28

Slide 28 text

Android client Done! Remember: ● Register your Google API_KEY ● Configure AndroidManifest.xml ● Android registration on GCM ● Send received registrationId to your application server (and store it) ● Prepare your broadcast receiver ● Start sending GCM messages from your server!

Slide 29

Slide 29 text

Hands on code! https://developers.google.com/cloud/samples/mbs/

Slide 30

Slide 30 text

+EmanuelePalazzetti Manager Google Developer Group Perugia @palazzem github.com/emanuele-palazzetti [email protected] Thanks for your attention!

Slide 31

Slide 31 text

Questions?