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 类:

公共类 GCMIntentService 扩展 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

如果设备具有移动数据连接(例如电话),GCM 只能将消息传递到处于睡眠模式的设备。对于仅支持 WiFi 的设备,如果设备进入睡眠状态,则不会传递 GCM 消息,直到有东西唤醒设备,足够长的时间让设备选择重新连接到 WiFi。

于 2013-06-17T18:21:48.997 回答
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 和 App Standby 空闲模式进行了优化。

于 2016-05-20T10:47:47.440 回答