补救措施

本页介绍了如何处理完整性判定方面的问题。

请求完整性令牌时,您可以选择向用户显示 Google Play 对话框。当存在一个或多个完整性判定方面的问题时,您可以显示该对话框。该对话框会显示在应用之上,并提示用户解决造成问题的原因。关闭该对话框后,您可以向 Integrity API 发出另一个请求以验证问题是否已解决。

完整性对话框

GET_LICENSED(类型代码 1)

判定问题

如果 appLicensingVerdict == "UNLICENSED",这意味着用户账号未获得许可。也就是说,用户未从 Google Play 安装或购买应用。

补救措施

您可以显示 GET_LICENSED 对话框,提示用户从 Google Play 获取您的应用。如果用户选择接受,该用户账号便会获得许可 (appLicensingVerdict == "LICENSED")。系统会将应用添加到用户的 Google Play 库,Google Play 就可以代表您交付应用更新。

用户体验示例

GET_LICENSED Play 对话框

请求完整性对话框

当客户端请求完整性令牌时,您可以使用 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());
    
    
  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;
    }
    
    
  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.
    }
    
    
  5. 对话框会显示在提供的 activity 之上。用户关闭对话框后,使用响应代码完成 Task。

  6. (可选)请求另一个令牌以显示任何其他对话框。如果您发出的是标准请求,则需要再次预热令牌提供程序以获取新的判定结果。