Why do Android C2DM push messages not always arrive?
I've built a functioning C2DM app and generally it works very well, and the push messages 开发者_JAVA技巧arrive fast. However I find that fairly regularly when I first start the app or bring it back to focus, messages do not arrive.
They are definitely sent successfully (I get a 200 response) and the message format is certainly right (sending the same message later works fine). The failure can last for a small number of minutes, and then suddenly it starts working again, and works consistently thereafter.
However even after it starts working, the older messages have apparently disappeared and never arrive. The messages are definitely not arriving - I have extensive logging that highlights any C2DM messages that arrive.
I'm fully aware that C2DM will never guarantee delivery, so I expect some messages will fail, but in my experience, on some occasions, it just doesn't work. I obviously want to understand if that is something I am doing wrong in my app, or the C2DM service itself.
What could cause C2DM to fail to deliver messages? If messages cannot be delivered, why is the C2DM server not trying again from its queue? Related - should I get a new Registration ID only when the app starts the first time, or refresh it each time the app starts? I currently do the latter (in the Application, not an Activity).
I realised I made a schoolboy error. I was re-registering with C2DM every time the app started. That process sometimes takes quite a few seconds, and during that time the app will not receive push messages sent to the old registration ID. This explains why push messages were particularly likely to fail to be received immediately after starting the app.
The solution was to ensure the registration ID is only requested if the app does not already have one. Push messages are now working much more reliably.
This is most likely the correct behaviour. Messages are not guarantied to be delivered at all. Let alone instantly.
What it most likely is is that the connection between your phone and the Google servers is being dropped and it takes a little while to come back up.
精彩评论