Google Developer Group Perugia http://gdg-perugia.org 16.11.2013 DevFest Central Italy @ Rome
Who am I? +EmanuelePalazzetti Manager Google Developer Group Perugia @palazzem github.com/emanuele-palazzetti emanuele.palazzetti@gmail.com
Wake up your Android with Google Cloud Messaging
Google Cloud Messaging Google Cloud Messaging? Not yet...let s talk about something else
Conference organization!
The problem It s not easy to organize a conference find speakers event marketing prepare website, flyer, etc communicate with attendees: before during after
What? Hey! This is a DevFest! We should talk about code!
Android app DevFest Android app (use case) all italian DevFest list during the year for each DevFest provide talks scheduling some notifications
Android app 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!
Google Cloud Messaging 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 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 Pros No battery consumption (server push) App doesn t need to be running to receive messages Reduces update latency Transmit messages to multiple devices
Registration Device Registration Google Cloud Messaging
Registration Registration ID Google Cloud Messaging
Registration Google Cloud Messaging Registration ID Your server
Send a message Message Google Cloud Messaging Message to: registrationid Your server
Google Cloud Messaging 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!
Getting started
Application server 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 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
Send-to-sync message Send-to-sync message with collapse key
Message with payload max 4kb payload useful to avoid further connections!
Android client Android manifest
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:
Android client Request registrationid
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
Android client Simple BroadcastReceiver to manager GCM messages
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!
Hands on code! https://developers.google.com/cloud/samples/mbs/
+EmanuelePalazzetti Manager Google Developer Group Perugia @palazzem github.com/emanuele-palazzetti emanuele.palazzetti@gmail.com Thanks for your attention!
Questions?