通知測試人員新版本


您可以使用選用的 Firebase App Distribution iOS 和 Android SDK,在應用程式有新版本可供安裝時,向測試人員顯示應用程式內快訊。本指南說明如何使用 App Distribution iOS 和 Android SDK,為測試人員建立及自訂新版本快訊。

事前準備

如果您尚未將 Firebase 新增至 iOS 專案,請先新增。

步驟 1:啟用 App Distribution Tester API

  1. 在 Google Cloud 控制台中選取專案。

  2. 在「Firebase App Testers API」下方,點選「啟用」。

步驟 2:在應用程式中加入 App Distribution

  1. 開啟您為專案建立的 Podfile (或執行 pod init 建立 Podfile),然後在目標區段中加入下列程式碼:

    pod 'FirebaseAppDistribution'
  2. 在 Podfile 的目錄中執行 pod install,然後開啟建立的 .xcworkspace 檔案。

  3. 在 App struct 或 UIApplicationDelegate 中匯入 Firebase 模組:

    Swift

    import FirebaseCore
    import FirebaseAppDistribution
    

    Objective-C

    @import FirebaseCore;
    @import FirebaseAppDistribution;
    
  4. 在應用程式委派的 application(_:didFinishLaunchingWithOptions:) 方法中,設定 FirebaseApp 共用例項:

    Swift

    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
    
  5. 如果已停用 swizzling,請在 application(_:open:options:) 的實作中,將任何開啟的網址傳遞至 App Distribution SDK:

    Swift

    func application(_ app: UIApplication, 
                     open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
       if AppDistribution.appDistribution().application(application, open: url, options: options) {
          return true
       }
    
       // Handle other non-Firebase URLs here.
    
       return false
    }
    

    Objective-C

    - (BOOL)application:(UIApplication *)app 
                openURL:(NSURL *)url 
                options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
       if ([[FIRAppDistribution appDistribution] application:app openURL:url options:options]) {
          return YES;
       }
    
       // Handle other non-Firebase URLs here.
    
       return NO;
    }
    
  6. 最後,重新編譯應用程式。

步驟 3:設定應用程式內快訊

App Distribution SDK 提供兩種方式,可為測試人員設定應用程式內建構版本快訊:基本快訊設定,隨附預先建構的應用程式更新和登入對話方塊,可向測試人員顯示;以及進階快訊設定,可讓您自訂使用者介面。如果您是初次使用 App Distribution SDK,建議先使用基本快訊設定。

基本設定

使用 checkForUpdate 向尚未啟用快訊的測試人員顯示預先建構的啟用快訊對話方塊,然後檢查是否有新的版本。呼叫後,這個方法會依序執行下列操作:

  1. 提示測試人員使用 Google 帳戶登入 App Distribution,檢查是否已啟用快訊。

  2. 如果測試人員尚未啟用快訊,系統會顯示預先建立的對話方塊。

    在測試裝置上啟用快訊是一次性程序,且在應用程式更新後仍會保留。快訊會持續在測試裝置上啟用,直到應用程式解除安裝或呼叫 signOutTester 方法為止。詳情請參閱該方法的參考文件 (Swift 或 Objective-C)。

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

您可以在應用程式中的任何位置叫用 checkForUpdate()。舉例來說,您可以在應用程式根層級檢視畫面的 onAppear(perform:) 中加入 checkForUpdate(),提示測試人員在啟動時安裝新推出的版本。

以下範例會檢查測試人員是否已啟用快訊,以及是否能存取新版本。如果可以,則會在版本可供安裝時顯示對話方塊:

Swift

注意:這項產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
AppDistribution.appDistribution().checkForUpdate(completion: { release, error in
  if error != nil {
      // Handle error
      return
  }

  guard let release = release else {
    return
  }

  // Customize your alerts here.
  let title = "New Version Available"
  let message = "Version \(release.displayVersion)(\(release.buildVersion)) is available."
  let uialert = UIAlertController(title: title,message: message, preferredStyle: .alert)

  uialert.addAction(UIAlertAction(title: "Update", style: UIAlertAction.Style.default) {
    _ in
    UIApplication.shared.open(release.downloadURL)
  })
  uialert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) {
    _ in
  })

  // self should be a UIViewController.
  self.present(uialert, animated: true, completion: nil)
})

Objective-C

注意:這項產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
[[FIRAppDistribution appDistribution]
  checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
                                 NSError *_Nullable error) {
  if (error) {
    // Handle error
    return;
  }

  if (release) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"New Version Available"
message:[NSString stringWithFormat:@"Version %@ (%@) is available.", release.displayVersion,
release.buildVersion] preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"Update"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
      [[UIApplication sharedApplication] openURL:release.downloadURL options:@{}
completionHandler:nil];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
    [alert addAction:updateAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
  }
}];

進階設定

signInTester() 和 isTesterSignedIn 方法可讓您更靈活地自訂測試人員的登入體驗,更符合應用程式的外觀及風格。

以下範例會檢查測試人員是否已登入 Firebase App Distribution 測試人員帳戶,因此您可以選擇只向尚未登入的測試人員顯示登入 UI。測試人員登入後,您可以呼叫 checkForUpdate(),檢查測試人員是否能存取新版本。

Swift

注意:這項產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
// Sign in a tester without automatically checking for update
if (!AppDistribution.appDistribution().isTesterSignedIn) {
  AppDistribution.appDistribution().signInTester (completion: { error in
    // completion block for signInTester
     if (error != nil) {
       // handle failed sign in
      return
     }
    // handle successful sign in
  })
}

// Only check for update if tester is already signed in - do not prompt
if (AppDistribution.appDistribution().isTesterSignedIn) {
  AppDistribution.appDistribution().checkForUpdate(completion: { release, error in
      // completion block for check for update
  })
}

Objective-C

注意:這項產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。
// Sign in a tester without automatically checking for update
if(![[FIRAppDistribution appDistribution] isTesterSignedIn]) {
  [[FIRAppDistribution appDistribution]
    signInTesterWithCompletion:^(NSError *_Nullable error) {
      // completion block for signInTester
     if (error) {
       // handle failed sign in
       return;
     }
      // handle successful sign in
  }];
}

// only check for update if tester is already signed in - do not prompt
if([[FIRAppDistribution appDistribution] isTesterSignedIn]) {
  [[FIRAppDistribution appDistribution]
        checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
                                       NSError *_Nullable error) {
     // completion block for check for update
  }];
}

如要瞭解其他方法 (包括 signOutTester()),請參閱 Swift 和 Objective-C 的 App Distribution 參考文件。

步驟 4:建構及測試實作項目

最後,請建構應用程式,並使用 Firebase 控制台發行版本給測試人員,測試導入結果。

請參閱App Distribution疑難排解指南,瞭解如何解決常見問題,例如:

  • 測試人員未收到應用程式內快訊
  • 測試人員多次收到登入 Google 的提示