开发者

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 with the same registration intent or is there any other intent that user needs to call for retry purpose.

Below are the two intents that开发者_高级运维 I have seen in one of the code. But in case the registration fails the retry actually never occurs and I do not see any registration id being generated.

com.google.android.c2dm.intent.REGISTER com.google.android.c2dm.intent.RETRY

http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html. This is the code that is being used in the app.

Please advice as to how do we need to handle the back off.


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)) {
        long backoffTimeMs = C2DMessaging.getBackoff(context);

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

        AlarmManager am = (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

    <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
        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="my.package.name" />
        </intent-filter>
        <!-- Receive the registration id -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="my.package.name" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver">
        <intent-filter>
         <action android:name="com.google.android.c2dm.intent.RETRY"/>
         <category android: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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜