Integrare le recensioni in-app (Kotlin o Java)

Questa guida descrive come integrare le recensioni in-app nella tua app utilizzando Kotlin o Java. Sono disponibili guide all'integrazione separate se utilizzi codice nativo, Unity o Unreal Engine.

Configurazione dell'ambiente di sviluppo

La libreria Play In-App Review fa parte delle librerie di base Google Play. Includi la seguente dipendenza Gradle per integrare la libreria per le recensioni in-app di Google Play.

Alla moda

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

Crea ReviewManager

ReviewManager è l'interfaccia che consente alla tua app di avviare un flusso di recensioni in-app. Per ottenerlo, crea un'istanza utilizzando ReviewManagerFactory.

Kotlin

val manager = ReviewManagerFactory.create(context)

Java

ReviewManager manager = ReviewManagerFactory.create(context)

Richiedi un oggetto ReviewInfo

Segui le indicazioni su quando richiedere recensioni in-app per determinare i punti appropriati nel flusso utente della tua app per chiedere all'utente di scrivere una recensione (ad esempio, quando l'utente completa un livello in un gioco). Quando l'app raggiunge uno di questi punti, utilizza l'istanza ReviewManager per creare un'attività di richiesta. Se l'operazione va a buon fine, l'API restituisce l'oggetto ReviewInfo necessario per avviare il flusso di revisione in-app.

Kotlin

val request = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
    if (task.isSuccessful) {
        // 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()) {
        // 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();
    }
});

Avvia il flusso di recensione in-app

Utilizza l'istanza ReviewInfo per avviare il flusso di recensioni in-app. Attendi che l'utente abbia completato il flusso di recensione in-app prima che l'app continui il suo normale flusso utente (ad esempio, passando al livello successivo).

Kotlin

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

Java

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

Passaggi successivi

Testa il flusso di recensioni in-app della tua app per verificare che l'integrazione funzioni correttamente.