開始在 Android 上使用 Firebase 電話號碼驗證

本頁面說明如何在 Android 應用程式中開始使用 Firebase Phone Number Verification。如需這項功能的概略說明,請參閱總覽

按照本頁的步驟操作,即可快速開始實作 Firebase PNV 的使用者流程。基於測試目的,您將產生僅供測試的權杖,該權杖會解析為假的電話號碼。使用這個測試權杖,您就能開始在應用程式中加入 Firebase PNV,不需帳單帳戶或具備實體 SIM 卡的裝置。

確認應用程式的Firebase PNV使用者體驗沒有問題後,您可以按照一些額外步驟,將應用程式投入正式環境。

事前準備

1. 將 Firebase PNV 程式庫新增至應用程式

模組 (應用程式層級) Gradle 檔案 (通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle) 中,加入 Android 適用的 Firebase Phone Number Verification 程式庫依附元件。建議使用 Firebase Android BoM 控制程式庫版本管理。

dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:34.13.0"))

    // Add the dependencies for the Firebase Phone Number Verification libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-pnv")
}

只要使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。

(替代做法)  使用 BoM 新增 Firebase 程式庫依附元件

如果選擇不使用 Firebase BoM,則必須在依附元件行中指定每個 Firebase 程式庫版本。

請注意,如果應用程式使用多個 Firebase 程式庫,強烈建議使用 BoM 管理程式庫版本,確保所有版本都相容。

dependencies {
    // Add the dependencies for the Firebase Phone Number Verification libraries
    // When NOT using the BoM, you must specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-pnv:16.1.0")
}

2. 在測試模式中初始化 Firebase PNV 程式庫

  1. Firebase 控制台中,依序前往「Security」(安全性) >「Phone Verification」(電話號碼驗證) >「Testing」(測試) 分頁標籤

  2. 點選「Generate token」

    執行此操作。
  3. 在應用程式中,初始化 Firebase PNV 用戶端以使用測試工作階段:

    Kotlin

    匯入程式庫:

      import com.google.firebase.pnv.FirebasePhoneNumberVerification
      ```
    
    Create a new instance of the `FirebasePhoneNumberVerification` class and use
    this instance for all Firebase PNV calls.
    
    ```kotlin
      val fpnv = FirebasePhoneNumberVerification.getInstance()
      ```
    
    Enable a test session using the token you generated in the previous step:
    
    ```kotlin
      fpnv.enableTestSession("COPIED_TOKEN_STRING")
      ```
    
    You must call this method only once on an instance of
        `FirebasePhoneNumberVerification`; subsequent calls will throw an error.
    

測試權杖的 TTL 為 7 天,過期後您必須產生新的權杖,才能啟用測試工作階段。測試權杖適用於實體裝置和模擬器,因此非常適合用於原型設計應用程式中的使用者體驗,或在 CI/CD 中進行測試。

3. 建議:檢查Firebase PNV支援情形

為協助您判斷何時顯示號碼輸入 UI 或說明 UI,建議您在應用程式啟動時檢查裝置及其 SIM 卡是否支援 Firebase PNV。這是預先檢查,不需要使用者同意。您可以根據這項測試的結果,決定是否要啟動 Firebase PNV 流程,或是使用其他電話號碼驗證方法,例如簡訊。

如要檢查裝置是否相容,請呼叫 getVerificationSupportInfo() 方法。如果測試工作階段處於啟用狀態,這個方法會傳回清單,其中包含專案中處於啟用狀態的測試權杖單一項目。稍後,在您將應用程式投入生產後,這個方法會傳回裝置中每張 SIM 卡的結果。

Kotlin

// Check all SIMs for support.
fpnv.getVerificationSupportInfo()
.addOnSuccessListener { results ->
  if (results.any { it.isSupported() }) {
    // At least one SIM is supported; okay to call getVerifiedPhoneNumber
    // (see the next step).
  } else {
    // No SIMs are supported, so fall back to SMS verification.
  }
}
.addOnFailureListener { e ->
  // Handle error.
}

4. 啟動驗證流程

如要啟動 Firebase PNV 流程,請呼叫 getVerifiedPhoneNumber() 方法:

Kotlin

fpnv.getVerifiedPhoneNumber(this@MainActivity)
.addOnSuccessListener { result ->
  // In test mode, this phone number will have a valid country code,
  // followed by all zeros.
  val phoneNumber = result.getPhoneNumber()
  val token = result.getToken()

  // Verification successful. Send token to your backend. (See Next Steps.)
}
.addOnFailureListener { e ->
  // Handle failures, such as the user declining consent or a network error.
}

getVerifiedPhoneNumber() 方法會執行完整的電話號碼驗證流程,包括:

  • 使用 Android Credential Manager 取得使用者同意,允許分享電話號碼。
  • Firebase PNV 後端發出要求。
  • 傳回含有裝置已驗證電話號碼的權杖 (在正式版應用程式中,這時會開始計費)。

後續步驟

  • 本頁面詳細說明如何使用統一的單一呼叫 API,與 Firebase PNV 整合。呼叫單一方法即可處理整個Firebase PNV使用者流程,包括取得使用者同意聲明,以及向Firebase PNV後端發出必要的網路呼叫。使用這個方法可將整合步驟簡化為單一方法呼叫。

    建議大多數開發人員使用這個 API。不過,如果程式庫無法滿足您的特定需求,請參閱「自訂 Firebase Phone Number Verification 流程」頁面,瞭解如何實作自訂流程。

  • 如果您在應用程式用戶端以外使用已驗證的電話號碼,請傳遞權杖,而非電話號碼本身,以便在使用時驗證完整性。請參閱「驗證權杖Firebase PNV

  • 實作並測試應用程式的 Firebase PNV 流程和後端整合後,即可將應用程式投入正式環境,開始取得經過驗證的真實電話號碼。請參閱「升級為正式版模式」。