Android GCM 已成功发送但在某些设备上未收到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20513927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:57:33  来源:igfitidea点击:

Android GCM sent successfully but not received on some devices

androidgoogle-cloud-messaging

提问by Jee Seok Yoon

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.

在服务器端,我使用 Google 提供的 GCM 服务器 1.0.2 库。在客户端,我按照官方文档中的说明设置了 GCM 。

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).

从我从上面代码的测试中可以看出,没有错误。消息 id 存在,但错误代码名称为 null(这是成功推送的标志)。

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

我几乎尝试了所有设置。使用 TTL、折叠键、空闲时延迟设置打开和关闭进行测试。

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

哪些情况会导致阻塞(?)GCM 推送?我该如何解决这个问题?

EDIT

编辑

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

我不知道为什么,但下面的临时解决方案解决了我的问题。

In GcmIntentService#onHandleIntent just remove

在 GcmIntentService#onHandleIntent 中删除

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 documentstates that I should call completeWakeFulIntent after each work. Also, my method will drain the battery significantly.

这不是解决方案,因为该文档指出我应该在每次工作后调用 completeWakeFulIntent。此外,我的方法会显着耗尽电池电量。

Any suggestion?

有什么建议吗?

回答by Assaf Gamliel

  1. Make sure you've set your SENDER IDyou'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 = 0if 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.
  1. 确保您已SENDER ID正确设置您从 Google 收到的信息。
  2. 确保您的设备已正确注册 Google 的 GCM 服务。
  3. 确保您将推送发送到您从 Google 收到的正确注册 ID。并且您没有收到来自 Google GCM 服务的错​​误。
  4. 你设置了delay_while_idle = 1吗?这意味着如果设备处于空闲状态(关闭、离线、锁定屏幕等),消息将不会到达设备。delay_while_idle = 0如果您希望唤醒锁定权限产生任何影响,请将其更改为。请在此处阅读更多内容。
  5. 有时推送到达需要时间(但永远不会太久,那么就会出现问题)。检查您发送的推送的“生存时间”是多少。

回答by BricoleurDev

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)
  • 确保 Google Play 商店已更新到最新版本
  • 确保没有系统范围的通知设置被阻止
  • 进入应用程序管理器中的应用程序并取消选中/重新选中“通知”
  • 如果在 Wifi 上,将 Wifi 连接切换到不同的 AP(如果可能)
  • 重启设备
  • 安装“推送通知修复程序”(无论如何都要安装)

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

首次运行通知设置可能令人讨厌,并且需要以这些方式中的一种或多种方式“踢”设备。

回答by Andros

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

您是否有正确版本的 Playstore 来接收通知?您是否在这些设备上使用有效的 Google 帐户登录?

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.

当我“强制停止”一个应用程序时,我也遇到了一个问题,在“强制停止”(从 android 3.1 开始)之后你无法在任何应用程序上收到通知,所以也要小心。

回答by Dehan Wjiesekara

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

如果您使用的是 wifi,可能是 Internet 防火墙阻止了 gcm。尝试使用您的手机 SIM 卡上网。