Android的GCM:不发送通知时,设备处于空闲状态。 [英] Android GCM: not sending notification when device is idle.

查看:224
本文介绍了Android的GCM:不发送通知时,设备处于空闲状态。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCM通知不被接受。他们都来过一次当设备被唤醒。这里是code发送消息给GCM:

GCM notifications are not being received when my device is idle. They all come through at once when the device is woken up. Here is the code that sends the message to 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类:

Here is my GCMIntentService class:

公共类GCMIntentService扩展GCMBaseIntentService {

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是这个答案所描述的code:

WakeLocker is the code described in this answer:

<一个href="http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up?rq=1">android AlarmManager没有醒来电话了

推荐答案

GCM只能将邮件传递到处于睡眠模式,如果设备有移动数据连接,例如电话设备。对于WiFi的唯一设备,如果设备睡着了,GCM消息不会被传递到的东西会唤醒器件,足够长的时间,该装置选举重新连接到无线网络。

GCM can only deliver messages to devices that are in sleep mode if the device has mobile data connectivity, such as a phone. For WiFi-only devices, if the device falls asleep, GCM messages will not be delivered until something wakes up the device, long enough that the device elects to reconnect to WiFi.

这篇关于Android的GCM:不发送通知时,设备处于空闲状态。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆