El dispositivo Android no recibe un mensaje mientras delay_while_idle está configurado como verdadero

Is there a way to see if a device is known to be idle to the GCM server?

Is there a way to use delay_while_idle without using a collapse_key?

When I setup my message like this in php it does work.

$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $key);
$data = array(
    'registration_ids' => $deviceRegistrationIds,
    'data' => array('message' => $messageText,
        'msgfromname' => $fromname,
        'close' => $close,
        'newchat' => $newchat,
        'msgfrom' => $from)
);

When I use delay_while_idle like this it does not work.

$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $key);
$data = array(
    'registration_ids' => $deviceRegistrationIds,
    'collapse_key' => $messageText,
    'delay_while_idle' => true,
    'data' => array('message' => $messageText,
        'msgfromname' => $fromname,
        'close' => $close,
        'newchat' => $newchat,
        'msgfrom' => $from)
);

I guess this is because $messageText has a same value as something in the data array? When I change it's value to 'hello' it does work.

preguntado el 28 de agosto de 12 a las 14:08

2 Respuestas

Only thing I can see is you need to surround $messageText and all other string with double quotes. I've also set delay_while_idle to 1 instead of true..I know it should convert to 1 when you pass it true.

Also I dont think you're using the collapse_key correctly (Cant know without knowing what actual data you're setting there) but typically you'll use that field as a "batch id".

$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $key);
$data = array(
    'registration_ids' => $deviceRegistrationIds,
    'collapse_key' => "$messageText",
    'delay_while_idle' => 1,
    'data' => array('message' => "$messageText",
        'msgfromname' => "$fromname",
        'close' => "$close",
        'newchat' => "$newchat",
        'msgfrom' => "$from")
);

Respondido 28 ago 12, 14:08

Thank you for your answer. I'm writing a chat application with GCM. So there shouldn't be a collapse_key since I'm sending the chat message with it. When using 1 instead of true all messages fail. - Klaasvaak

Using a random number now for collapse_key. It seems to work. - Klaasvaak

Probably best you don't use a random number, it might cause messages to vanish at times if the numbers are the same. Probably just do some counter on the user. However for a chat client, delaying while idle may not be suitable at all as I would like my messages delivered when my phone is idle. - Joe Simpson

If the collapse keys are all unique it shouldn't pose a problem (as far as I can see).. by having the collapse keys be unique it should cause each message to be delivered.. however you can only have 100 unique messages "queued" developer.android.com/guide/google/gcm/adv.html "And that's where the collapse_key flag plays a role: if there is already a message with the same collapse key (and registration ID) stored and waiting for delivery, the old message will be discarded and the new message will take its place (that is, the old message will be collapsed by the new one)." - Ainesophaur

@Klaasvaak The phone goes to sleep when the screen is turned off.. by default if the phone is "unreachable" because it's off then GCM will queue up to 100 messages that do not have a collapsed key. If you set 'delay_while_idle' then GCM will not deliver messages when the screen is off..it'll wait until the device is in "Active" use - Ainesophaur

You can set delay_while_idle to true without specifying a collapse_key.

GCM only supports up to 4 collapse keys at a time. If you use more than 4 collapse keys while a device is offline, only 4 of the messages will be kept, and there is no guarantee which 4 it will be.

If you use the message text as the collapse key, GCM will only hold four unique messages, and there's no way for you to know which four messages it will end up delivering.

(BTW, if you do want to use a collapse_key, I'm pretty sure you must also provide a time_to_live value or the request will be rejected.)

Respondido 31 Oct 12, 22:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.