Skip to content

Instantly share code, notes, and snippets.

@shiv19
Created August 7, 2020 05:09
Show Gist options
  • Save shiv19/ed55e3b59663de79a3d6273f414c339d to your computer and use it in GitHub Desktop.
Save shiv19/ed55e3b59663de79a3d6273f414c339d to your computer and use it in GitHub Desktop.
Implementation of Google Play In-App Review API for NativeScript Apps
/*
https://developer.android.com/guide/playcore/in-app-review
In app reviews implementation for NativeScript Apps
Pre-requisites:
Add Google Play Core lib to your app.gradle's dependency list
app/App_Resources/Android/app.gradle ->
dependencies {
...
implementation 'com.google.android.play:core:1.8.0'
...
}
Next, decide at which point in your app's flow you want to request for review,
and just call the function below. The review dialog won't show up unless the
app was installed from google play
Read this for more info: https://developer.android.com/guide/playcore/in-app-review/test
*/
import { Application, isAndroid } from '@nativescript/core';
function requestReview() {
if (isAndroid) {
return new Promise((resolve, reject) => {
/* global com */
const manager = com.google.android.play.core.review.ReviewManagerFactory.create(Application.android.context);
const request = manager.requestReviewFlow();
request.addOnCompleteListener(
new com.google.android.play.core.tasks.OnCompleteListener({
onComplete(task) {
if (task.isSuccessful()) {
const reviewInfo = task.getResult();
const flow = manager.launchReviewFlow(Application.android.foregroundActivity, reviewInfo);
flow.addOnCompleteListener(
new com.google.android.play.core.tasks.OnCompleteListener({
onComplete(task) {
if (task.isSuccessful()) {
console.log('Rating success');
resolve();
} else {
console.log('Rating failed', task.getException().getMessage());
reject(task.getException().getMessage());
}
},
})
);
} else {
console.log('Failed to get review info', task.getException().getMessage());
reject(task.getException().getMessage());
}
},
})
);
});
} else {
return Promise.resolve();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment