Andre Bandarra's Blog

Integrating in-app-reviews with Trusted Web Activity

I was reading the questions on the trusted-web-activity tag on StackOverflow, as I often do, when a question asking if it is possible to integrate a Trusted Web Activity with in-app-reviews caught my attention. In short, the answer is yes, it’s possible to do it, but there are caveats.

Ok, tell me how to do it!

Let’s assume an application bootstrapped with Bubblewrap and go over the changes needed to that application to implement in-app-reviews. I you are new to Trusted Web Activity, I do recommend reading the documentation.

The idea is to use a custom schema, like my-app:// that is handled by an Android Activity. This Activity will, in turn, launch the review flow and then finish itself.

The reason we need a custom schema is that internal URLs will trigger navigation inside the Trusted Web Activity and we want to get the user back to the Android part of the app for the review flow.

Step 1: Add the in-app-reviews dependency

You will need to add the com.google.android.play:core dependency to app/build.gradle. After adding it, the dependencies section should look like the following:

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.1.0'
implementation 'com.google.android.play:core:1.9.0'
}

Step 2: Create a ReviewActivity

Add a ReviewActivity.java file to the same folder where you will find Application.java, LauncherActivity.java and others, and implement in-app-reviews in this Activity.

This Activity won't have any UI and you'll only use its onCreate() method to launch the review flow:

package com.doom_fire.twa;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.play.core.review.ReviewInfo;
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;
import com.google.android.play.core.tasks.Task;

public class ReviewActivity extends Activity {
private static final String TAG = "ReviewActivity";
private ReviewManager mReviewManager;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Review Activity started.");
startReview();
}

public void startReview() {
mReviewManager = ReviewManagerFactory.create(this);
Log.d(TAG, "Requesting Review flow.");
Task<ReviewInfo> request =
mReviewManager.requestReviewFlow();
request.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Log.d(TAG, "Review Flow request succeeded.");
launchReviewFlow(task.getResult());
} else {
Log.d(TAG, "Review Flow request failed. Finishing.");
finish();
}
});
}

private void launchReviewFlow(@NonNull ReviewInfo reviewInfo) {
Log.d(TAG, "Launching review flow.");
Task<Void> reviewFlow =
mReviewManager.launchReviewFlow(this, reviewInfo);
reviewFlow.addOnCompleteListener(task -> {
Log.d(TAG, "Review flow finished. Finishing Activity.");
finish();
});
}
}

Step 3

Add the Activity you just created to AndroidManifest.xml. In the example below, we are using doom-fire as the schema and review as the host. This means that, in our web application, we'll link to doom-fire://review to trigger the ReviewActivity. Make sure to change this to something that suits your app.

  <activity android:name=".ReviewActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar">

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="doom-fire" android:host="review" />
</intent-filter>
</activity>

Step 4: Add a link to the web application:

<a href="doom-fire://review">Rate app now!</a>

The result

Caveats

  1. The link above will only work when the web app is running inside a Trusted Web Activity. This question explains how to detect this.

  2. Due to the way Trusted Web Activities on ChromeOS works, this solution may not work on the platform.

  3. The app review flow will only work if the application has been deployed to the Play Store, which can making testing a bit tricky.

The future

Reviews and ratings have been available to platform-specific developers for a long time and not only allow users to express their happiness (or the lack of it), but are also a tool that shortens the feedback cycle for developers and provides another metric businesses can use to benchmark against their competitors.

It’s interesting to think what such tool would mean for the web. If you are interested in the subject, the Prompt for Rating/Review Explainer proposes how a general API for this use-case could work.

← Home