Get started with Firebase Phone Number Verification on Android

This page describes how to get started using Firebase Phone Number Verification in an Android app. See the overview for a general description of this feature.

By following the steps on this page, you can quickly get started implementing the user flows for Firebase PNV. For testing purposes, you will generate a testing-only token that resolves to a fake phone number. Using this test token, you can start adding Firebase PNV to your app without needing a billing account or a device with a real SIM.

Once you're satisfied with the Firebase PNV user experience in your app, you can follow some additional steps to productionize your app.

Before you begin

1. Add the Firebase PNV library to your app

In your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle.kts or <project>/<app-module>/build.gradle), add the dependency for the Firebase Phone Number Verification library for Android. We recommend using the Firebase Android BoM to control library versioning.

dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:34.12.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")
}

By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.

(Alternative)  Add Firebase library dependencies without using the BoM

If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.

Note that if you use multiple Firebase libraries in your app, we strongly recommend using the BoM to manage library versions, which ensures that all versions are compatible.

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.0.0")
}

2. Initialize the Firebase PNV library in test mode

  1. On the Testing tab of the Firebase console Phone Verification section, click the Generate token button.

  2. In your app, initialize the Firebase PNV client to use a test session:

    Kotlin

    Import the library:

    import com.google.firebase.pnv.FirebasePhoneNumberVerification
    

    Create a new instance of the FirebasePhoneNumberVerification class and use this instance for all Firebase PNV calls. The getInstance() method doesn't return a singleton object, so you must retain it after creating it.

    val fpnv = FirebasePhoneNumberVerification.getInstance(this@MainActivity)
    

    Enable a test session using the token you generated in the previous step:

    fpnv.enableTestSession("COPIED_TOKEN_STRING")
    

    You must call this method only once on an instance of FirebasePhoneNumberVerification; subsequent calls will throw an error.

Test tokens have a 7 day TTL; after this period you will need to generate a new token to enable test sessions. Test tokens work on physical devices and emulators, making them ideal for prototyping the UX in your app, or testing in CI/CD.

3. Recommended: Check for Firebase PNV support

To help you determine when to show number entry UI or explainer UI, on app launch it is recommended to check if the device and its SIM card support Firebase PNV. This is a pre-check that doesn't require user consent. You can use the result of this test to decide whether to initiate the Firebase PNV flow or to use an alternative method of phone number verification, such as SMS.

To check the device for compatibility, call the getVerificationSupportInfo() method. While you have a test session active, this method will return a list of every test token active in your project. Later, after you productionize your app, this method will return a result for each SIM in the device.

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. Initiate the verification flow

To initiate the Firebase PNV flow, call the getVerifiedPhoneNumber() method:

Kotlin

fpnv.getVerifiedPhoneNumber()
  .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.
  }

The getVerifiedPhoneNumber() method carries out the entire phone number verification flow, including:

  • Using the Android Credential Manager to acquire user consent to share their phone number.
  • Making the request to the Firebase PNV backend.
  • Returning a token containing the verified phone number for the device (in a production app, this is when billing happens).

Next steps

  • This page details how to integrate with Firebase PNV using the unified, single-call API. Calling a single method handles the entire Firebase PNV user flow, from obtaining user consent to making the necessary network calls to the Firebase PNV backend. By using this method, you reduce the integration steps to a single method call.

    This API is recommended for most developers; however, if you have specific requirements not met by the library, see the Customize the Firebase Phone Number Verification flow page for information on implementing a custom flow.

  • If you use the verified phone number outside the app client, you should pass around the token instead of the phone number itself so you can verify its integrity when you use it. See Verify Firebase PNV tokens.

  • After you have implemented and tested your app's Firebase PNV flow and backend integration, you can put your app into production to start getting real verified phone numbers. See Upgrade to production mode.