我错过了一些东西,考虑到我遵循了指南,我不确定什么,即:https://developers.google.com/cloud-messaging/android/client
我的服务器端代码是正确的,将成功发送 GCM
消息,但我的 Android 客户端没有收到任何内容。
我认为问题出在 list 中的某个地方:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.here"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<permission
android:name="com.mypackage.here.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.mypackage.here.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- C2DM service Receive -->
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.mypackage.here" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name=".EPAOAGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name=".EPAOAInstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name=".EPAOAGcmRegisterIntentService"
android:exported="false" >
</service>
<!-- C2DM service Receive End -->
</application>
</manifest>
我会注意到我正在注册并接收设备 token ,一切正常。我只是无法接收消息(通知)。如果在查看 list 时没有立即想到任何想法,如果能就我应该查看的下一个地方提出建议,我们将不胜感激。
最佳答案
简单的答案是:不用担心!这是第一次,显然需要一些时间!这归因于 Google 服务器中发生的同步。
此外,如果是通知,则可能需要长达 15-28 分钟的延迟,因为: 客户端电话上的 GCM 框架部分使用端口 5228 上的 TCP 连接。此连接用于推送通知,但作为每个 tcp 连接,它可能会与某些应用严格策略来终止不活动 tcp 连接的路由器/运营商超时( TCP 空闲超时)。
大多数 WiFi 路由器会在 5 分钟后终止不活动的连接,例如我的。
GCM 框架使用保持 Activity 机制,在 wifi 上每 15 分钟发送一次心跳网络数据包,在 3G 上每 28 分钟发送一次心跳网络数据包。
关于java - Android:无法接收 GCM 消息/监听器未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305507/