Share
Embed
Transcript
66 slides
-
@Tom404_ #DV13-gcm Google Cloud Messaging • Push message service •
No more client polling -
@Tom404_ #DV13-gcm Google Cloud Messaging • Push message service •
No more client polling • Messages are queued while device is offline -
@Tom404_ #DV13-gcm Google Cloud Messaging • Push message service •
No more client polling • Messages are queued while device is offline • Two types of messages • send-to-sync • with payload -
@Tom404_ #DV13-gcm Send-to-sync message • Collapsible message • Tickle to
notify a need of update -
@Tom404_ #DV13-gcm Message with payload • Non-collapsible message • Up
to 4 kb of payload data -
@Tom404_ #DV13-gcm Message with payload • Non-collapsible message • Up
to 4 kb of payload data • Delivery order not guaranteed -
@Tom404_ #DV13-gcm Message with payload • Non-collapsible message • Up
to 4 kb of payload data • Delivery order not guaranteed • Max 100 non-collapsible messages per queue -
@Tom404_ #DV13-gcm Main advantages • Reduces update latency ! •
Preserves battery life ! • Part of Google Play Services ! • Available back to Android 2.2 -
@Tom404_ #DV13-gcm Main advantages • No quota limits ! •
Unicast/multicast ! • App doesn’t need to be running to receive messages -
@Tom404_ #DV13-gcm Data flow 1 3 2 Server Mobile Registration
flow Message flow -
@Tom404_ #DV13-gcm Data flow 1 3 2 a Server Mobile
Registration flow Message flow -
@Tom404_ #DV13-gcm Data flow 1 3 2 a b Server
Mobile Registration flow Message flow -
@Tom404_ #DV13-gcm Prerequisites • Google account • Create a project
in https://cloud.google.com/console -
@Tom404_ #DV13-gcm Prerequisites • Google account • Create a project
in https://cloud.google.com/console • Project number -
@Tom404_ #DV13-gcm Prerequisites • Google account • Create a project
in https://cloud.google.com/console • Project number • Server API key -
@Tom404_ #DV13-gcm Simple as a POST • POST request to
https://android.googleapis.com/gcm/send -
@Tom404_ #DV13-gcm Simple as a POST • POST request to
https://android.googleapis.com/gcm/send • Two headers required • Content-Type • Authorization -
@Tom404_ #DV13-gcm Simple as a POST • POST request to
https://android.googleapis.com/gcm/send • Two headers required • Content-Type • Authorization • Specific format -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key": "your_collapse_key", "time_to_live": 108, "delay_while_idle": true, "data": { "message": "Hi from GCM!" }, "registration_ids": ["1", "2"] "restricted_package_name": "com.devoxx.gcm" , "dry_run": false, -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key": "your_collapse_key", "time_to_live" "delay_while_idle" "data" }, "registration_ids" "restricted_package_name" "dry_run" -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key" "time_to_live": 108, "delay_while_idle" "data" }, "registration_ids" "restricted_package_name" "dry_run" -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key" "time_to_live" "delay_while_idle": true, "data" }, "registration_ids" "restricted_package_name" "dry_run" -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key" "time_to_live" "delay_while_idle" "data": { "message": "Hi from GCM!" }, "registration_ids" "restricted_package_name" "dry_run" -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key" "time_to_live" "delay_while_idle" "data" }, "registration_ids" "restricted_package_name": "com.devoxx.gcm" , "dry_run" -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key" "time_to_live" "delay_while_idle" "data" }, "registration_ids" "restricted_package_name" "dry_run": false, -
@Tom404_ #DV13-gcm { ! ! ! ! ! !
! ! ! } Message format "collapse_key" "time_to_live" "delay_while_idle" "data" }, "registration_ids": ["1", "2"] "restricted_package_name" "dry_run" -
@Tom404_ #DV13-gcm var request = require('request'); ! ...
! request( { method: 'POST', uri:"https://android.googleapis.com/gcm/send", headers:{'Content-‐type': 'application/json', 'Authorization': 'key=YOUR_KEY'}, json:{ 'registration_ids': ['1', '2', '3', '4', '5', '6'], 'collapse_key': 'Update now' } },function(err,res,gcmResult){ // Handle the result } ) Sample request -
@Tom404_ #DV13-gcm { "multicast_id": 216,
"success": 3, "failure": 3, "canonical_ids": 1, "results": [ { "message_id": "1:0408" }, { "error": "Unavailable" }, { "error": "InvalidRegistration" }, { "message_id": "1:1516" }, { "message_id": "1:2342", "registration_id": "32" }, { "error": "NotRegistered"} ] } Response format -
@Tom404_ #DV13-gcm Permissions <uses-‐permission android:name="android.permission.INTERNET" /> <uses-‐permission android:name="android.permission.GET_ACCOUNTS" />
<uses-‐permission android:name="android.permission.WAKE_LOCK" /> -
@Tom404_ #DV13-gcm Permissions <uses-‐permission android:name="android.permission.INTERNET" /> <uses-‐permission android:name="android.permission.GET_ACCOUNTS" />
<uses-‐permission android:name="android.permission.WAKE_LOCK" /> <uses-‐permission android:name="com.google.android.c2dm.permission.RECEIVE" /> -
@Tom404_ #DV13-gcm Permissions <uses-‐permission android:name="android.permission.INTERNET" /> <uses-‐permission android:name="android.permission.GET_ACCOUNTS" />
<uses-‐permission android:name="android.permission.WAKE_LOCK" /> <uses-‐permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.devoxx.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-‐permission android:name="com.devoxx.gcm.permission.C2D_MESSAGE" /> -
@Tom404_ #DV13-gcm private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } else { Log.i(TAG, "This device is not supported."); finish(); } return false; } return true; } Play services, are you there? -
@Tom404_ #DV13-gcm ! ! ! ! ! Registration try {
! ! ! ! ! ! } catch (IOException e) { Log.i(TAG, "Registration failed"); } GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String registrationId = gcm.register(getString(R.string.sender_id)); if (sendRegistrationIdToServer(registrationId)) { } storeRegistrationId(context, registrationId); -
@Tom404_ #DV13-gcm ! ! ! ! ! Registration try
! ! ! ! ! ! } } GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String registrationId = gcm. } storeRegistrationId -
@Tom404_ #DV13-gcm ! ! ! ! ! Registration try {
! ! ! ! ! ! } catch (IOException e) { Log.i(TAG, "Registration failed"); } GoogleCloudMessaging gcm = GoogleCloudMessaging. String registrationId = gcm.register(getString(R.string.sender_id)); } storeRegistrationId -
@Tom404_ #DV13-gcm ! ! ! ! ! Registration try
! ! ! ! ! ! } } GoogleCloudMessaging gcm = GoogleCloudMessaging. String registrationId = gcm. if (sendRegistrationIdToServer(registrationId)) { } storeRegistrationId -
@Tom404_ #DV13-gcm ! ! ! ! ! Registration try
! ! ! ! ! ! } } GoogleCloudMessaging gcm = GoogleCloudMessaging. String registrationId = gcm. } storeRegistrationId(context, registrationId); -
@Tom404_ #DV13-gcm public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { !
@Override public void onReceive(Context context, Intent intent) { ComponentName comp = new ComponentName(context.getPackageName(), GcmMessageHandlerIntentService.class.getName()); startWakefulService(context, intent.setComponent(comp)); setResultCode(Activity.RESULT_OK); } ! } Message reception -
@Tom404_ #DV13-gcm ! ! Message reception @Override protected void
onHandleIntent(Intent intent) { ! ! ! ! ! ! ! ! ! ! ! ! ! } Bundle extras = intent.getExtras(); if (!extras.isEmpty()) { ! ! ! ! ! ! ! ! ! } GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { ! } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { ! } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { String message = intent.getStringExtra("message"); } GcmBroadcastReceiver.completeWakefulIntent(intent); -
@Tom404_ #DV13-gcm ! ! Message reception @Override protected
! ! ! ! ! ! ! ! ! ! ! ! ! } Bundle extras = intent.getExtras(); if (!extras.isEmpty()) { ! ! ! ! ! ! ! ! ! } GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm. ! } ! } String message = intent. } GcmBroadcastReceiver. -
@Tom404_ #DV13-gcm ! ! Message reception @Override protected
! ! ! ! ! ! ! ! ! ! ! ! ! } Bundle extras = intent. ! ! ! ! ! ! ! ! ! } GoogleCloudMessaging gcm = GoogleCloudMessaging. String messageType = gcm.getMessageType(intent); if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { ! } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { ! } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { String message = intent.getStringExtra("message"); } GcmBroadcastReceiver. -
@Tom404_ #DV13-gcm ! ! Message reception @Override protected
! ! ! ! ! ! ! ! ! ! ! ! ! } Bundle extras = intent. ! ! ! ! ! ! ! ! ! } GoogleCloudMessaging gcm = GoogleCloudMessaging. String messageType = gcm. ! } ! } String message = intent. } GcmBroadcastReceiver.completeWakefulIntent(intent); -
@Tom404_ #DV13-gcm What’s new? • GCM HTTP • Upstream (GCM
Cloud Connection Server) -
@Tom404_ #DV13-gcm What’s new? • GCM HTTP • Upstream (GCM
Cloud Connection Server) • User notifications