Hộp thoại khắc phục

Trang này mô tả cách xử lý các vấn đề liên quan đến kết quả về tính toàn vẹn.

Khi yêu cầu mã thông báo về tính toàn vẹn, bạn có thể cho người dùng thấy hộp thoại của Google Play. Bạn có thể cho người dùng thấy hộp thoại khi gặp phải một hoặc nhiều vấn đề liên quan đến kết quả về tính toàn vẹn. Hộp thoại này sẽ xuất hiện ở bên trên ứng dụng và nhắc người dùng giải quyết nguyên nhân của vấn đề. Sau khi đóng hộp thoại, bạn có thể xác minh xem vấn đề đã được khắc phục chưa bằng cách gửi một yêu cầu khác tới API Tính toàn vẹn.

Yêu cầu hộp thoại về tính toàn vẹn

Khi ứng dụng khách yêu cầu mã thông báo về tính toàn vẹn, bạn có thể sử dụng phương thức mà chúng tôi cung cấp trong StandardIntegrityToken (API Chuẩn) và IntegrityTokenResponse (API Kiểu cũ): showDialog(Activity activity, int integrityDialogTypeCode).

Các bước sau đây sẽ trình bày cách sử dụng API Tính toàn vẹn của Play để hiện hộp thoại về biện pháp khắc phục bằng mã hộp thoại GET_LICENSED. Các mã hộp thoại khác mà ứng dụng của bạn có thể yêu cầu được liệt kê sau phần này.

  1. Yêu cầu mã thông báo về tính toàn vẹn từ ứng dụng rồi gửi mã thông báo này đến máy chủ của bạn. Bạn có thể sử dụng yêu cầu Chuẩn hoặc Kiểu cũ.

    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); 

    Mã gốc

    /// 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. Trên máy chủ của bạn, hãy giải mã mã thông báo về tính toàn vẹn rồi kiểm tra trường appLicensingVerdict. Mã sẽ có dạng như sau:

    // Licensing issue
    {
      ...
      accountDetails: {
          appLicensingVerdict: "UNLICENSED"
      }
    }
  3. Nếu mã thông báo chứa appLicensingVerdict: "UNLICENSED", hãy phản hồi ứng dụng khách của bạn và yêu cầu hiện hộp thoại cấp phép:

    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;
    } 

    Mã gốc

    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. Trên ứng dụng, hãy gọi showDialog bằng mã bắt buộc được truy xuất từ máy chủ:

    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);
    }

    Mã gốc

    // 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. Hộp thoại sẽ xuất hiện bên trên hoạt động được cung cấp. Khi người dùng đóng hộp thoại, Tác vụ sẽ hoàn tất bằng một mã phản hồi.

  6. (Không bắt buộc) Hãy yêu cầu mã thông báo khác để hiện thêm hộp thoại. Nếu thực hiện yêu cầu chuẩn, bạn cần khởi động lại trình cung cấp mã thông báo để nhận kết quả mới.

Mã hộp thoại về tính toàn vẹn

GET_LICENSED (Mã loại 1)

Vấn đề về kết quả kiểm tra

Hộp thoại này phù hợp với 2 vấn đề:

  • Truy cập trái phép: appLicensingVerdict: "UNLICENSED". Điều này có nghĩa là tài khoản người dùng không có quyền đối với ứng dụng của bạn. Điều này có thể xảy ra nếu người dùng cài đặt ứng dụng qua cửa hàng ứng dụng hoặc có được ứng dụng từ một cửa hàng ứng dụng khác ngoài Google Play.
  • Ứng dụng bị giả mạo: appRecognitionVerdict: "UNRECOGNIZED_VERSION". Điều này có nghĩa là tệp nhị phân của ứng dụng đã bị sửa đổi hoặc không phải là phiên bản được Google Play công nhận.

Cách khắc phục

Bạn có thể cho người dùng thấy hộp thoại GET_LICENSED để nhắc họ tải ứng dụng chính hãng qua Google Play. Hộp thoại duy nhất này giải quyết cả hai trường hợp:

  • Đối với người dùng chưa được cấp phép, phương thức này sẽ cấp cho họ giấy phép Play. Điều này cho phép người dùng nhận được bản cập nhật ứng dụng từ Google Play.
  • Đối với người dùng có phiên bản ứng dụng bị giả mạo, thông báo này sẽ hướng dẫn họ cài đặt ứng dụng chưa bị sửa đổi từ Google Play.

Khi người dùng hoàn tất hộp thoại, các quy trình kiểm tra tính toàn vẹn tiếp theo sẽ trả về appLicensingVerdict: "LICENSED"appRecognitionVerdict: "PLAY_RECOGNIZED".

Ví dụ về trải nghiệm người dùng

Hình 1. Hộp thoại GET_LICENSED của Play.

CLOSE_UNKNOWN_ACCESS_RISK (Mã loại 2)

Vấn đề về kết quả kiểm tra

Khi environmentDetails.appAccessRiskVerdict.appsDetected chứa "UNKNOWN_CAPTURING" hoặc "UNKNOWN_CONTROLLING", điều này có nghĩa là có các ứng dụng không xác định đang chạy trên thiết bị có thể chụp màn hình hoặc điều khiển thiết bị.

Cách khắc phục

Bạn có thể cho người dùng thấy hộp thoại CLOSE_UNKNOWN_ACCESS_RISK để nhắc họ đóng tất cả các ứng dụng không xác định có thể đang chụp màn hình hoặc điều khiển thiết bị. Nếu người dùng nhấn vào nút Close all, tất cả các ứng dụng như vậy sẽ đóng.

Ví dụ về trải nghiệm người dùng

Hình 2. Hộp thoại đóng rủi ro truy cập không xác định.

CLOSE_ALL_ACCESS_RISK (Mã loại 3)

Vấn đề về kết quả kiểm tra

Khi environmentDetails.appAccessRiskVerdict.appsDetected chứa bất kỳ giá trị nào trong số "KNOWN_CAPTURING", "KNOWN_CONTROLLING","UNKNOWN_CAPTURING" hoặc "UNKNOWN_CONTROLLING", điều này có nghĩa là có những ứng dụng đang chạy trên thiết bị có thể chụp màn hình hoặc điều khiển thiết bị.

Cách khắc phục

Bạn có thể cho người dùng thấy hộp thoại CLOSE_ALL_ACCESS_RISK để nhắc họ đóng tất cả các ứng dụng có thể đang chụp màn hình hoặc điều khiển thiết bị. Nếu người dùng nhấn vào nút Close all, tất cả các ứng dụng như vậy sẽ bị đóng trên thiết bị.

Ví dụ về trải nghiệm người dùng

Hình 3. Hộp thoại đóng tất cả các rủi ro khi truy cập.