Skip to content Skip to sidebar Skip to footer

Handling Backoff While Implementing C2dm In Android

I am implementing C2DM in one of the application. In case there is an error while registering for C2DM we need to backoff and then try again. I want to whether the user needs to go

Solution 1:

You need to set an alarm to retry the registration after a back off period of time You need to add the RETRY intent filter to the receiver declaration in the manifest.html and the alarm code could look something like this

if ("SERVICE_NOT_AVAILABLE".equals(error)) {
        longbackoffTimeMs= C2DMessaging.getBackoff(context);

        Log.d(TAG, "Scheduling registration retry, backoff = " + backoffTimeMs);
        IntentretryIntent=newIntent(C2DM_RETRY);
        PendingIntentretryPIntent= PendingIntent.getBroadcast(context, 
                0/*requestCode*/, retryIntent, 0/*flags*/);

        AlarmManageram= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + backoffTimeMs,
                retryPIntent);

        // Next retry should wait longer.
        backoffTimeMs *= 2;
        C2DMessaging.setBackoff(context, backoffTimeMs);
    } 

C2DMessaging is a class from jumpnote app which can be found here http://code.google.com/p/jumpnote/source/checkout

UPDATE

Make sure the RETRY permission is set in your manifest file as suggested in my response to your comment. My manifest looks something like this

<receiverandroid:name="com.google.android.c2dm.C2DMBroadcastReceiver"android:permission="com.google.android.c2dm.permission.SEND"><!-- Receive the actual message --><intent-filter><actionandroid:name="com.google.android.c2dm.intent.RECEIVE" /><categoryandroid:name="my.package.name" /></intent-filter><!-- Receive the registration id --><intent-filter><actionandroid:name="com.google.android.c2dm.intent.REGISTRATION" /><categoryandroid:name="my.package.name" /></intent-filter></receiver><receiverandroid:name="com.google.android.c2dm.C2DMBroadcastReceiver"><intent-filter><actionandroid:name="com.google.android.c2dm.intent.RETRY"/><categoryandroid:name="io.modem" /></intent-filter></receiver>

Also - This is easy to test on the emulator - Just disconnect your P.C. from the internet and you will get retry events for service not available exceptions until you re-connect to the internet

Post a Comment for "Handling Backoff While Implementing C2dm In Android"