W przypadku powiadomień push na Androida z GCM 2016:
1) w Android SDK-> SDK Tools sprawdź usługi Google Play
2) w gradle dodaj zależności tylko jedną linię:
compile 'com.google.android.gms:play-services-gcm:9.4.0'
(nie ma określonej ścieżki klas i u mnie działa)
3) musisz utworzyć klasę 3 (GCMPushReceiverService, GCMRegistrationIntentService, GCMTokenRefreshListenerService)
4.1) kod usługi GCMTokenRefreshListenerService:
package com.myapp.android;
import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService;
public class GCMTokenRefreshListenerService extends InstanceIDListenerService {
@Override
public void onTokenRefresh() {
Intent intent = new Intent(this, GCMRegistrationIntentService.class);
startService(intent);
}
}
4.2) Kod dla GCMRegistrationIntentService (zmień autoryzowany podmiot na numer projektu):
package com.myapp.android;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
public class GCMRegistrationIntentService extends IntentService {
public static final String REGISTRATION_SUCCESS = "RegistrationSuccess";
public static final String REGISTRATION_ERROR = "RegistrationError";
public GCMRegistrationIntentService() {
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
registerGCM();
}
private void registerGCM() {
Intent registrationComplete = null;
String token = null;
try {
InstanceID instanceID = InstanceID.getInstance(this);
String authorizedEntity = "XXXXXXXXXX";
token = instanceID.getToken(authorizedEntity, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.w("GCMRegIntentService", "token:" + token);
registrationComplete = new Intent(REGISTRATION_SUCCESS);
registrationComplete.putExtra("token", token);
} catch (Exception e) {
Log.w("GCMRegIntentService", "Registration error");
registrationComplete = new Intent(REGISTRATION_ERROR);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
}
4.3) Kod dla GCMPushReceiverService:
package com.myapp.android;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import com.google.android.gms.gcm.GcmListenerService;
public class GCMPushReceiverService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.your_logo)
.setContentTitle("Your Amazing Title")
.setContentText(message)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);
noBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build());
}
}
5) Nie zapomnij zmienić nazwy pakietu
6) W swoim mainActivity wklej ten kod:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
String token = intent.getStringExtra("token");
Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show();
} else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
Toast.makeText(getApplicationContext(), "GCM registration error!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
}
}
};
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(ConnectionResult.SUCCESS != resultCode) {
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
Toast.makeText(getApplicationContext(), "Google Play Service is not install/enabled in this device!", Toast.LENGTH_LONG).show();
GooglePlayServicesUtil.showErrorNotification(resultCode, getApplicationContext());
} else {
Toast.makeText(getApplicationContext(), "This device does not support for Google Play Service!", Toast.LENGTH_LONG).show();
}
} else {
Intent itent = new Intent(this, GCMRegistrationIntentService.class);
startService(itent);
}
}
@Override
public void onPause() {
super.onPause();
Log.w("MainActivity", "onPause");
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
}
@Override
public void onResume() {
super.onResume();
Log.w("MainActivity", "onResume");
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(GCMRegistrationIntentService.REGISTRATION_SUCCESS));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(GCMRegistrationIntentService.REGISTRATION_ERROR));
}
7) W pliku AndroidManifest.xml dodaj następujące wiersze:
<uses-permission android:name="android.permission.INTERNET" />
8) w logcat konsoli skopiuj token i wklej w tej witrynie
dodaj numer projektu, token i wiadomość. U mnie działa dobrze :)