13

我想嘗試Google Cloud Messaging(GCM)服務,並且我在開始時遇到問題。Google Cloud Messaging註冊AUTHENTICATION_FAILED

嘗試向GCM註冊設備時出現錯誤AUTHENTICATION_FAILED。 我搜索了我所發現的所有密碼都是不正確的。我的密碼是正確的,我只使用一個帳戶。

有兩種方法可以實現在Android GCM客戶端:

  1. GCM庫附加罐子,現在已廢棄。
  2. 谷歌播放服務API

我開始課程的第二和得到這個問題。

我以爲問題出在我的手機上,但後來決定試試第一種方式,哪種方法奏效! 但是,它已被棄用,並需要一個額外的jar,這似乎不是正確的方式。

爲了理解錯誤的原因,我編譯了Google Play Services jar,並將其與GCM庫進行了比較。

原來,他們都有類似的方法,是這樣的:

在GCM庫,它是com.google.android.gsf,其中gsf是谷歌服務框架(我猜:

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

一行的區別),它的工作原理!

在谷歌播放服務API jar它是com.google.android.gms,它不起作用(AUTHENTICATION_FAILED錯誤)。

然後在GCM庫中,我將「gsf」替換爲「gms」並運行。我得到了同樣的AUTHENTICATION_FAILED錯誤!如果我輸入另一個包,那麼它不起作用。

我需要做些什麼才能使它工作?我應該在手機中設置一些東西嗎?或者它是Google Play服務中的錯誤?有人遇到過這樣的問題嗎?

提前致謝!

回答

1

這看起來像一個錯誤。這裏有一個Android開發者說什麼關於它的android-gcm Google Group

一些背景資料:升級Froyo和薑餅登記在 GoogleServicesFramework實現,使用谷歌賬號註冊。 這對於賬戶 未處於良好狀態的用戶會導致很多身份驗證錯誤。

從ICS開始,GCM不依賴或使用Google帳戶 - 您可以在添加帳戶或沒有任何帳戶之前使用它。

「播放服務」更新正在實施對所有 設備的新方案 - 但似乎設備少數有 這個問題,我們正在調查 - 但數字遠遠高於 具有較低舊計劃。

如果要使用GSF中的代碼,對於Froyo和薑餅 - 您需要使用以前的庫,該庫會明確設置包名。 GCM中的新庫使用新的註冊碼。

與谷歌的實際連接遵循相同的路徑 - 我們 逐漸(並慢慢)將設備移動到播放 服務中的新代碼。

到目前爲止,我有2個bugreports,我們有一些嫌疑人。我們知道 如果設備未連接超過9個月,它將處於此 狀態,並且將需要工廠重置。

我們有一些報告出廠重置沒有解決問題 - 但我沒有錯誤報告或信息來確認或追蹤此問題。 唯一的情況下,我確定了出廠重置不會幫助的地方是,如果 手機在初始登錄 中向服務器發送了錯誤信息 - 我們正在爲此添加額外的檢查。

顯然,出廠重置可能會解決問題,但他們仍在調查。

+0

謝謝,我不想做出廠重置。我會在另一臺設備上檢查它。直到情況清理完畢,我會使用舊圖書館,或者兩者兼而有之。 – user2862139

1

因此,看起來避免此問題的解決方案是回退到舊的棄用GCM客戶端庫,以防AUTHENTICATION_FAILED錯誤發生在FROYO和GINGERBREAD上。

下面是我如何升級新的GCM客戶退卻到使用舊的客戶端簡單的代碼片段:

@Override 
protected void onPostExecute(Integer resultCode) { 

    if(resultCode == EXCEPTION_THROWED) { 

     //Android 2.2 gmc bug http://stackoverflow.com/questions/19269607/google-cloud-messaging-register-authentication-failed 

     //fall back to old deprecated GCM client library 

     GCMRegistrar.checkDevice(StartActivity.this); 
     GCMRegistrar.checkManifest(StartActivity.this); 
     final String registrationId = GCMRegistrar.getRegistrationId(StartActivity.this); 
     if (registrationId.equals("")) { 
      GCMRegistrar.register(StartActivity.this, SENDER_ID); 
     } 

     //Toast.makeText(context, "Orders and menus won't be sync with other devices since GoogleCloudMessaging is not working correctly on this device. Please notify the developer.", Toast.LENGTH_LONG).show(); 

    } 

}

您可以找到老過時的GCM客戶在這裏helpher:http://developer.android.com/google/gcm/helper.html

你可以在路徑中找到計算機上GCM客戶端的代碼: ANDROID_SDK_ROOT/extras/google/gcm-client(假設您已使用Android SDK管理器下載了此額外內容)。

我把舊的gcm客戶端放到一個名爲com.google.android.gcm.deprecated的新軟件包中,試圖記住自己不要將其用於其他的東西。

2

我遇到了同樣的問題,似乎並不像谷歌急着修復它。

我不想棄用的客戶端助手gcm.jar添加到我的應用程序,所以我編寫了一個最小的解決方案,它在我的Android 2.3.6的Nexus One手機的工作原理失敗登記在上面

問題
  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異常在上面的代碼

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添加到您的應用程序)

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以確保您可以收到註冊意圖。

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

我希望這有助於谷歌一起行動!

+0

不知道我明白需要清除regcallback Pendingintent,因爲這個意圖似乎什麼都不做,而且我也沒有看到你怎麼能夠從接收到REGISTRATION意圖的廣播接收器調用那個例程? regCallback是一個靜態類變量嗎?想知道你是否可以創建它,傳遞它,然後馬上取消它,或者甚至不包括額外的意圖開始。你知道com.google.android.gsf包是否需要額外的「app」嗎? – guyland123

相關問題