【问题标题】:Can not receive gcm message无法接收 gcm 消息
【发布时间】:2015-11-24 09:55:29
【问题描述】:

我遵循这个最新的教程https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging

但是我的接收器没有收到 gcm 消息。

我很确定 gcm 发送成功,因为来自服务器端的响应是

{
 "multicast_id":8234052701190658472,
 "success":1,
 "failure":0,
 "canonical_ids":0,
 "results":
     [
      {
       "message_id":"0:1448357690225369%9aaa71e7f9fd7ecd"
      }
     ]
 }

这是我的清单

<uses-permission
    android:name="com.my.app.permission.C2D_MESSAGE" />

    <!-- [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" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.my.app" />
        </intent-filter>
    </receiver>
    <!-- [END gcm_receiver] -->

    <!-- [START gcm_listener] -->
    <service
        android:name="com.my.app.MyGcmListenerService"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->
    <!-- [START instanceId_listener] -->
    <service
        android:name="com.my.app.MyInstanceIDListenerService"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!-- [END instanceId_listener] -->

这里是 MyGcmListenerService.java。但是,它永远不会被调用。

public class MyGcmListenerService extends GcmListenerService {

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
     // [START receive_message]
     @Override
     public void onMessageReceived(String from, Bundle data) {
         String message = data.getString("data");
         Log.d(TAG, "From: " + from);
         Log.d(TAG, "Message: " + message);
     }
}

这是我的 gcm 依赖项。

compile 'com.android.support:support-annotations:23.0.1'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
compile 'com.android.support:appcompat-v7:23.0.0'

为什么我的安卓应用收不到gcm?

谢谢!

【问题讨论】:

  • 您是否连接到互联网并添加互联网权限?
  • 是的,我有上网权限。
  • 我也遇到了与 nexus 设备相同的问题。如果可能,请尝试在不同的设备上进行测试。
  • 您是否添加了元数据:
  • 没有。这是必要的吗? @mdtuyen

标签: android google-cloud-messaging


【解决方案1】:

下面的代码对我有用

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.push.gcm">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.push.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.push.gcm.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <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.push.gcm" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.push.gcm" />
            </intent-filter>
        </receiver>

        <service
            android:name=".services.GCMInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name=".services.GCMListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
    </application>

</manifest>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多