GCM doesn't work on Android 4.2.2?

2019-05-06 20:17发布

问题:

I have a peculiar problem with GCM: it doesn't seem to send pushes to phones that are running on Android 4.2.2. I have eight phones that I test on, ranging from 2.2 to 4.2.2, and the one running 4.2.2 was not receiving pushes. We sent out the apk to some contacts who have 4.2.2 phones, and they were able to confirm that only on the 4.2.2 phones push failed. All in all, we tested our app on over 15 devices, five of which were on 4.2.2, and it was only those five that could not receive pushes.

The devices that could not receive pushes were:

  • Samsung Galaxy S4
  • Samsung Galaxy Nexus
  • 2 x LG Nexus 4
  • A dev phone from the Razor M family

After investigating the logs, the phone is registering fine with the GCM servers, complete with a registration id. However, the onMessage just never gets triggered. Again, it works on all of our other phones which are not running 4.2.2. I'm not sure what would cause this API-specific behavior; my target API is set to 17, which I believe is 4.2.

My other suspicion is that because all of these phones that are running 4.2.2 are either not out on the market yet, or come factory unlocked, that might have to do with the issue. I haven't been able to investigate this suspicion yet because 4.2.2 has not been rolled out by AT&T and T-Mobile yet, and Verizon just started doing incremental rollouts starting yesterday (May 21). I don't really see how a 4.2.2 stock ROM could be the cause of GCM not working, however.

I tried doing some research on this issue, but nobody seems to be having any issues. All I get on SO and the google when I search for GCM and android 4.2.2 are issues with the new bluetooth stack and the shortened battery life with the update. I am pretty convinced I must be doing something wrong because other apps seem to send pushes no problem, but I am not sure where to start investigating. If somebody could point me in the right direction, it would be much appreciated.


EDIT: So found out I had turned off logging on the server side by accident, so GCM actually does return an error on send. I get back a 'NotRegistered' error on the 4.2.2 devices, even though the registration process on the client throws no errors and even returns a registration id. Again, pushes to devices not on Android 4.2.2 work fine.

回答1:

I fixed this issue by setting the 'delay_while_idle' flag to false, thanks to a hint in this thread: https://groups.google.com/forum/#!topic/android-gcm/CimKwgaf2ws

In 4.2.2, google changed GCM to set the delay_while_idle flag to true by default, which apparently causes push to not make a notification sound when the screen is off, or in my case, cause push to not work at all!

I hope this helps anybody having similar issues.