未从Notification Server发送的GCM接收通知

时间:2016-05-18 05:33:26

标签: java android google-cloud-messaging android-notifications notificationservices

我正在尝试为Android应用程序实现通知服务器。我按照教程http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/进行了操作并且工作正常。但是现在服务器端的整个事情由我的通知服务器处理,我与用户登录时共享我的设备ID和userId。我的通知服务器成功将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);
}
 }

2 个答案:

答案 0 :(得分:0)

如果您从GCM服务器获得如下所示的成功消息,则必须收到您的设备通知

来自GCM服务器的成功消息

if

可能是以下是设备中未收到消息的情况。

  1. 在android端错误的发件人ID。
  2. 服务器端的错误Google API密钥(PHP)。
  3. 您要发送到GCM服务器的设备注册ID错误。
  4. 请立即验证所有值并检查。 如果一切都很完美,那么您必须在设备中收到消息。 请检查设备中的互联网连接。

    请在下方验证您的shmaddr文件。

    {
        "multicast_id": 661164****4469281,
        "success": 1,
        "failure": 0,
        "canonical_ids": 1,
        "results": [{
            "registration_id": "APA91bFWKKC4Bh_B******-H",
            "message_id": "0:14635506***b6f9fd7ecd"
        }]
    }
    

答案 1 :(得分:0)

如果您确实在示例代码中收到了消息;我认为您的项目gcm上不允许服务器的IP地址。或者服务器的防火墙阻止gcm的消息,如https://stackoverflow.com/a/13470029/2917564中所示。无论哪种方式,我建议你在你的服务器上运行一个独立的简单php脚本,只有你的注册ID,以确保它不在代码中,你可以找到一个https://stackoverflow.com/a/11253231/2917564

相关问题