C2DM dynamic receiver register using registerReceiver

I can register my Android application with C2DM successfully using <receiver>in my manifest. However, if I remove <receiver>from the manifest and register my receiver using the registerReceiver method of the context, I get an error response SERVICE_NOT_AVAILABLE. I reproduced this behavior in an emulator and in a real device.

Is C2DM receiver dynamic registration possible?

This is a snippet of the manifest that I deleted:

<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>

And here is the code:

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");
        }   
    }
}
+5
source share
3 answers

, , , . - C2DM , , C2DM. , .

+2

C2DMReceiver BroadcastReceiver .

+3

It may make sense to get the intention of c2dm / gcm REGISTERED in the dynamically registered BroadcastReceiver in Activity, but, like the original author, I found that this does not work (except for GCM on JB). Is there a reason for this? I cannot find this restriction in documents anywhere.

0
source

All Articles