Questa guida descrive come integrare le recensioni in-app nella tua app utilizzando Kotlin o Java. Esistono guide all'integrazione separate se utilizzi codice nativo, Unity o Unreal Engine.
Configura l'ambiente di sviluppo
La libreria Play In-App Review fa parte delle librerie Google Play Core. Includi la seguente dipendenza Gradle per integrare la libreria di revisione in-app di Play.
Groovy
// 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 revisione in-app. Puoi ottenerlo creando 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 giusti nel flusso utente della tua app in cui chiedere una recensione (ad esempio, quando l'utente completa un livello in un gioco). Quando la tua app raggiunge uno di questi punti, utilizza l'istanza ReviewManager
per creare un'attività di richiesta. In caso di esito positivo, 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) { // 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(); } });
Avvia il flusso di revisione in-app
Utilizza l'istanza ReviewInfo
per avviare il flusso di revisione in-app. Attendi che
l'utente abbia completato il flusso di revisione in-app prima che la tua app continui il suo
normale flusso utente (ad esempio, il passaggio al livello successivo).
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. });
Passaggi successivi
Esegui il test del flusso di revisione in-app della tua app per verificare che l'integrazione funzioni correttamente.