在 Android 中实现 C2DM 时处理退避

发布于 2024-12-08 04:22:42 字数 464 浏览 1 评论 0原文

我正在其中一个应用程序中实现 C2DM。如果注册 C2DM 时出现错误,我们需要退后,然后重试。我想知道用户是否需要采用相同的注册意图,或者用户是否需要调用任何其他意图以进行重试。

以下是我在其中一个代码中看到的两个意图。但如果注册失败,重试实际上永远不会发生,并且我没有看到生成任何注册 ID。

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

http://www.vogella.de/文章/AndroidCloudToDeviceMessaging/article.html。这是应用程序中使用的代码。

请建议我们如何处理回退。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2024-12-15 04:22:42

您需要设置闹钟,以便在退避一段时间后重试注册
您需要将 RETRY 意图过滤器添加到 manifest.html 中的接收器声明中,警报代码可能看起来像这样

    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 是来自 Jumpnote 应用程序的一个类,可以在此处找到 http://code.google.com/p/jumpnote/source/checkout

更新

确保重试权限设置在您的清单文件,如我对您的评论的回复中所建议的。我的清单看起来像这样

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

另外 - 这很容易在模拟器上测试 - 只需断开您的电脑与互联网的连接,您将收到服务不可用异常的重试事件,直到您重新连接到互联网

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文