Pergunta

I use GCM this way:

Server tells the client, via GCM, "hey, you need to download stuff from the server, do a HTTP request now"

Situation:

  1. Client goes offline (no internet connection) (2:00 pm)
  2. Server sends GCM message to the client (3:00 pm)
  3. Server sends GCM message to the client (5:00 pm)
  4. Server sends GCM message to the client (5:15 pm)
  5. Client is now online (has Internet connection) (6:00 pm), client receives these 3 GCM messages

Problem:
Client is going to do 3 consecutive HTTP requests.
The first request will download all the new stuff, the second and third request will be just a waste of battery.

Is there a standard best solution for this situation?

Foi útil?

Solução

Yes, there is. If all 3 messages have the same collapse key, only one of them will be delivered when the client becomes online.

From the docs :

When a 3rd-party server posts a message to GCM and receives a message ID back, it does not mean that the message was already delivered to the device. Rather, it means that it was accepted for delivery. What happens to the message after it is accepted depends on many factors.

In the best-case scenario, if the device is connected to GCM, the screen is on, and there are no throttling restrictions (see Throttling), the message will be delivered right away.

If the device is connected but idle, the message will still be delivered right away unless the delay_while_idle flag is set to true. Otherwise, it will be stored in the GCM servers until the device is awake. 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). However, if the collapse key is not set, both the new and old messages are stored for future delivery. Collapsible messages are also called send-to-sync messages.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top