Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic register of C2DM receiver using registerReceiver

I can register my android app with C2DM successfully using a <receiver> in my manifest. However, if I delete the <receiver> from the manifest and register my receiver using the method registerReceiver of the context, I receive a SERVICE_NOT_AVAILABLE error response. I have reproduced this behaviour in the emulator and in a real device.

Is dynamically registering a C2DM receiver possible?

This is the fragment of the manifest 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", "[email protected]");
        ComponentName service = context.startService(registrationIntent);
        if(service!=null){
            Log.d("MyTestAppC2DM","C2DM Registration sent");
        }else{
            Log.d("MyTestAppC2DM","C2DM Service not found");
        }   
    }
}
like image 336
hectorct Avatar asked Apr 25 '12 19:04

hectorct


People also ask

Which of the following methods Register broadcast receiver dynamically?

Alternatively to this static registration, you can also register a receiver dynamically via the Context. registerReceiver() method. The implementing class for a receiver extends the BroadcastReceiver class.

Where we should register and deregister the broadcast?

You should register and unregister your broadcast in onResume() and onPause() methods. if you register in onStart() and unregister it in onStop().

What is c2d_ message permission?

C2D_MESSAGE – Registers the application with Android and requests permission to exclusively receive all C2D (cloud to device) messages. The package_name prefix is the same as your application ID.

What is com Google Android c2dm permission send?

android. c2dm. permission. SEND permission is held by Google Play services. This prevents other apps from invoking the broadcast receiver.


1 Answers

You can register the receiver C2DMReceiver in manifest file, and dispatch the intent to real BroadcastReceiver to handle it.

like image 199
xfx Avatar answered Oct 11 '22 17:10

xfx