Google Cloud Messaging 注册 AUTHENTICATION_FAILED [英] Google Cloud Messaging register AUTHENTICATION_FAILED

本文介绍了Google Cloud Messaging 注册 AUTHENTICATION_FAILED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试 Google Cloud Messaging (GCM) 服务,一开始我遇到了问题.

I want to try Google Cloud Messaging (GCM) service, and I am faced with a problem at the beginning.

我在尝试将设备注册到 GCM 时遇到错误 AUTHENTICATION_FAILED.我进行了搜索,发现的都是错误密码的变体.我的密码正确,而且我只使用一个帐户.

I get an error AUTHENTICATION_FAILED while trying to register a device to GCM. I searched and all I found were variations of the incorrect password. My password is correct and I am using just one account.

在Android上实现GCM客户端有两种方式:

There are two ways to implement GCM client on Android:

  1. 带有额外 jar 的 GCM 库,现已弃用.
  2. Google Play 服务 API

我当然从第二个开始并遇到了这个问题.

I started with the second of course and got this issue.

我以为问题出在我的手机上,但后来决定尝试第一种方法,它奏效了!但是,它已被弃用并且需要一个额外的 jar,这似乎不是正确的方法.

I thought the problem is in my phone, but then decided to try the first way, which worked! However, it is deprecated and requires an additional jar, which doesn't seem like the right way.

为了了解错误原因,我反编译了 Google Play Services jar 并与 GCM 库进行了比较.

In an attempt to understand the reasons for the error, I decompiled Google Play Services jar and compared it with GCM library.

事实证明他们都有类似的方法,例如:

It turns out they both have a similar method, something like:

void register(Context context, String senderIds) {
    Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
    intent.setPackage("com.google.android.gms"); // this one row are different
    setPackageNameExtra(context, intent);
    intent.putExtra("sender", senderIds);
    context.startService(intent);
}

一行的区别:

在 GCM 库中,它是 com.google.android.gsf,其中 gsf 是 Google 服务框架(我猜),它可以工作!

In GCM library it is com.google.android.gsf, where gsf is Google Services Framework (I guess), and it works!

在 Google Play 服务 API jar 中,它是 com.google.android.gms,并且它不起作用(AUTHENTICATION_FAILED 错误).

In Google Play Services API jar it is com.google.android.gms, And it does not work (AUTHENTICATION_FAILED error).

然后在 GCM 库中,我将gsf"替换为gms"并运行.我得到了同样的 AUTHENTICATION_FAILED 错误!如果我输入另一个包,则它不起作用.

Then in GCM library I replaced "gsf" to "gms" and run. And I got the same AUTHENTICATION_FAILED error! If I enter another package, then it is not working.

我需要做什么才能让它发挥作用?我应该在手机中设置一些东西吗?还是 Google Play 服务中的错误?有人遇到过这样的问题吗?

What do I need to do to make it work? Should I set up something in the phone? Or is it a bug in Google Play Services? Have someone encountered such a problem?

提前致谢!

推荐答案

我遇到了同样的问题,而且 google 似乎并不急于解决它.

I ran into the same problem, and it doesn't seem like google is in any hurry to fix it.

我不想将已弃用的客户端助手 gcm.jar 添加到我的应用程序中,因此我编写了一个最小解决方案,该解决方案适用于我的 Android 2.3.6 Nexus One 手机,但如上述问题所述,注册失败

I didn't want to add the deprecated client helper gcm.jar to my app, so I coded a minimal solution that works on my Android 2.3.6 Nexus One phone that fails registration as in the question above

            try {
                gcm = GoogleCloudMessaging.getInstance(context);
                regID = gcm.register(SENDER_ID);
                storeRegistrationId(regID);
                msg = "Device registered, registration ID=" + regID;

                sendRegistrationIdToBackend();
            } catch (IOException ex) {
                msg = "Exception registering for GCM :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                oldSchoolRegister();
            }

AUTHENTICATION_FAILED 触发了上面代码中的 IOException

The AUTHENTICATION_FAILED triggers the IOException in the code above

private void oldSchoolRegister() {
    Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
    intent.setPackage("com.google.android.gsf");
    setRegCallbackIntent(context, intent);
    intent.putExtra("sender", SENDER_ID);
    context.startService(intent);
}

private static synchronized void setRegCallbackIntent(Context context, Intent intent) {
    regCallback = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
    intent.putExtra("app", regCallback);
}

public static synchronized void cancelRegCallbackIntent() {
    if (regCallback != null) {
        regCallback.cancel();
        regCallback = null;
    }
}

我将上述代码添加到我的应用程序中.它们是来自 Client Helper gcm.jar 的简化方法(因此您无需将 jar 添加到您的应用程序中)

I added the above code to my app. They are simplified methods from the Client Helper gcm.jar (so you don't need to add the jar to your app)

protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    if (extras != null && !extras.isEmpty()) {  // has effect of unparcelling Bundle
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (messageType != null) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                showMessage(extras.getString("message")); // call your code
                Logger.d(TAG, "Received message: " + message.alert + ": " + message.url);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                Logger.e(TAG, "Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                Logger.e(TAG, "Deleted messages on server: " + extras.toString());
            }
        } else {
            String regID = extras.getString("registration_id");
            if (regID != null && !regID.isEmpty()) {
                doRegistration(regID); // send to your server etc.
                GCMSetup.storeRegistrationId(regID);
                GCMSetup.cancelRegCallbackIntent();
            }
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

此代码在意图服务中,有几行用于存储从 GCM 接收到的 ID.正如您所看到的,与基本实现相比,仅增加了大约 20 行代码,并且没有额外的依赖项!您只需要更新您的 AndroidManifest.xml 以确保您可以收到 REGISTRATION Intent.

This code is in the intent service, and has a few lines to store the ID received from GCM. As you can see only about 20 extra lines of code compared to a basic implementation, and no additional dependencies! You only need to update your AndroidManifest.xml to make sure you can receive the REGISTRATION intent.

    <receiver android:name="com.camiolog.android.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.camiolog.android"/>
        </intent-filter>
    </receiver>

我希望这会有所帮助,直到谷歌联合起来!

I hope this helps until google gets their act together!

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

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