开发者

C2DM registration retry

开发者 https://www.devze.com 2023-02-14 15:59 出处:网络
I\\m using C2DM and it\'s working fine if the registration was successfull. But sometimes registration fails and then it tries to register later:

I\m using C2DM and it's working fine if the registration was successfull. But sometimes registration fails and then it tries to register later:

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,
             backoffTimeMs, retryPIntent);

But what to do if the alarm manager fires this intent? will i have to catch it? Beca开发者_运维知识库use somehow the program never retrys to register.


First of all. The retry code supplied is WRONG! Yes, even folks from google can publish wrong code!

The am.set method (in C2DMBaseReceiver.handleRegistration) takes in the time since bootup in milliseconds that the the intent should fire at. We are passing in 30000, 60000, 120000 etc. All these values will be WELL in the past. What we should pass in is:

am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + backoffTimeMs,
                        retryPIntent);

This means we are saying the next intent should be fired at now + backOffTimeMs. This is the first bug in the published code.

The second bug is that there is no BroadcastReceiver that is wired up to receive the

com.google.android.c2dm.intent.RETRY

intent!

So, we include the following addition in the manifest file:

<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver">
    <intent-filter>
             <action android:name="com.google.android.c2dm.intent.RETRY"/>
             <category android:name="com.google.android.apps.chrometophone" />
          </intent-filter>
</receiver>

(this is an additional block, leave all other things as is)

And there you go! It will start working!

0

精彩评论

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

关注公众号