android - 设备停止接收 GCM 推送,直到网络切换

标签 android push-notification google-cloud-messaging

我有一个依赖 GCM 推送通知的 Android 应用程序,有时,用户的设备会停止接收推送通知。这种行为是间歇性的:在某些设备上,它永远不会失败,只会持续工作数周或数月,而在其他设备上,它会工作几天,然后随机停止接收推送。

每次设备停止工作时,我都会验证设备是否具有来自 GCM 的有效 regId,并且我的服务器正在向该 regId 发送推送。但是,日志显示设备没有收到任何东西。以下是正常推送接收的日志:

05-15 10:39:21.091: V/GCMBroadcastReceiver(13766): onReceive: com.google.android.c2dm.intent.RECEIVE
05-15 10:39:21.091: V/GCMBroadcastReceiver(13766): GCM IntentService class: com.medigram.v2.GCMIntentService
05-15 10:39:21.091: V/GCMBaseIntentService(13766): Acquiring wakelock
05-15 10:39:21.111: V/GCMBaseIntentService(13766): Intent service name: GCMIntentService-468134393228-13
05-15 10:39:21.131: V/GCMBaseIntentService(13766): Releasing wakelock
05-15 10:39:21.171: V/GCMBroadcastReceiver(13766): onReceive: com.google.android.c2dm.intent.RECEIVE
05-15 10:39:21.171: V/GCMBroadcastReceiver(13766): GCM IntentService class: com.medigram.v2.GCMIntentService
05-15 10:39:21.171: V/GCMBaseIntentService(13766): Acquiring wakelock
05-15 10:39:21.241: V/GCMBaseIntentService(13766): Intent service name: GCMIntentService-468134393228-14
05-15 10:39:21.271: V/GCMBaseIntentService(13766): Releasing wakelock

这些都不会出现在已停止接收推送的设备上,因此,我的 GCMIntentService 的 onMessage() 方法也不会被调用。

奇怪的是,当我将设备连接从 3G/4G 切换到 wifi 或从 wifi 切换到 wifi 时,它再次开始工作,并且设备立即获得所有积压的推送。到底发生了什么连接类型何时更改?

如果有帮助,以下是我在推送可手动重现问题但无济于事的设备上尝试过的一些方法:

-我首先认为这可能是上下文问题,但当简单地切换网络就解决了问题时,很快就排除了这个问题。无论如何,我尝试通过使用“不保留 Activity ”开发人员选项然后将应用后台化来进一步调查,但这对推送没有影响。

-我已经尝试强制停止该应用程序以查看它是否会导致推送停止工作,但该问题似乎与该应用程序被强制停止没有任何关系,因为它一直在工作。

-我试过将手机置于飞行模式并返回,但推送仍然有效。

如果有人能指出正确的方向,我将不胜感激,因为我已经用尽了 stackoverflow 和 google 上的所有资源。我仍然像刚开始时一样对这个问题一无所知。

最佳答案

通常,Android 在移动连接上每 28 分钟发送一次心跳,在 Wifi 上每 15 分钟发送一次心跳,因此您可以通过在您的应用中以更短的间隔广播这两个 Intent 来解决此问题。

com.google.android.intent.action.MCS_HEARTBEAT

com.google.android.intent.action.GTALK_HEARTBEAT

        Intent intent = new Intent("com.google.android.intent.action.MCS_HEARTBEAT");
        Intent intent1 = new Intent("com.google.android.intent.action.GTALK_HEARTBEAT");
        sendBroadcast(intent);
        sendBroadcast(intent1);

详细可以看here

关于android - 设备停止接收 GCM 推送,直到网络切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572067/

相关文章:

android - 我在构建 gradle 时遇到这个问题,如何解决?

android - RenderScript 阻塞函数调用

ios - 如何在 AppDelegate 之外处理 UIApplicationDidRegisterToRemoteNoticationsWithDeviceToken?

java - 安排通知?

android - 是否有用于自定义 android 构建的 Google Cloud Messaging 的替代方案?

java - 无法获取提供商 com.google.android.gms.measurement.AppMeasurementContentProvider

android - 无法在 Eclipse 中安装 Android ADT 插件 - "error occurred while uninstalling"

Android/AOSP Android.mk 构建和支持-v4 AAR 等

ios - Azure 移动应用程序 Node.js 后端 - 推送通知错误状态代码 400

cordova - 在AppBrowser中更改cordova的url