Android GCM sent successfully but not received on some devices

匆匆过客 提交于 2019-12-17 06:36:08

问题


On the server side, I am using GCM server 1.0.2 library provided by Google. On the client side, I set up GCM as provided on the official documentation.

My problem is that everything works fine on most devices, but on few devices, push is not recieved.

if (case1)
    message = new Message.Builder()
        .timeToLive(0)
        .collapseKey("0")
        .delayWhileIdle(false)
        .addData("msg", msg).build();
else if (case2)
    message = new Message.Builder()
        .collapseKey("2")
        .addData("msg", msg).build();
else
    message = new Message.Builder().addData("msg", msg).build();

Result result = sender.sendNoRetry(message, regId);
System.out.println("Message ID:"+result.getMessageId());
System.out.println("Failed:" + result.getErrorCodeName());

From what I can see from tests with the above code, there are no error. The message id is present, but error code name is null(which is a sign of successful push).

I've tried almost every setting. Tested with TTL, collapse key, delay while idle set on and off.

What are some cases that can cause to block(?) GCM push? And how can I resolve this?

EDIT

I have no idea why but the temporary solution below solved my problem.

In GcmIntentService#onHandleIntent just remove

GcmBroadcastReceiver.completeWakefulIntent(intent);

This line releases the wakeful service. I am curious though because on other devices, push messages are sent continuously even when this line was not removed.

This is not a solution because this document states that I should call completeWakeFulIntent after each work. Also, my method will drain the battery significantly.

Any suggestion?


回答1:


  1. Make sure you've set your SENDER ID you've received from Google correctly.
  2. Make sure your device was registered with Google's GCM service correctly.
  3. Make sure you are sending the push to the correct reg id you've received from Google. and that you didn't receive an error from Google GCM service.
  4. Have you set the delay_while_idle = 1? This means the message won't reach the device if it's idle (off, offline, locked screen, etc...). Change it to delay_while_idle = 0 if you want your wakelock permission to make any difference. Please read more here.
  5. Some times it takes time for the push to arrive (but never too much time, then there is a problem). Check what's the "time to live" of the push you've sent.



回答2:


Do you have the correct version of the Playstore to receive the notification ? Are you logged with a functional Google Account on those devices ?

I also had a problem when I "Force stop" an app, you can't receive notification on any app after a "force stop" (start with android 3.1) so be careful with that too.




回答3:


if you are using wifi, may be the internet firewall block the gcm. try to use internet with your mobile sim card.




回答4:


Checklist

  • Make sure Google Play Store is updated to latest version
  • Make sure there are no systemwide settings for notifications which are blocking
  • Go into app in Application Manager and uncheck/recheck 'Notifications'
  • If on Wifi, switch Wifi connection to a different AP (if possible)
  • Reboot the device
  • Install 'Push Notification Fixer' (install this anyway)

First-run notification setup can be annoyingly arbitrary and require the device be 'kicked' in one or more of these ways.



来源:https://stackoverflow.com/questions/20513927/android-gcm-sent-successfully-but-not-received-on-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!