2. Google Cloud Messaging for Android
GCM & C2DM
• no signup to get access;
• no authentication using Google account;
• no message quota.
3. 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).
6. Google Cloud Messaging for Android
Enabling GCM
GCM Server
Sender ID,
Application ID
2
1
Registration ID Registration ID
3
Mobile phone
7. Google Cloud Messaging for Android
Enabling GCM
import 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
}
}
}
8. Google Cloud Messaging for Android
Enabling GCM
import 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);
}
}
9. Google Cloud Messaging for Android
Sending a message
1
GCM A message with API
Server
key,
Registration ID
2
Mobile phone
12. Google Cloud Messaging for Android
Sending a message
Client
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");
}
}
13. Google Cloud Messaging for Android
Unregistration
• manually by sending an intent
com.google.android.c2dm.intent.UNREGI
STER;
• 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.RECEIV
E intents.
14. Google Cloud Messaging for Android
Unregistration
Device not
registered error
5
1
4
GCM Server
invalidate
2
3
Mobile phone
15. Google Cloud Messaging for Android
Message states
• sent;
• stored (a device is offline);
• collapsed (a device is offline & we have a new message);
• throttled.
17. 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.
18. 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.
19. 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).