0

デバイスがアイドル状態のときに GCM 通知が受信されません。デバイスが起動されると、それらはすべて一度に通過します。メッセージを GCM に送信するコードは次のとおりです。

Sender sender = new Sender(API_KEY);
Message message = new Message.Builder().addData("type","newRequest").delayWhileIdle(false).build();
MulticastResult results = sender.send(message, devices, 5);

ここに私の GCMIntentService クラスがあります:

public class GCMIntentService extends GCMBaseIntentService{

public static final String SENDER_ID = "XXXXXXXXXXXXX";

public GCMIntentService(){
    super(SENDER_ID);
}

@Override
protected void onError(Context context, String errorId) {
    // TODO Auto-generated method stub
}

@Override
protected void onMessage(Context context, Intent intent) {
    // TODO Auto-generated method stub
    WakeLocker.acquire(context);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    long when = System.currentTimeMillis();
    int icon = R.drawable.ic_dialog_info;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);
    Notification noti = new NotificationCompat.Builder(this)
            .setWhen(when).setContentTitle("New Storage Request")
            .setContentText("Touch to open app")
            .setSmallIcon(icon)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setTicker("New Storage Request").setAutoCancel(true)
            .setContentIntent(contentIntent)
            .getNotification();

    mNotificationManager.notify(0, noti);
}

@Override
protected void onRegistered(Context context, String regId) {
    // TODO Auto-generated method stub

    RestClient client = new RestClient(StorageApp.STORAGE_HOST+"StorageWebService/rest/operations/registerForGCM");
    client.AddParam("regId", regId);

    try {
        client.Execute(RequestMethod.GET);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("done with call");
}

@Override
protected void onUnregistered(Context context, String regId) {
    // TODO Auto-generated method stub
    RestClient client = new RestClient(StorageApp.STORAGE_HOST+"StorageWebService/rest/operations/unregisterForGCM");
    client.AddParam("regId", regId);

    try {
        client.Execute(RequestMethod.GET);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

WakeLocker は、この回答で説明されているコードです。

Android AlarmManagerが電話を起こさない

4

3 に答える 3

1

サーバー側のコードで、delayWhileIdle() メソッドに渡す値を確認してください。偽にします。それが動作します。

于 2014-05-30T09:12:19.030 に答える
0

マシュマロについては、これを見てください:

https://developer.android.com/training/monitoring-device-state/doze-standby.html#using_gcm

GCM は、優先度の高い GCM メッセージによって Doze およびアプリ スタンバイのアイドル モードで動作するように最適化されています。

于 2016-05-20T10:47:47.440 に答える