Rewarded interstitial ads are a format that lets you offer rewards for ads
that appear automatically during natural app transitions. Unlike rewarded ads,
users don't have to opt in to view a rewarded interstitial ad.

For more information, see
[Rewarded interstitial guidance](https://support.google.com/admob/answer/9884467).


This guide explains how to integrate rewarded interstitial ads into an Android
app.

## Before you begin

Before you continue, do the following:

- [Set up GMA Next-Gen SDK](https://developers.google.com/admob/android/next-gen/quick-start.md.txt).
- Use the test rewarded interstitial ad unit ID `ca-app-pub-3940256099942544/5354046379`.
  - When building and testing your app, make sure that you use test ads rather than live, production ads. If you don't use the test ad unit ID, Google can suspend your account.
  - Before you publish your app, replace this ID with your ad unit ID.
  - For details on GMA Next-Gen SDK test ads, see [Enable test ads](https://developers.google.com/admob/android/next-gen/test-ads.md.txt).

## Load an ad

> [!TIP]
> **Tip:** To optimize ad serving latency for multiple ads, use [ad preloading](https://developers.google.com/admob/android/next-gen/rewarded-interstitial.md.txt).

The following example loads a single ad:


### Kotlin

```kotlin
// Load ads after you initialize MobileAds.
RewardedInterstitialAd.load(
  AdRequest.Builder(adUnitId).build(),
  object : AdLoadCallback<RewardedInterstitialAd> {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      // Rewarded interstitial ad loaded.
      rewardedInterstitialAd = ad
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      // Rewarded interstitial ad failed to load.
      Log.e(TAG, "Rewarded interstitial ad failed to load: ${adError.message}")
      rewardedInterstitialAd = null
    }
  },
)
```

### Java

```java
// Load ads after you initialize MobileAds.
RewardedInterstitialAd.load(
    new AdRequest.Builder(adUnitId).build(),
    new AdLoadCallback<RewardedInterstitialAd>() {
      @Override
      public void onAdLoaded(@NonNull RewardedInterstitialAd ad) {
        // Rewarded interstitial ad loaded.
        rewardedInterstitialAd = ad;
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError adError) {
        // Rewarded interstitial ad failed to load.
        Log.e(TAG, "Rewarded interstitial ad failed to load: " + adError.getMessage());
        rewardedInterstitialAd = null;
      }
    });
```

<br />

When testing, use the test ad unit ID:
`ca-app-pub-3940256099942544/5354046379`.

## Show the ad

To show a rewarded interstitial ad, call the `show` method. To handle reward events, use an `OnUserEarnedRewardListener` object.

The following example displays the ad:


### Kotlin

```kotlin
private fun showAd(rewardedInterstitialAd: RewardedInterstitialAd, activity: Activity) {
  // Show the ad.
  rewardedInterstitialAd.show(
    activity,
    object : OnUserEarnedRewardListener {
      override fun onUserEarnedReward(rewardItem: RewardItem) {
        // User earned the reward.
        val rewardAmount = rewardItem.amount
        val rewardType = rewardItem.type
      }
    },
  )
}
```

### Java

```java
private void showAd(RewardedInterstitialAd rewardedInterstitialAd, Activity activity) {
  // Show the ad.
  rewardedInterstitialAd.show(
      activity,
      new OnUserEarnedRewardListener() {
        @Override
        public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
          // User earned the reward.
          int rewardAmount = rewardItem.getAmount();
          String rewardType = rewardItem.getType();
        }
      });
}
```

<br />

## Listen to ad events

Before showing the ad, listen to ad lifecycle events. The following example
registers callbacks for ad events:


### Kotlin

```kotlin
private fun listenToAdEvents() {
  // Listen for ad events.
  val ad = rewardedInterstitialAd
  if (ad == null) {
    Log.e(TAG, "Rewarded interstitial ad is not ready yet.")
    return
  }

  ad.adEventCallback =
    object : RewardedInterstitialAdEventCallback {
      override fun onAdShowedFullScreenContent() {
        // Rewarded interstitial ad did show.
      }

      override fun onAdDismissedFullScreenContent() {
        // Rewarded interstitial ad did dismiss.
        rewardedInterstitialAd = null
      }

      override fun onAdFailedToShowFullScreenContent(
        fullScreenContentError: FullScreenContentError
      ) {
        // Rewarded interstitial ad failed to show.
        Log.e(TAG, "Rewarded interstitial ad failed to show: ${fullScreenContentError.message}")
      }

      override fun onAdImpression() {
        // Rewarded interstitial ad did record an impression.
      }

      override fun onAdClicked() {
        // Rewarded interstitial ad did record a click.
      }
    }
}
```

### Java

```java
private void listenToAdEvents() {
  // Listen for ad events.
  if (rewardedInterstitialAd == null) {
    Log.e(TAG, "Rewarded interstitial ad is not ready yet.");
    return;
  }

  rewardedInterstitialAd.setAdEventCallback(
      new RewardedInterstitialAdEventCallback() {
        @Override
        public void onAdShowedFullScreenContent() {
          // Rewarded interstitial ad did show.
        }

        @Override
        public void onAdDismissedFullScreenContent() {
          // Rewarded interstitial ad did dismiss.
          rewardedInterstitialAd = null;
        }

        @Override
        public void onAdFailedToShowFullScreenContent(
            @NonNull FullScreenContentError fullScreenContentError) {
          // Rewarded interstitial ad failed to show.
          Log.e(
              TAG,
              "Rewarded interstitial ad failed to show: " + fullScreenContentError.getMessage());
        }

        @Override
        public void onAdImpression() {
          // Rewarded interstitial ad did record an impression.
        }

        @Override
        public void onAdClicked() {
          // Rewarded interstitial ad did record a click.
        }
      });
}
```

<br />

## Optional: Validate server-side verification (SSV) callbacks

To pass user and application context in
[server-side verification](https://developers.google.com/admob/android/next-gen/ssv.md.txt)
(SSV) callbacks, set
verification options on the ad object before showing the ad. Because SSV
callbacks arrive at your server from Google without session state,
attach the following optional strings. Google forwards these strings unchanged
as signed query parameters in the SSV callback:

- `user_id` query parameter: Identifies the user to credit, such as an internal account ID.
- `custom_data` query parameter: Carries app data, such as the reward placement, game level, or a server-generated nonce.

If you don't set a value, Google omits that query parameter from the SSV
callback.

The following example sets custom data on an ad object before
showing the ad:


### Kotlin

```kotlin
RewardedInterstitialAd.load(
  context,
  AD_UNIT_ID,
  AdRequest.Builder().build(),
  object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      rewardedInterstitialAd = ad
val options =
ServerSideVerificationOptions.Builder().setCustomData("SAMPLE_CUSTOM_DATA_STRING").build()
rewardedInterstitialAd?.setServerSideVerificationOptions(options)
}
},
)
```

### Java

```java
RewardedInterstitialAd.load(
    context,
    AD_UNIT_ID,
    new AdRequest.Builder().build(),
    new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        rewardedInterstitialAd = ad;
ServerSideVerificationOptions options =
new ServerSideVerificationOptions.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build();
rewardedInterstitialAd.setServerSideVerificationOptions(options);
      }
    });
```

<br />

Replace `SAMPLE_CUSTOM_DATA_STRING` with your custom
data.

Key point: The `custom_data` query parameter is
[percent-encoded](https://en.wikipedia.org/wiki/Percent-encoding). Before parsing the
value, URL-decode the string on your server and verify the callback signature
before trusting the `user_id` or `custom_data` query parameters.