5

<receiver>我可以使用清单中的 a 成功向 C2DM 注册我的 android 应用程序。但是,如果我从清单中删除<receiver>并使用上下文的方法 registerReceiver 注册我的接收器,我会收到SERVICE_NOT_AVAILABLE错误响应。我已经在模拟器和真实设备中重现了这种行为。

可以动态注册 C2DM 接收器吗?

这是我删除的清单片段:

<receiver android:name=".service.C2DM.C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <category android:name="mytestapp" />
  </intent-filter>
  <intent-filter>
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
      <category android:name="mytestapp" />
  </intent-filter>
</receiver>

这是代码:

public class C2DMReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            //TODO: Registration failed, should try again later.
            Log.e("MyTestAppC2DM", "C2DM Error = " + intent.getStringExtra("error"));
        } else if (intent.getStringExtra("unregistered") != null) {
            Log.d("MyTestAppC2DM", "C2DM Unregistered");
        } else if (registration != null) {
            Log.d("MyTestAppC2DM","C2DM registration_id = " + registration);
        } else {
            Log.w("MyTestAppC2DM", "C2DM No registration_id");
        }
    }

    public static void register(Context context){
        /* BEGIN OF DYNAMIC REGISTER */
        C2DMReceiver c2dmReceiver = new C2DMReceiver();

        IntentFilter receiveIntentFilter = new IntentFilter();
        receiveIntentFilter.addAction("com.google.android.c2dm.intent.RECEIVE");
        receiveIntentFilter.addCategory("mytestapp");
        context.registerReceiver(c2dmReceiver, receiveIntentFilter, "com.google.android.c2dm.permission.SEND", null);

        IntentFilter registrationIntentFilter = new IntentFilter();
        registrationIntentFilter.addAction("com.google.android.c2dm.intent.REGISTRATION");
        registrationIntentFilter.addCategory("mytestapp");
        context.registerReceiver(c2dmReceiver, registrationIntentFilter, "com.google.android.c2dm.permission.SEND", null);

        /*END OF DYNAMIC REGISTER*/

        Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
        registrationIntent.putExtra("sender", "mysender@gmail.com");
        ComponentName service = context.startService(registrationIntent);
        if(service!=null){
            Log.d("MyTestAppC2DM","C2DM Registration sent");
        }else{
            Log.d("MyTestAppC2DM","C2DM Service not found");
        }   
    }
}
4

3 回答 3

3

您可以在清单文件中注册接收器 C2DMReceiver,并将意图分发给真正的 BroadcastReceiver 来处理它。

于 2012-05-23T10:36:04.820 回答
2

当系统发送广播时,如果应用程序在清单中声明了匹配的意图过滤器,它将启动应用程序来处理意图。由于 C2DM 的性质,动态安装广播接收器没有用处,因为收到 C2DM 消息时您的应用程序可能未运行。如果它没有运行,它将不会为动态安装的接收器启动。

于 2012-06-19T11:54:20.300 回答
0

在 Activity 中动态注册的 BroadcastReceiver 中接收c2dm/gcm REGISTERED意图可能是有意义的,但就像原作者一样,我发现这不起作用(JB 上的 GCM 除外)。是否有一个原因 ?我在任何地方的文档中都找不到这个限制。

于 2012-07-22T00:16:53.897 回答