GCM - Google’s New Push Notification Service

Previously, Android used C2DM (Cloud to Device Messaging) as a service for delivering push notifications to a device. But on June 26, it was officially canceled by Google. In its place came the new GCM (Google Cloud Messaging).
Similar names. The same role. What is the difference?
- To use GCM, you need to get the Simple API Key in the Google APIs console.
- For GCM, you need to get a Sender ID. It is the equivalent of email in C2DM. You can get it again from the Google APIs console, or rather from the URL:
code.google.com/apis/console/#project:{SENDER_ID} - GCM notifications are in JSON format along with plain text.
- GCM can send notifications to multiple devices at once.
- Now one device with one registration identifier can receive notifications from several servers at once.
- Notifications can now have a lifespan of up to 4 weeks. GCM will store them until the expiration date.
- Now you can send notifications up to 4K with a payload. This will be very beneficial for the real time of various chats. However, this method will eat the device’s battery more strongly.
- Now there is no need to transfer the device identifier to the server in order to avoid repeated registrations of one device. The canonical registration identifier is determined by the GCM from the last registration of the device. And if the server sends a notification with the old identifier, then GCM will return the canonical (last) identifier, by which it will be necessary to replace the old one.
Configure GCM
Let's start with Android Manifest
First you need to register permissions:
Then the receiver and service:
* {package} replace with your package (I have com.habrahabr.gcm)
Then, in the root directory of the package, create the GCMIntentService class , inherited from GCMBaseIntentService :
package {package};
import android.app.Activity;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(GCMConfig.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered");
// Здесь мы должны отправить registrationId на наш сервер, чтобы он смог на него отправлять уведомления
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received new message");
}
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
}
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
Log.i(TAG, "Received recoverable error: " + errorId);
return super.onRecoverableError(context, errorId);
}
}
And after that we prescribe in the main activity:
// Делаем проверки
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
// Достаем идентификатор регистрации
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) { // Если отсутствует, то регистрируемся
GCMRegistrar.register(this, GCMConfig.SENDER_ID);
} else {
Log.v("GCM", "Already registered: " + regId);
}
Now everything is ready, except for sending the notifications themselves from the server, but I think that for one article this is enough for now.
Sources for the resulting
GCM Architectural Overview GCM Advanced Topics application