我按照最新的教程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'
为什么我的Android应用程序没有收到gcm?
谢谢!
答案 0 :(得分: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>