GCM 2016을 사용한 Android 푸시 알림 :
1) Android SDK-> SDK 도구에서 Google Play 서비스 확인
2) gradle에서 종속성을 한 줄만 추가하십시오.
compile 'com.google.android.gms:play-services-gcm:9.4.0'
(특정 클래스 경로가 없으며 나를 위해 작동합니다)
3) 3 개의 클래스 (GCMPushReceiverService, GCMRegistrationIntentService, GCMTokenRefreshListenerService)를 생성해야합니다.
4.1) 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) GCMRegistrationIntentService 용 코드 (authorizedEntity를 프로젝트 번호로 변경) :
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) 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) 패키지 이름을 변경하는 것을 잊지 마십시오
6) mainActivity에 다음 코드를 붙여 넣으십시오.
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) AndroidManifest.xml에 다음 줄을 추가하십시오.
<uses-permission android:name="android.permission.INTERNET" />
8) 콘솔 logcat에서 토큰을 복사하여이 사이트
에 붙여넣고 프로젝트 번호, 토큰 및 메시지를 추가하십시오. 그것은 나를 위해 잘 작동합니다 :)