通知測試人員新版本的消息

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

事前準備

如果您尚未將 Firebase 新增至 iOS 專案,請先完成這項操作。

步驟 1:啟用 App Distribution Tester API

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

  2. 按一下「Firebase App Testers API」下方的「啟用」

步驟 2:在應用程式中新增應用程式發布內容

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

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

  3. 將 Firebase 模組匯入 App 結構或 UIApplicationDelegate

    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. 如果已停用滑動檢查功能,請將所有開啟的網址傳遞至您實作 application(_:open:options:) 中的應用程式發布 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:設定應用程式內快訊

應用程式發布 SDK 提供兩種為測試人員設定應用程式內建構快訊的方法:一種基本的快訊設定,包含預先建構的應用程式更新和登入對話方塊,可以向測試人員顯示;後者則可讓您自訂自己的使用者介面。如果您是應用程式發布 SDK 新手,建議您先使用基本快訊設定。

基本設定

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

  1. 提示測試人員使用 Google 帳戶登入「應用程式發布」頁面,藉此檢查是否已啟用快訊功能。

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

    啟用快訊是在測試裝置上啟用的一次性程序,且會在應用程式更新時持續顯示。除非應用程式解除安裝或呼叫 signOutTester 方法,否則測試裝置上的快訊會保持啟用狀態。詳情請參閱該方法的參考說明文件 (SwiftObjective-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 應用程式發布測試人員帳戶,這樣您就可以選擇只向尚未登入的測試人員顯示登入 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()),請參閱 SwiftObjective-C 的應用程式發布參考說明文件。

步驟 4:建構並測試實作成果

最後,請使用 Firebase 控制台建構應用程式,並發布版本給測試人員,藉此測試實作成果。

請參閱應用程式發布疑難排解指南,取得常見問題的說明,例如:

  • 測試人員沒有收到應用程式內快訊
  • 系統多次提示測試人員登入 Google