有什么方法可以知道 Google Play 应用内评论 API 是否显示评分对话框?

问题描述 投票:0回答:1

我需要知道是否已向用户显示费率对话框。如果尚未显示,我将需要处理另一个逻辑。有什么办法可以做到这一点吗?我几乎阅读了所有有关此问题的帖子,但没有找到解决方案。

我阅读了文档并在网上搜索了替代方法,但没有找到。

android kotlin mobile
1个回答
0
投票

我找到了解决办法。我将与您分享我的代码,然后进行解释。

private var manager: ReviewManager = ReviewManagerFactory.create(activity)

/** exception will be null if userSawDialog is true. */
private lateinit var onResultListener: (success: Boolean, exception: Exception?) -> Unit

fun start() {
    val request = manager.requestReviewFlow()
    request.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val reviewInfo = task.result
            reviewInfo?.let { 
                show(reviewInfo)
            }
        } else {
            onResultListener(false, task.exception)
        }
    }
}

private fun show(reviewInfo: ReviewInfo) {
    val flow = manager.launchReviewFlow(activity, reviewInfo)
    flow.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            // Obtains if the user has seen the dialog or not, by extracting the value from the ReviewInfo object.
            // when isNoOp is false, the user has seen the dialog.
            // So, if isNoOp is false, userSawDialog will be true.
            val isNoOp = reviewInfo.toString().substring(
                reviewInfo.toString().indexOf("isNoOp") + 7,
                reviewInfo.toString().length - 1
            )
            val userSawDialog = isNoOp == "false"
            onResultListener(userSawDialog, null)
        } else {
            onResultListener(false, task.exception)
        }
    }
}

基本上,没有“官方”方法可以知道用户是否看到了对话框。但是这个 ReviewInfo 对象在转换为字符串时是 json 格式,然后我们可以提取“isNoOp”字段的值,当向用户显示对话框时该值为 false,而当不向用户显示对话框时为 true。然后,变量“userSawDialog”表示用户是否看到该对话框。这是我的代码示例,您应该根据您的需要进行调整。

© www.soinside.com 2019 - 2024. All rights reserved.