我有一个使用 C2DM 的 Android 应用程序。我在主要活动中定义了接收者(不在清单中)。我有这样的事情:
c2dmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
String registrationId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
System.out.println("receiver registration: " + registrationId);
System.out.println("unregister aplicacion: " + unregistered);
if (registrationId != null){
saveConfig("key", registrationId);
postC2dm(registrationId);
}
}
else
if (action.equals("com.google.android.c2dm.intent.RECEIVE")){
final String payload = intent.getStringExtra("payload");
System.out.println("Package : " + intent.getPackage());
}
}
};
IntentFilter filtroReceiver = new IntentFilter("com.google.android.c2dm.intent.REGISTRATION");
filtroReceiver.addAction("com.google.android.c2dm.intent.RECEIVE");
filtroReceiver.addCategory("es.unizar.c2dm");
registerReceiver(c2dmReceiver,filtroReceiver,"com.google.android.c2dm.permission.SEND",null);
在清单文件中:
<uses-permission android:name="mypackage.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND" />
我的问题是:当我通过意图“com.google.c2dm.intent.RECEIVE”收到“有效负载”时,我正确收到了数据“有效负载”,但我收到了一个 registrationId=null 并且我的类别未注册(在操作中“ com.google.c2dm.intent.registration”)。
出现的警告是:
未授予 com.google.android.c2dm.permission.SEND 对包 es.mypackage.es 的权限(protectionLevel=3 flags=0xbe46)
[DataMsgMdr] 广播意图回调:结果 = CANCELED for Intent {act=com.google.c2dm.intent.RECEIVE cat= es.mypackage.es(有附加功能)
找不到接收方包,取消注册应用程序 es.mypackage.es 发送方 sender@gm...
谢谢你。此致。