Android アプリの通知サーバーを実装しようとしています。チュートリアルhttp://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/に従い、正常に動作しました。しかし今では、サーバー側のすべてが通知サーバーによって処理され、ユーザーのログイン時にデバイス ID とユーザー ID を共有しています。通知サーバーは regid を gcm に正常に送信しますが、デバイスで通知を受け取りません。私の onReceive() は現在呼び出されていません。これは、私のサーバーが gcm から受け取った応答です。messageId=0:1463550332634647%fd0791fdf9fd7ecd
Config.java
public interface Config {
//static final String APP_SERVER_URL = "http://192.168.100.27:8081/GCMServer/GCMNotification?shareRegId=1";//earlier it was used to share regid with server
static final String GOOGLE_PROJECT_ID = "299876636766";
static final String MESSAGE_KEY = "message";
}
GCMNotificationIntentService.java
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
System.out.println("MessageType is"+messageType);
String msg = intent.getStringExtra("message");
System.out.println("mESSAGE IS---->>>"+msg);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 3; i++) {
Log.i(TAG,
"Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification(""+msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
System.out.println("Message is"+msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent=null ;
if(msg.equalsIgnoreCase("call")){
System.out.println("Inside iffffffffffffffffffffff");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wl.acquire();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
//setResultCode(Activity.RESULT_OK)
wl.release();
contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SplashScreen.class), 0);
System.out.println("the content intent is"+contentIntent.toString());
}else{
System.out.println("Inside else");
contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
}
GCMBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}