---
title: "Integrating in-app-reviews with Trusted Web Activity"
description: "Integrate in-app reviews into your Trusted Web Activity (TWA) using a custom schema.  This tutorial shows you how to add the necessary Android dependency, create a ReviewActivity to handle the review flow, and add a link in your web app to trigger the review.  Learn how to overcome caveats like ChromeOS compatibility and Play Store deployment requirements for testing.\n"
slug: "Integrating-in-app-reviews-with-Trusted-Web-Activity"
created: 2021-01-18T00:00:00Z
updated: 2021-01-18T00:00:00Z
tags:
  - "trusted-web-activity"
  - "in-app-review"
ai_assisted: false
---

I was reading the questions on the [`trusted-web-activity`](https://stackoverflow.com/questions/tagged/trusted-web-activity/) tag on StackOverflow, as I often do, when [a question](https://stackoverflow.com/questions/65752429/how-can-i-extend-twa-application-with-in-app-review) asking if it is possible to integrate a [Trusted Web Activity](https://developers.google.com/web/android/trusted-web-activity/) with [`in-app-reviews`](https://developer.android.com/guide/playcore/in-app-review) 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](https://www.npmjs.com/package/@bubblewrap/cli) 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](https://developers.google.com/web/android/trusted-web-activity/).

The idea is to use a custom schema, like `my-app://` that is handled by an [Android Activity](https://developer.android.com/reference/android/app/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:

```groovy
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:

```java
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.

```xml
  <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:

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

## The result

<video src="/img/2021/01/in-app-review.mp4" controls poster="/img/2021/01/in-app-review-cover.png"></video>

# Caveats

1. The link above will only work when the web app is running inside a Trusted Web Activity. [This question](https://stackoverflow.com/questions/54580414/how-can-i-detect-if-my-website-is-opened-inside-a-trusted-web-actvity) 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](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/RatingsAndReviewsPrompt/explainer.md) proposes how a general API for this use-case could work.