Максим Щеглов - google cloud messaging for android
TRANSCRIPT
Google Cloud Messaging for Android
Shcheglov MaksymMTS1 at Magento inc.
Google Cloud Messaging for Android
GCM & C2DM
• no signup to get access;
• no authentication using Google account;
• no message quota.
Google Cloud Messaging for Android
Requirements
• Android 2.2 or higher;
• Google account (OS version < Android 4.0.4);
• project ID (using Google API console);
• API key (using Google API console).
Google Cloud Messaging for Android
Requirements
Google Cloud Messaging for Android
Requirements
Google Cloud Messaging for Android
GCM Server
Mobile phone
Registration ID
1
2
3
Registration ID
Sender ID,Application ID
Enabling GCM
Google Cloud Messaging for Android
Enabling GCMimport com.google.android.gcm.GCMRegistrar;
public class UAMobileActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String regID = GCMRegistrar.getRegistrationId(this);
if (regID.equals("")) {
// register application
GCMRegistrar.register(this, "484449755275");
} else {
// Device is already registered on GCM
}
}
}
Google Cloud Messaging for Android
Enabling GCMimport com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onRegistered(Context context, String registrationID) {
//send registrationID to the server
registerOnServer(context, registrationID);
}
}
Google Cloud Messaging for Android
GCM Server
Mobile phone
1
2
A message with API key,
Registration ID
Sending a message
Google Cloud Messaging for Android
Sending a messageRequest
https://android.googleapis.com/gcm/send
HTTP headers:
Content-Type:application/json
Authorization:key=AIzaSyAIpOT5VHEOthyrwKjwrNwwEVHQIgioypk
HTTP body:
{
“collapse_key” : “news_update”,
“time_to_live” : 100,
“delay_while_idle” : true,
"data" : { ... },
"registration_ids" : [”ID1”, “ID2”, “ID3”],
}
Google Cloud Messaging for Android
Sending a messageResponse
{
"multicast_id": 112,
"success": 2,
"failure": 1,
"canonical_ids": 1,
”results": [
{ “message_id": "1:2036" },
{ "message_id": "1:7695", "registration_id": ”102" },
{ "error": "NotRegistered"}
]
}
Google Cloud Messaging for Android
Sending a messageClient
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onMessage(Context context, Intent intent) {
// received the message
final Bundle bundle = intent.getExtras();
String data = bundle.getString("some_data");
}
}
Google Cloud Messaging for Android
Unregistration
• manually by sending an intent com.google.android.c2dm.intent.UNREGISTER;
• uninstall the application;
• registration ID is expired;
• application is updated, but does not have a broadcast receiver configured to receive com.google.android.c2dm.intent.RECEIVE intents.
Google Cloud Messaging for Android
GCM Server
Mobile phone
1
2
Unregistration
3
invalidate
4
5
Device not registered error
Google Cloud Messaging for Android
Message states
• sent;
• stored (a device is offline);
• collapsed (a device is offline & we have a new message);
• throttled.
Google Cloud Messaging for Android
Concepts
• send-to-sync;
• message with payload.
Google Cloud Messaging for Android
Features
• no broadcasting;
• no guarantees about delivery and order;
• message multicasting (up to 1000 devices per 1 request);
• multiple senders (max 100 project IDs per application);
• store up to 100 non-collapsible messages;
• maximum 4kb per message;
• optimizations;
• analytics.
Google Cloud Messaging for Android
How to migrate from C2DM
Client
• deliver a new version of your app;
• send a flag with registration ID.
Server
• new endpoint;
• API key in the Authorization header.
Google Cloud Messaging for Android
GCM & APNS
• no certificates;
• one http address for development and production;
• uses json or plaint text in requests, not binary data;
• no feedback service;
• developer-friendly error codes;
• no actions (alert/badge/sound);
• message size (4kb & 256b).
Demo
Q & A