如何修复 Google Cloud Messaging 注册错误:SERVICE_NOT_AVAILABLE? [英] How to fix Google Cloud Messaging Registration error: SERVICE_NOT_AVAILABLE?

查看:72
本文介绍了如何修复 Google Cloud Messaging 注册错误:SERVICE_NOT_AVAILABLE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I encountered a strange problem - I've been using GCM in my application for quite a long time and everything works perfectly. However, before a release to Google Play I changed my application package name from com.android.testapp to com.android.recognition and after this GCM stopped working. At first I got en error GCM sender id not set on constructor and fixed it by overriding getSenderIds(Context context), but now I can't get a registration ID. Here are the messages from logcat:

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

How can I fix this? When I switched to a new package I changed everything in the manifest file to the new package:

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

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

So what is the problem behind this? Can renaming the application package cause this or is there another reason?

推荐答案

这个 SERVICE_NOT_AVAILABLE 错误表示 GCM Service 在当前不可用.等待一段时间后再试.

This SERVICE_NOT_AVAILABLE error says that GCM Service is not available in current. Wait and try after some time.

这种情况经常发生(根据我的经验),所以不用担心.

This happens many time (As my experience), so don't worry about it.

请参阅 GCM Lib 的 GCMConstants 类.

See the GCMConstants class of GCM Lib.

/**
     * 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";

更多调查见GCMBaseIntentServicehandleRegistration()

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);
        }
    }

这篇关于如何修复 Google Cloud Messaging 注册错误:SERVICE_NOT_AVAILABLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆