Google Cloud Messaging - messages sometimes not received until network state changed

后端 未结 5 1793
我寻月下人不归
我寻月下人不归 2020-11-28 04:40

While working on a little project that integrates with GCM, I\'ve stumbled across a bit of a strange issue.

Some times when I start watching the log to see if messag

5条回答
  •  抹茶落季
    2020-11-28 05:13

    I've noticed this as well. Although I haven't dug into the actual code, here's my understanding of why this happens.

    GCM (and most push messaging services) works by keeping a long-lived socket open to Google's push notification server. The socket is kept open by sending "heartbeat" messages between the phone and server.

    Occasionally, the network state may change and this socket will be broken (because the IP address of the device changes, from 3g to wifi, for example). If the message comes in before the socket is reestablished, then the device will not immediately get the message.

    The reconnection only happens when the phone notices the socket is broken, which only happens when it tries to send a heartbeat message.

    Again, just my basic understanding of how it works and why it happens, and I could be wrong.

提交回复
热议问题