通知測試人員有關新版本的信息

可選的 Firebase App Distribution iOS 和 Android SDK 可讓您在應用程序的新版本可供安裝時向測試人員顯示應用程序內警報。本指南介紹瞭如何使用 App Distribution iOS 和 Android SDK 為測試人員創建和自定義新的構建警報。

在你開始之前

如果您尚未將 Firebase 添加到您的 Android 項目中,請將其添加到您的 Android 項目中。

第 1 步:啟用 App Distribution Tester API

  1. Google Cloud Console中選擇您的項目。

  2. 在 Firebase App Testers API 下,單擊啟用

第 2 步:將應用程序分發添加到您的應用程序

App Distribution Android SDK 由兩個庫組成:

  • firebase-appdistribution-api - 僅 API 庫,您可以將其包含在所有構建變體中。
  • firebase-appdistribution - 完整的 SDK 實現(可選)。

僅 API 庫允許您的代碼調用 SDK。如果不存在完整的 SDK 實現,則調用將無效。

模塊(應用程序級) Gradle 文件(通常<project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle中聲明 App Distribution Android SDK 的依賴項)。為了避免在 Play 構建中包含完整 SDK 實現的自更新功能,請將僅 API 庫依賴項添加到所有構建變體中。僅將完整的 SDK 實現添加到專門用於預發布測試的變體中:

Kotlin+KTX

dependencies {
    // ADD the API-only library to all variants
    implementation("com.google.firebase:firebase-appdistribution-api-ktx:16.0.0-beta10")

    // ADD the full SDK implementation to the "beta" variant only (example)
    betaImplementation("com.google.firebase:firebase-appdistribution:16.0.0-beta10")
}

Java

dependencies {
    // ADD the API-only library to all variants
    implementation("com.google.firebase:firebase-appdistribution-api:16.0.0-beta10")

    // ADD the full SDK implementation to the "beta" variant only (example)
    betaImplementation("com.google.firebase:firebase-appdistribution:16.0.0-beta10")
}

第 3 步:配置應用內警報

App Distribution Android SDK 提供了以下方法來為測試人員設置應用內構建警報:

  • 基本警報配置附帶預構建的應用程序更新和登錄對話框以顯示給測試人員。
  • 高級警報配置允許您自定義自己的用戶界面。

如果您是第一次使用 App Distribution Android SDK,我們建議使用基本配置

基本配置

使用updateIfNewReleaseAvailable向尚未啟用警報的測試人員顯示預構建的啟用警報對話框,然後檢查新版本是否可用。調用時,該方法會執行以下序列:

  1. 檢查測試人員是否啟用了警報。如果測試人員尚未啟用警報,該方法會提示測試人員使用其 Google 帳戶登錄 App Distribution。

  2. 檢查是否有新的可用版本供測試人員安裝。

  3. 顯示預先構建的警報,提示測試人員進行更新。

  4. 如果新版本是 Android App Bundle (AAB),則將測試人員重定向到 Google Play 以完成更新過程。

    如果新版本是 Android 應用程序 PackKage (APK),則 SDK 會在後台下載新版本,並在下載完成後提示測試人員進行安裝。 SDK 使用NotificationManager向用戶發送下載進度通知。您還可以通過將onProgressUpdate處理程序附加到updateIfNewReleaseAvailable任務來添加自己的進度指示器。

您可以在應用程序中隨時調用updateIfNewReleaseAvailable 。例如,您可以在應用程序主 Activity 的onResume方法期間調用updateIfNewReleaseAvailable

以下示例檢查測試人員是否啟用了警報並有權訪問新版本。如果滿足這些條件,則當構建可供安裝時會顯示一個對話框:

Kotlin+KTX

// Copy and paste this into any part of your app - for example, in your main
// activity's onResume method.
val firebaseAppDistribution = FirebaseAppDistribution.getInstance()
firebaseAppDistribution.updateIfNewReleaseAvailable()
    .addOnProgressListener { updateProgress ->
      // (Optional) Implement custom progress updates in addition to
      // automatic NotificationManager updates.
    }
    .addOnFailureListener { e ->
      // (Optional) Handle errors.
      if (e is FirebaseAppDistributionException) {
        when (e.errorCode) {
          Status.NOT_IMPLEMENTED -> {
            // SDK did nothing. This is expected when building for Play.
          }
          else -> {
            // Handle other errors.
          }
        }
      }
    }

Java

// Copy and paste this into any part of your app - for example, in your main
// activity's onResume method.
FirebaseAppDistribution firebaseAppDistribution = FirebaseAppDistribution.getInstance();
firebaseAppDistribution.updateIfNewReleaseAvailable()
    .addOnProgressListener(updateProgress -> {
      // (Optional) Implement custom progress updates in addition to
      // automatic NotificationManager updates.
    })
    .addOnFailureListener(e -> {
      // (Optional) Handle errors.
      if (e instanceof FirebaseAppDistributionException) {
        switch (((FirebaseAppDistributionException)e).getErrorCode()) {
          case NOT_IMPLEMENTED:
            // SDK did nothing. This is expected when building for Play.
            break;
          default:
            // Handle other errors.
            break;
        }
      }
    });

高級配置

高級登錄配置

方法signInTesterisTesterSignedIn使您可以更靈活地自定義測試人員的登錄體驗,以便測試人員的體驗可以更好地匹配您的應用程序的外觀和感覺。

以下示例檢查測試人員是否已登錄其 App Distribution 測試人員帳戶。這使您可以選擇僅向尚未登錄的測試人員顯示登錄用戶界面 (UI)。測試人員登錄後,您可以調用updateIfNewReleaseAvailable來檢查測試人員是否有權訪問新版本。

Kotlin+KTX

// Only show sign-in UI if this is the "beta" variant (example).
if (BuildConfig.BUILD_TYPE == "beta" && !firebaseAppDistribution.isTesterSignedIn) {
    // Start your sign-in UI here.
}

// Only check for updates if the tester is already signed in (do not prompt).
if (firebaseAppDistribution.isTesterSignedIn) {
    firebaseAppDistribution.updateIfNewReleaseAvailable().addOnFailureListener {
        // Handle failed update.
    }
}

Java

// Only show sign-in UI if this is the "beta" variant (example).
if (BuildConfig.BUILD_TYPE == "beta" && !firebaseAppDistribution.isTesterSignedIn()) {
    // Start your sign-in UI here.
}

// Only check for updates if the tester is already signed in (do not prompt).
if (firebaseAppDistribution.isTesterSignedIn()) {
    firebaseAppDistribution.updateIfNewReleaseAvailable().addOnFailureListener( e -> {
        // Handle failed update.
    });
}

在您的登錄 UI 中,當測試人員選擇繼續時,調用signInTester()

Kotlin+KTX

firebaseAppDistribution.signInTester().addOnSuccessListener {
  // Handle successful sign-in.
}.addOnFailureListener {
  // Handle failed sign-in.
});

Java

firebaseAppDistribution.signInTester().addOnSuccessListener( unused -> {
  // Handle successful sign-in.
}).addOnFailureListener(e -> {
  // Handle failed sign-in.
});

高級更新配置

當提示測試人員更新時,方法checkForNewReleaseupdateApp為您提供了更大的定制靈活性。您還可以自定義預構建的更新對話框和下載進度指示器,以便它們更好地匹配您的應用程序的外觀和感覺。

請注意, updateApp不提供下載進度指示。這意味著您需要使用NotificationManager 、某種應用內狀態顯示或其他方法來實現自己的進度指示。

以下示例檢查新版本是否可用,然後顯示自定義 UI。在調用checkForNewReleaseupdateApp之前,請確保測試人員已使用高級登錄配置登錄

Kotlin+KTX

firebaseAppDistribution.checkForNewRelease().addOnSuccessListener { release ->
    if (release != null) {
        // New release available. Start your update UI here.
    }
}.addOnFailureListener {
    // Handle failed check for new release. Fails with Status#NOT_IMPLEMENTED
    // if built for Play.
}

Java

firebaseAppDistribution.checkForNewRelease().addOnSuccessListener(release -> {
    if (release != null) {
        // New release available. Start your update UI here.
    }
}).addOnFailureListener(e -> {
    // Handle failed check for new release. Fails with Status#NOT_IMPLEMENTED
    // if built for Play.
});

當測試人員選擇從更新 UI 繼續更新時,調用updateApp()

Kotlin+KTX

firebaseAppDistribution.updateApp()
    .addOnProgressListener { updateState ->
      // Use updateState to show update progress.
    }

Java

firebaseAppDistribution.updateApp()
    .addOnProgressListener(updateState -> {
      // Use updateState to show update progress.
    });

第 4 步:構建並測試您的實施

通過使用 Firebase 控制台將構建分發給測試人員來構建您的應用並測試您的實現。

請訪問應用程序分發故障排除指南以獲取常見問題的幫助,例如:

  • 測試人員未收到應用內警報
  • 測試人員多次被提示登錄 Google