⬆️ ⬇️

GCM - New Push Notification Service from Google

image

Earlier, Android used C2DM (Cloud to Device Messaging) as a push notification delivery service to a device. But on June 26, it was officially canceled by Google. In its place came the new GCM (Google Cloud Messaging).



Related names. The same role. What is the difference?






GCM Setup



Let's start with Android Manifest


')

First you need to register permissions:

 <uses-permission android:name="android.permission.WAKE_LOCK"/> <permission android:name="{package}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="{package}.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 




Then the receiver and service:

 <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="{package}" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" /> 




* {package} replace with your package (I have com.habrahabr.gcm)



Then in the root directory of the package create a class GCMIntentService , 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 activit:


  //   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, with the exception of sending the notifications from the server themselves, but I think this is enough for one article so far.



Source codes of the resulting application

GCM Architectural Overview

GCM Advanced Topics

Source: https://habr.com/ru/post/147585/



All Articles