我正在尝试使用C2DM注册我的设备,但遇到了重大问题。我已经参考了几个非常相似的教程进行操作。我认为问题与发送给C2DM服务器的注册意图有关。是否有人有任何建议?以下是相关代码:
清单文件:权限(在应用程序标记之外):
这是意图(Intent)注册的代码(在应用程序标签内):
以下是我称之为向C2DM服务器注册设备的代码(它启动了联系C2DM服务器的服务,该服务应该会发送一个包含我的注册ID的注册意图)。它位于名为C2DMessaging的文件中:
组件名称信息如下:
感谢MisterSquonk的回复。
清单文件:权限(在应用程序标记之外):
<!-- Used for C2DM -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.companyname.parade.permission.C2D_MESSAGE" />
这是意图(Intent)注册的代码(在应用程序标签内):
<receiver
android:name=".C2DMReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.companyname.parade" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.companyname.parade" />
</intent-filter>
</receiver>
以下是我称之为向C2DM服务器注册设备的代码(它启动了联系C2DM服务器的服务,该服务应该会发送一个包含我的注册ID的注册意图)。它位于名为C2DMessaging的文件中:
public static void register(Context context) {
Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra(EXTRA_SENDER, SENDER_ID);
ComponentName name = context.startService(registrationIntent);
if(name == null){
// FAIL!
Log.d(TAG, "FAIL");
}else{
// SUCCESS
Log.d(TAG, "Success");
}
}
组件名称信息如下:
com.google.android.gsf/com.google.android.gsf.gtalkservice.PushMessagingRegistrar
没有logcat输出。我的接收器(名为C2DMReceiver)如下:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (C2DMessaging.INTENT_REGISTRATION_CALLBACK.equals(action)) {
// Registration Intent
Log.w(TAG, "Registration Receiver called");
handleRegistration(context, intent);
} else if (action.equals(C2DMessaging.INTENT_RECEIVED_MESSAGE_CALLBACK)) {
Log.w(TAG, "Message Receiver called");
handleMessage(context, intent);
} else if (action.equals(C2DMessaging.INTENT_C2DM_RETRY)) {
C2DMessaging.register(context);
}
}
这个函数根本没有被调用。
编辑:整个问题是我的一个愚蠢的错误。在我阅读的教程中,我不知道为什么漏掉了一步。我需要将以下内容添加到我的权限中:
<permission android:name="com.companyname.parade.permission.C2D_MESSAGE" android:protectionLevel="signature" />
感谢MisterSquonk的回复。
<permission android:name="com.companyname.parade.permission.C2D_MESSAGE" android:protectionLevel="signature" />
。你在AndroidManifest.xml
中设置了吗? - Squonk