如何修复谷歌云消息注册错误:SERVICE_NOT_AVAILABLE?

我遇到了一个奇怪的问题 - 我已经在我的应用程序中使用GCM很长一段时间了,一切都很完美。但是,在发布到Google Play之前,我将我的应用程序包名称从更改为此GCM停止工作之后。起初我得到了en错误,并通过覆盖来修复它,但现在我无法获得注册ID。以下是来自 logcat 的消息:com.android.testappcom.android.recognitionGCM sender id not set on constructorgetSenderIds(Context context)enter image description here

我该如何解决这个问题?当我切换到新包时,我将清单文件中的所有内容都更改为新包:

<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.android.recognition" />
        </intent-filter>
    </receiver>

那么这背后的问题是什么呢?重命名应用程序包是否会导致此问题,还是有其他原因?


答案 1

问题得到了解答,在我的情况下,它稍微复杂一些。

  1. 检查您的互联网连接是否有效
  2. 检查您的清单中是否具有互联网权限
  3. 确保软件包名称正确无误,如 Eran 所述
  4. 设备时间设置正确。即使一切都很完美,如果设备时钟设置不正确,它也会失败。

错误的时钟给我带来了问题。:)


答案 2

此错误表示当前不可用。等待一段时间后再试。SERVICE_NOT_AVAILABLEGCM Service

这种情况发生了很多次(根据我的经验),所以不要担心。


请参阅 GCM Lib 的类。GCMConstants

/**
     * The device can't read the response, or there was a 500/503 from the
     * server that can be retried later. The application should use exponential
     * back off and retry.
     */
    public static final String ERROR_SERVICE_NOT_AVAILABLE =
            "SERVICE_NOT_AVAILABLE";

有关更多调查,请参阅handleRegistration()GCMBaseIntentService

private void handleRegistration(final Context context, Intent intent) {
        String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
        String error = intent.getStringExtra(EXTRA_ERROR);
        String unregistered = intent.getStringExtra(EXTRA_UNREGISTERED);
        Log.d(TAG, "handleRegistration: registrationId = " + registrationId +
                ", error = " + error + ", unregistered = " + unregistered);

        // registration succeeded
        if (registrationId != null) {
            GCMRegistrar.resetBackoff(context);
            GCMRegistrar.setRegistrationId(context, registrationId);
            onRegistered(context, registrationId);
            return;
        }

        // unregistration succeeded
        if (unregistered != null) {
            // Remember we are unregistered
            GCMRegistrar.resetBackoff(context);
            String oldRegistrationId =
                    GCMRegistrar.clearRegistrationId(context);
            onUnregistered(context, oldRegistrationId);
            return;
        }

        // last operation (registration or unregistration) returned an error;
        Log.d(TAG, "Registration error: " + error);
        // Registration failed
        if (ERROR_SERVICE_NOT_AVAILABLE.equals(error)) {
            boolean retry = onRecoverableError(context, error);
            if (retry) {
                int backoffTimeMs = GCMRegistrar.getBackoff(context);
                int nextAttempt = backoffTimeMs / 2 +
                        sRandom.nextInt(backoffTimeMs);
                Log.d(TAG, "Scheduling registration retry, backoff = " +
                        nextAttempt + " (" + backoffTimeMs + ")");
                Intent retryIntent =
                        new Intent(INTENT_FROM_GCM_LIBRARY_RETRY);
                retryIntent.putExtra(EXTRA_TOKEN, TOKEN);
                PendingIntent retryPendingIntent = PendingIntent
                        .getBroadcast(context, 0, retryIntent, 0);
                AlarmManager am = (AlarmManager)
                        context.getSystemService(Context.ALARM_SERVICE);
                am.set(AlarmManager.ELAPSED_REALTIME,
                        SystemClock.elapsedRealtime() + nextAttempt,
                        retryPendingIntent);
                // Next retry should wait longer.
                if (backoffTimeMs < MAX_BACKOFF_MS) {
                  GCMRegistrar.setBackoff(context, backoffTimeMs * 2);
                }
            } else {
                Log.d(TAG, "Not retrying failed operation");
            }
        } else {
            // Unrecoverable error, notify app
            onError(context, error);
        }
    }

推荐