Android:无法接收 GCM 消息/监听器未被调用

Android: Unable to receive GCM Messages/ Listener not being called

我遗漏了一些东西,我不确定考虑到我遵循了指南,即:https://developers.google.com/cloud-messaging/android/client

并检查了我的代码:https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart

我的服务器端代码是正确的,将成功发送一条 GCM 消息,但我的 android 客户端没有收到任何东西。

我认为问题出在清单中的某处:

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>

我会注意到我正在注册和接收设备令牌,一切正常。我就是收不到消息(通知)。如果在浏览清单时没有立即想到什么,如果能就我应该查看的下一个地方提出建议,我们将不胜感激。

简单的回答是:不用担心!第一次显然需要一些时间!它归因于 Google 服务器中发生的同步。

此外,如果它的通知可能需要 15-28 分钟的延迟,因为: 客户端 phone 上的 GCM 框架部分在端口 5228 上使用 TCP 连接。此连接用于推送通知,但作为每个 tcp 连接,它可能会因某些 routers/carriers 应用严格策略而超时终止不活动的 tcp 连接(tcp 空闲超时)。

大多数 wifi 路由器会在 5 分钟后终止非活动连接,例如我的。

GCM 框架使用 keep-alive 机制,在 wifi 上每 15 分钟发送一个心跳网络数据包,在 3G 上每 28 分钟发送一个心跳网络数据包。

你应该写你的包裹的名字是 com.mutalminds.epaoa 而不是 com.mypackage.here 。