補救對話方塊

本頁面說明如何處理完整性判定結果的問題。

在要求提供完整性權杖時,您可以選擇向使用者顯示 Google Play 對話方塊。如果完整性判定結果出現一或多個問題,您就可以顯示這個對話方塊。對話方塊會顯示在應用程式頂端,並提示使用者解決問題。對話方塊關閉後,您可以向 Integrity API 提出另一項要求,驗證問題是否已修正。

要求完整性對話方塊

當用戶端要求完整性權杖時,您可以使用 StandardIntegrityToken (標準 API) 和 IntegrityTokenResponse (傳統 API) 中提供的方法: showDialog(Activity activity, int integrityDialogTypeCode)

下列步驟概述如何使用 Play Integrity API,透過「GET_LICENSED」對話方塊程式碼顯示修復對話方塊。本節後方列出應用程式可要求的其他對話方塊代碼。

  1. 透過應用程式要求完整性權杖,然後將權杖傳送至伺服器。使用「標準」或「傳統」要求都可以。

    Kotlin

    // Request an integrity token
    val tokenResponse: StandardIntegrityToken = requestIntegrityToken()
    // Send token to app server and get response on what to do next
    val yourServerResponse: YourServerResponse = sendToServer(tokenResponse.token())  

    Java

    // Request an integrity token
    StandardIntegrityToken tokenResponse = requestIntegrityToken();
    // Send token to app server and get response on what to do next
    YourServerResponse yourServerResponse = sendToServer(tokenResponse.token());  

    Unity

    // Request an integrity token
    StandardIntegrityToken tokenResponse = RequestIntegrityToken();
    // Send token to app server and get response on what to do next
    YourServerResponse yourServerResponse = sendToServer(tokenResponse.Token); 

    Unreal Engine

    // Request an integrity token
    StandardIntegrityToken* Response = RequestIntegrityToken();
    // Send token to app server and get response on what to do next
    YourServerResponse YourServerResponse = SendToServer(Response->Token); 

    原生

    /// Request an integrity token
    StandardIntegrityToken* response = requestIntegrityToken();
    /// Send token to app server and get response on what to do next
    YourServerResponse yourServerResponse = sendToServer(StandardIntegrityToken_getToken(response));
  2. 在伺服器上解密完整性權杖,然後檢查 appLicensingVerdict 欄位。如下所示:

    // Licensing issue
    {
      ...
      "accountDetails": {
          "appLicensingVerdict": "UNLICENSED"
      }
    }
  3. 如果權杖包含 appLicensingVerdict: "UNLICENSED",請回覆應用程式用戶端,要求顯示授權對話方塊:

    Kotlin

    private fun getDialogTypeCode(integrityToken: String): Int{
      // Get licensing verdict from decrypted and verified integritytoken
      val licensingVerdict: String = getLicensingVerdictFromDecryptedToken(integrityToken)
    
      return if (licensingVerdict == "UNLICENSED") {
              1 // GET_LICENSED
          } else 0
    }

    Java

    private int getDialogTypeCode(String integrityToken) {
      // Get licensing verdict from decrypted and verified integrityToken
      String licensingVerdict = getLicensingVerdictFromDecryptedToken(integrityToken);
    
      if (licensingVerdict.equals("UNLICENSED")) {
        return 1; // GET_LICENSED
      }
      return 0;
    }

    Unity

    private int GetDialogTypeCode(string IntegrityToken) {
      // Get licensing verdict from decrypted and verified integrityToken
      string licensingVerdict = GetLicensingVerdictFromDecryptedToken(IntegrityToken);
    
      if (licensingVerdict == "UNLICENSED") {
        return 1; // GET_LICENSED
      }
      return 0;
    } 

    Unreal Engine

    private int GetDialogTypeCode(FString IntegrityToken) {
      // Get licensing verdict from decrypted and verified integrityToken
      FString LicensingVerdict = GetLicensingVerdictFromDecryptedToken(IntegrityToken);
    
      if (LicensingVerdict == "UNLICENSED") {
        return 1; // GET_LICENSED
      }
      return 0;
    } 

    原生

    private int getDialogTypeCode(string integrity_token) {
      /// Get licensing verdict from decrypted and verified integrityToken
      string licensing_verdict = getLicensingVerdictFromDecryptedToken(integrity_token);
    
      if (licensing_verdict == "UNLICENSED") {
        return 1; // GET_LICENSED
      }
      return 0;
    }
  4. 從伺服器擷取要求的程式碼後,在應用程式中使用該程式碼呼叫 showDialog

    Kotlin

    // Show dialog as indicated by the server
    val showDialogType: Int? = yourServerResponse.integrityDialogTypeCode()
    if (showDialogType != null) {
      // Call showDialog with type code, the dialog will be shown on top of the
      // provided activity and complete when the dialog is closed.
      val integrityDialogResponseCode: Task<Int> =
      tokenResponse.showDialog(activity, showDialogType)
      // Handle response code, call the Integrity API again to confirm that
      // verdicts have been resolved.
    } 

    Java

    // Show dialog as indicated by the server
    @Nullable Integer showDialogType = yourServerResponse.integrityDialogTypeCode();
    if (showDialogType != null) {
      // Call showDialog with type code, the dialog will be shown on top of the
      // provided activity and complete when the dialog is closed.
      Task<Integer> integrityDialogResponseCode =
          tokenResponse.showDialog(activity, showDialogType);
      // Handle response code, call the Integrity API again to confirm that
      // verdicts have been resolved.
    }

    Unity

    IEnumerator ShowDialogCoroutine() {
      int showDialogType = yourServerResponse.IntegrityDialogTypeCode();
    
      // Call showDialog with type code, the dialog will be shown on top of the
      // provided activity and complete when the dialog is closed.
      var showDialogTask = tokenResponse.ShowDialog(showDialogType);
    
      // Wait for PlayAsyncOperation to complete.
      yield return showDialogTask;
    
      // Handle response code, call the Integrity API again to confirm that
      // verdicts have been resolved.
    } 

    Unreal Engine

    // .h
    void MyClass::OnShowDialogCompleted(
      EStandardIntegrityErrorCode Error,
      EIntegrityDialogResponseCode Response)
    {
      // Handle response code, call the Integrity API again to confirm that
      // verdicts have been resolved.
    }
    
    // .cpp
    void MyClass::RequestIntegrityToken()
    {
      UStandardIntegrityToken* Response = ...
      int TypeCode = YourServerResponse.integrityDialogTypeCode();
    
      // Create a delegate to bind the callback function.
      FShowDialogStandardOperationCompletedDelegate Delegate;
    
      // Bind the completion handler (OnShowDialogCompleted) to the delegate.
      Delegate.BindDynamic(this, &MyClass::OnShowDialogCompleted);
    
      // Call ShowDialog with TypeCode which completes when the dialog is closed.
      Response->ShowDialog(TypeCode, Delegate);
    }

    原生

    // Show dialog as indicated by the server
    int show_dialog_type = yourServerResponse.integrityDialogTypeCode();
    if (show_dialog_type != 0) {
      /// Call showDialog with type code, the dialog will be shown on top of the
      /// provided activity and complete when the dialog is closed.
      StandardIntegrityErrorCode error_code =
          IntegrityTokenResponse_showDialog(response, activity, show_dialog_type);
    
      /// Proceed to polling iff error_code == STANDARD_INTEGRITY_NO_ERROR
      if (error_code != STANDARD_INTEGRITY_NO_ERROR)
      {
          /// Remember to call the *_destroy() functions.
          return;
      }
    
      /// Use polling to wait for the async operation to complete.
      /// Note, the polling shouldn't block the thread where the IntegrityManager
      /// is running.
    
      IntegrityDialogResponseCode* response_code;
      error_code = StandardIntegrityToken_getDialogResponseCode(response, response_code);
    
      if (error_code != STANDARD_INTEGRITY_NO_ERROR)
      {
          /// Remember to call the *_destroy() functions.
          return;
      }
    
      /// Handle response code, call the Integrity API again to confirm that
      /// verdicts have been resolved.
    }
  5. 對話方塊會顯示在系統提供的活動頂端。使用者關閉對話方塊後,工作就會完成,並顯示回應代碼

  6. (選用) 要求其他權杖,以便顯示任何後續對話方塊。如果您提出標準要求,需再次替權杖供應工具暖機,才能取得最新判定結果。

完整性對話方塊代碼

GET_LICENSED (類型代碼 1)

判定結果問題

這個對話方塊適用於下列兩種問題:

  • 未經授權的存取行為appLicensingVerdict: "UNLICENSED"。也就是說,使用者帳戶沒有應用程式授權。如果使用者側載應用程式,或從 Google Play 以外的應用程式商店取得應用程式,就會發生這種情況。
  • 遭竄改的應用程式appRecognitionVerdict: "UNRECOGNIZED_VERSION"。這表示應用程式的二進位檔已遭修改,或不是 Google Play 認可的版本。

修復

您可以顯示 GET_LICENSED 對話方塊,提示使用者從 Google Play 取得正版應用程式。這個單一對話方塊可解決這兩種情況:

  • 如果是未獲授權的使用者,系統會授予 Play 授權。這樣使用者就能透過 Google Play 接收應用程式更新。
  • 如果使用者安裝的應用程式版本遭到竄改,系統會引導他們從 Google Play 安裝未修改的應用程式。

使用者完成對話方塊後,後續的完整性檢查會傳回 appLicensingVerdict: "LICENSED"appRecognitionVerdict: "PLAY_RECOGNIZED"

使用者體驗範例

圖 1. GET_LICENSED Play 對話方塊。

CLOSE_UNKNOWN_ACCESS_RISK (類型代碼 2)

判定結果問題

如果 environmentDetails.appAccessRiskVerdict.appsDetected 包含 "UNKNOWN_CAPTURING""UNKNOWN_CONTROLLING",表示裝置上執行了不明應用程式,可能正在擷取螢幕畫面或控制裝置。

修復

您可以顯示 CLOSE_UNKNOWN_ACCESS_RISK 對話方塊,提示使用者關閉所有可能擷取螢幕畫面或控制裝置的不明應用程式。如果使用者輕觸 Close all 按鈕,系統會關閉所有這類應用程式。

使用者體驗範例

圖 2. 關閉不明存取風險的對話方塊。

CLOSE_ALL_ACCESS_RISK (類型代碼 3)

判定結果問題

如果 environmentDetails.appAccessRiskVerdict.appsDetected 包含 "KNOWN_CAPTURING""KNOWN_CONTROLLING""UNKNOWN_CAPTURING""UNKNOWN_CONTROLLING",表示裝置上執行的應用程式可能會擷取螢幕截圖或控制裝置。

修復

您可以顯示 CLOSE_ALL_ACCESS_RISK 對話方塊,提示使用者關閉所有可能擷取螢幕或控制裝置的應用程式。如果使用者輕觸 Close all 按鈕,裝置上所有這類應用程式都會關閉。

使用者體驗範例

圖 3. 對話方塊,用於關閉所有存取風險。