שילוב ביקורות בתוך האפליקציה (Kotlin או Java)

במדריך הזה מוסבר איך לשלב ביקורות בתוך האפליקציה באמצעות Kotlin או Java. יש מדריכי שילוב נפרדים אם אתם משתמשים בקוד Native, ב-Unity או ב-Unreal Engine.

הגדרת סביבת הפיתוח

ספריית Play In-App Review היא חלק מספריות Google Play Core. כדי לשלב את ספריית הביקורות בתוך האפליקציה של Play, צריך לכלול את התלות הבאה ב-Gradle.

מגניב

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

השלבים הבאים

בודקים את תהליך כתיבת הביקורות בתוך האפליקציה כדי לוודא שההטמעה פועלת כמו שצריך.