繁体   English   中英

谷歌云消息传递(gcm)服务即使在指数补偿后也不可用

[英]google cloud messaging(gcm) service not available even after exponential backoff

总是在应用程序中获取gcm的“ gcm服务不可用”:

演示后台注册过程的代码:

private void registerInBackground() {

    new AsyncTask<Void, Void, String>(){

        @Override
        protected String doInBackground(Void... params) {
            String msg="";
            long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);

            for (int i = 1; i <= MAX_ATTEMPTS; i++) {
                Log.d(TAG, "Attempt #" + i + " to register");
                try{    
                    if (gcm == null){
                        gcm = GoogleCloudMessaging.getInstance(context);
                    }
                    //Should get registrationid from gcm server
                    //but this portion is never running and control tranfers to
                    //catch portion with error "service not available "
                    regid = gcm.register(SENDER_ID);

                    msg = "Device registered, registration ID=" + regid;

                    Log.i(TAG,msg);
                    sendRegistrationIdToBackEnd();

                    storeRegistrationId(context, regid);

                } catch(IOException e){
                    //here getting the error message service not available
                    msg = "Error: " + e.getMessage();
                    Log.e(TAG, "Failed to register on attempt " + i + ":" + e);
                    if (i == MAX_ATTEMPTS) {
                        break;
                    }
                    try {
                        Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
                        Thread.sleep(backoff);
                    } catch (InterruptedException e1) {
                        // Activity finished before we complete - exit.
                        Log.d(TAG, "Thread interrupted: abort remaining retries!");
                        Thread.currentThread().interrupt();
                        return "";
                    }
                    // increase backoff exponentially
                    backoff *= 2;
                }

            }
            return msg;
        }
        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null,null,null);

}

表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcm"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="14" />

<!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
<!-- The targetSdkVersion is optional, but it's always a good practice
     to target higher versions. -->


<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!--
 Creates a custom permission so only this app can receive its messages.

 NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
       where PACKAGE is the application's package name.
-->
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission
    android:name="com.google.android.c2dm.permission.RECEIVE" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <activity
        android:name=".DemoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.gcm" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />
</application>

</manifest>

应用程序的完整代码: https : //drive.google.com/file/d/0B4rrYgKfVJpQSDAwWU8wNl96Ylk/edit?usp=sharing

如果注册(或注销)操作是同步的,则可以在一个简单的循环中重试该操作。 但是,由于它是异步的,因此最好的方法是安排PendingIntent重试该操作。

http://developer.android.com/google/gcm/adv.html#retry

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM