问题
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:
- Make sure you've set your
SENDER ID
you've received from Google correctly. - Make sure your device was registered with Google's GCM service correctly.
- 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.
- 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 todelay_while_idle = 0
if you want your wakelock permission to make any difference. Please read more here. - 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