也许第二眼可以帮助我。即使我从服务器发布到gcm发送网址时我收到了成功的邮件,但似乎无法在我的Receiver中获取任何内容。
清单:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="<pkgname>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="<pkgname>.permission.C2D_MESSAGE" />
<receiver
android:name="<pkgname>.GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.gcm.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<pkgname>" />
</intent-filter>
</receiver>
<service android:name="<pkgname>.GcmIntentService" />
= com.myappsname我已将其删除以保护隐私
这是服务器请求和响应:
{"registration_ids":[removed],"data":{"project":{"comment":"Chubb","username":"Dave Thomas","time":"2014-12-05 15:33:13","projectId":5}}}
{"multicast_id":removed,"success":2,"failure":0,"canonical_ids":1,"results":[{"message_id":"removed"},{"registration_id":"removed","message_id":"removed"}]}
所以服务器发送成功...但根本没有看到应用程序端的任何内容。在接收者的onReceive方法的开头加上一个断点,它并没有抓住任何东西。
接收器:
package <pkgname>;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
更新在我的日志中找到了这个,所以我知道设备实际上正在发送消息:
12-08 12:48:42.910: W/BroadcastQueue(960): Permission Denial: broadcasting Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=<pkgname> (has extras) } from com.google.android.gsf (pid=28999, uid=10007) requires com.google.android.gcm.c2dm.permission.SEND due to receiver <pkgname>/.GcmBroadcastReceiver
我想我已经找到了我的问题
答案 0 :(得分:0)
我发现你缺少两个权限
<uses-permission android:name="<pkgname>.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
并进入你的接收者声明android:authorities="<pkgname>"
<receiver
android:name="<pkgname>.GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND"
android:authorities="<pkgname>">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<pkgname>" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
问题出在我的AndroidManifest
<uses-permission android:name="com.google.android.gcm.c2dm.permission.SEND"/>
应该是:
<uses-permission android:name="com.google.android.c2dm.permission.SEND"/>