ผสานรวมรีวิวในแอป (Kotlin หรือ Java)

คู่มือนี้อธิบายวิธีผสานรวมการตรวจสอบในแอปในแอปของคุณโดยใช้ Kotlin หรือ Java โดยจะมีคู่มือการผสานรวมแยกต่างหากหากคุณใช้โค้ด ดั้งเดิม, Unity หรือ Unreal Engine

ตั้งค่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์

ไลบรารีการตรวจสอบในแอปของ Play เป็นส่วนหนึ่งของไลบรารี Google Play Core รวมทรัพยากร Dependency ของ Gradle ต่อไปนี้เพื่อผสานรวมไลบรารีการตรวจสอบในแอปของ Google Play

ดึงดูด

// In your app's build.gradle file:
...
dependencies {
    // This dependency is downloaded from the Google's Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation 'com.google.android.play:review:2.0.2'

    // For Kotlin users also add the Kotlin extensions library for Play In-App Review:
    implementation 'com.google.android.play:review-ktx:2.0.2'
    ...
}

Kotlin

// In your app's build.gradle.kts file:
...
dependencies {
    // This dependency is downloaded from the Google's Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation("com.google.android.play:review:2.0.2")

    // For Kotlin users also import the Kotlin extensions library for Play In-App Review:
    implementation("com.google.android.play:review-ktx:2.0.2")
    ...
}

สร้าง ReviewManager

ReviewManager เป็นอินเทอร์เฟซที่ช่วยให้แอปของคุณเริ่มโฟลว์การตรวจสอบในแอป ได้ คุณรับอินเทอร์เฟซนี้ได้โดยการสร้างอินสแตนซ์โดยใช้ ReviewManagerFactory

Kotlin

val manager = ReviewManagerFactory.create(context)

Java

ReviewManager manager = ReviewManagerFactory.create(context)

ขอออบเจ็กต์ ReviewInfo

ทำตามคำแนะนำเกี่ยวกับ เวลาที่ควรขอการตรวจสอบในแอป เพื่อกำหนดจุดที่เหมาะสม ในโฟลว์ผู้ใช้ของแอปเพื่อแจ้งให้ผู้ใช้เขียนรีวิว (เช่น เมื่อผู้ใช้ผ่านด่านในเกม) เมื่อแอปของคุณไปถึงจุดใดจุดหนึ่งเหล่านี้ ให้ใช้ ReviewManager อินสแตนซ์เพื่อสร้างงานคำขอ หาก สำเร็จ API จะแสดงออบเจ็กต์ ReviewInfo ที่จำเป็นในการเริ่ม โฟลว์การตรวจสอบในแอป

Kotlin

val request = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
    if (task.isSuccessful) {
        // We got the ReviewInfo object
        val reviewInfo = task.result
    } else {
        // There was some problem, log or handle the error code.
        @ReviewErrorCode val reviewErrorCode = (task.getException() as ReviewException).errorCode
    }
}

Java

ReviewManager manager = ReviewManagerFactory.create(this);
Task<ReviewInfo> request = manager.requestReviewFlow();
request.addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        // We can get the ReviewInfo object
        ReviewInfo reviewInfo = task.getResult();
    } else {
        // There was some problem, log or handle the error code.
        @ReviewErrorCode int reviewErrorCode = ((ReviewException) task.getException()).getErrorCode();
    }
});

เปิดใช้โฟลว์การตรวจสอบในแอป

ใช้อินสแตนซ์ ReviewInfo เพื่อเปิดใช้โฟลว์การตรวจสอบในแอป รอจนกว่าผู้ใช้จะทำโฟลว์การตรวจสอบในแอปเสร็จสมบูรณ์แล้ว แอปจึงจะดำเนินการโฟลว์ผู้ใช้ตามปกติ (เช่น ไปยังระดับถัดไป)

Kotlin

val flow = manager.launchReviewFlow(activity, reviewInfo)
flow.addOnCompleteListener { _ ->
    // The flow has finished. The API does not indicate whether the user
    // reviewed or not, or even whether the review dialog was shown. Thus, no
    // matter the result, we continue our app flow.
}

Java

Task<Void> flow = manager.launchReviewFlow(activity, reviewInfo);
flow.addOnCompleteListener(task -> {
    // The flow has finished. The API does not indicate whether the user
    // reviewed or not, or even whether the review dialog was shown. Thus, no
    // matter the result, we continue our app flow.
});

ขั้นตอนถัดไป

ทดสอบโฟลว์การตรวจสอบในแอปของแอป เพื่อยืนยันว่าการผสานรวมทำงาน อย่างถูกต้อง