Android GCM: not sending notification when device is idle.
Asked Answered
S

3

0

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);

Here is my GCMIntentService class:

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 is the code described in this answer:

android AlarmManager not waking phone up

Salvo answered 17/6, 2013 at 17:49 Comment(2)
Is your device WiFi-only?Abhor
Yes, it is a media playerSalvo
A
1

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.

Abhor answered 17/6, 2013 at 18:21 Comment(3)
Not entirely true. I have developed an app that runs on a Wi-Fi only device and it will receive GCM messages although this seems to be intermittent. I have left a device off overnight and then the next morning sent messages to it and it received it. At the time of your writing, perhaps an older version of the Google Play services didn't wake up. The current version does appear to wake up the device every 15 minutes if the device is in sleep mode but then switches to a permanent connection if the user takes it out of sleep mode.Chondro
That seems so, and it should do that. The answer is not old enough to agree with the statement. If device is sleep how can Google Device Manager can invoke device through their own service?Damselfly
@Abhor : I have got the same issue in FCM. Please provide me the solutionCrone
H
1

In server side code please check the value you are passing in delayWhileIdle() method. make it false. It will work.

Hettie answered 30/5, 2014 at 9:12 Comment(1)
By default it is false, so no need to set it!Folkmoot
E
0

For Marshmallow please look at this:

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

GCM is optimized to work with Doze and App Standby idle modes by means of high-priority GCM messages.

Eulogy answered 20/5, 2016 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.