code.google.com/apis/console/#project:{SENDER_ID}
<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" />
<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 {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); } }
// 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); }
Source: https://habr.com/ru/post/147585/