如何在服务不可用时实施Google Cloud Messaging Api中的退款

时间:2015-02-09 10:55:32

标签: android google-cloud-messaging

我使用Google Cloud Messaging Api来实现GCM功能,但我需要修复服务不可用错误。为此,我正在使用退出,但我不知道线程睡眠之后..如何设置再次注册设备。

 private void registerInBackground() {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(SettingsActivity.this);
                    }
                    regid="";
                    regid = gcm.register(SENDER_ID);
                    msg = "Device registered, registration ID=" + regid;

                    // You should send the registration ID to your server over HTTP, so it
                    // can use GCM/HTTP or CCS to send messages to your app

                    sendRegistrationIdToBackend();

                    // For this demo: we don't need to send it because the device will send
                    // upstream messages to a server that echo back the message using the
                    // 'from' address in the message.

                    // Persist the regID - no need to register again.
                    storeRegistrationId(SettingsActivity.this, regid);
                } catch (IOException ex) {
                    Log.e("Error on register",""+ex);
                    msg = "Error :" + ex.getMessage();
                    ex.printStackTrace();
                    // If there is an error, don't just keep trying to register.
                    // Require the user to click a button again, or perform
                    // exponential back-off.


                    if(ex.getMessage().equals(ERROR_SERVICE_NOT_AVAILABLE))
                    {
                        try
                        {
                            for(int n=0;n<5;n++)
                            {
                                Log.e("service_error","retrying:"+(n+1)+"time");
                                Thread.sleep((1 << n) * 1000 + randomGenerator.nextInt(1001));
                               /* gcm=GoogleCloudMessaging.getInstance(SettingsActivity.this);
                                regid=gcm.register(SENDER_ID);
                                Log.e("back off reg id:",""+regid);*/

                            }
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                            Log.e("back off error",""+e);
                        }
                    }


                }
                catch (Exception e)
                {
                    Log.e("Erron on register main exp",""+e);
                    e.printStackTrace();
                }
                Log.i("reg resp",msg);
                return msg;
            }

1 个答案:

答案 0 :(得分:1)

您可以在外部try-catch上放置一个for循环,以便在register()调用失败时迭代它。一旦msg有一个值来打破循环,你就必须返回regid

实现指数退避的另一种方法是通过do-while循环。有关示例,请参阅this method。您仍然需要在try-catch语句中包含register()调用。