Load a single rewarded ad

Rewarded ads let you reward users with in-app items for interacting with video ads, playable ads, and surveys.

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

Before you begin

Before you continue, do the following:

  • Set up GMA Next-Gen SDK.
  • Use the test rewarded ad unit ID /21775744923/example/rewarded.
    • 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.

Load an ad

The following example loads a single ad:

Kotlin

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

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

Java

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

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

When testing, use the test ad unit ID: /21775744923/example/rewarded.

Show the ad

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

The following example displays the ad:

Kotlin

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

Java

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

Listen to ad events

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

Kotlin

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

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

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

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

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

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

Java

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

  rewardedAd.setAdEventCallback(
      new RewardedAdEventCallback() {
        @Override
        public void onAdShowedFullScreenContent() {
          // Rewarded ad did show.
        }

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

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

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

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

Optional: Validate server-side verification (SSV) callbacks

To pass user and application context in server-side verification (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

RewardedAd.load(
  context,
  AD_UNIT_ID,
  AdRequest.Builder().build(),
  object : RewardedAdLoadCallback() {
    override fun onAdLoaded(ad: RewardedAd) {
      rewardedAd = ad
      val options =
        ServerSideVerificationOptions.Builder().setCustomData("SAMPLE_CUSTOM_DATA_STRING").build()
      rewardedAd?.setServerSideVerificationOptions(options)
    }
  },
)

Java

RewardedAd.load(
    context,
    AD_UNIT_ID,
    new AdRequest.Builder().build(),
    new RewardedAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedAd ad) {
        rewardedAd = ad;
        ServerSideVerificationOptions options =
            new ServerSideVerificationOptions.Builder()
                .setCustomData("SAMPLE_CUSTOM_DATA_STRING")
                .build();
        rewardedAd.setServerSideVerificationOptions(options);
      }
    });

Replace SAMPLE_CUSTOM_DATA_STRING with your custom data.

Key point: The custom_data query parameter is percent-encoded. 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.