リワード インタースティシャルは、アプリの画面が変わる自然なタイミングで自動的に表示される広告に対して報酬を提供できる、インセンティブ広告フォーマットの一種です。リワード広告とは異なり、ユーザーはリワード インタースティシャルを表示するためにオプトインする必要はありません。
前提条件
- Google Mobile Ads SDK 19.2.0 以降
- スタートガイドの手順を完了していること
実装
リワード インタースティシャル広告を組み込む際の基本的な流れは次のとおりです。
- 広告を読み込む
- 全画面表示イベントのコールバックを登録する
- リワード コールバックを処理する
- 広告を表示する
広告を読み込む
広告の読み込みは、RewardedInterstitialAd クラスで静的 load() メソッドを使用して行います。読み込みメソッドでは、広告の読み込みの成功時または失敗時に、コンテキスト、広告ユニット ID、AdManagerAdRequest オブジェクト、RewardedInterstitialAdLoadCallback が通知される必要があります。読み込まれた RewardedInterstitialAd オブジェクトは、onRewardedInterstitialAdLoaded() コールバックのパラメータとして提供されます。
次の例は、MainActivity で RewardedInterstitialAd を読み込む方法を示しています。
Java
Kotlin
AD_UNIT_ID は、実際の広告ユニット ID に置き換えてください。
コールバックを登録する
表示イベントに関する通知を受け取るには、広告のセッターに FullScreenContentCallback オブジェクトを渡す必要があります。FullScreenContentCallback オブジェクトは、広告の表示が成功または失敗した場合のコールバックと、広告が閉じられた場合のコールバックを処理します。次のコードは、RewardedInterstitialAdLoadCallback 内で匿名の FullScreenContentCallback オブジェクトを設定する方法を示しています。
Java
    rewardedInterstitialAd.setFullScreenContentCallback(
        new FullScreenContentCallback() {
          @Override
          public void onAdDismissedFullScreenContent() {
            // Called when fullscreen content is dismissed.
            Log.d(TAG, "The ad was dismissed.");
            // Make sure to set your reference to null so you don't
            // show it a second time.
            rewardedInterstitialAd = null;
            if (googleMobileAdsConsentManager.canRequestAds()) {
              loadRewardedInterstitialAd();
            }
          }
          @Override
          public void onAdFailedToShowFullScreenContent(AdError adError) {
            // Called when fullscreen content failed to show.
            Log.d(TAG, "The ad failed to show.");
            // Make sure to set your reference to null so you don't
            // show it a second time.
            rewardedInterstitialAd = null;
          }
          @Override
          public void onAdShowedFullScreenContent() {
            // Called when fullscreen content is shown.
            Log.d(TAG, "The ad was shown.");
          }
          @Override
          public void onAdImpression() {
            // Called when an impression is recorded for an ad.
            Log.d(TAG, "The ad recorded an impression.");
          }
          @Override
          public void onAdClicked() {
            // Called when ad is clicked.
            Log.d(TAG, "The ad was clicked.");
          }
        });
    rewardedInterstitialAd.show(
        MainActivity.this,
        new OnUserEarnedRewardListener() {
          @Override
          public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
            Log.d(TAG, "The user earned the reward.");
            // Handle the reward.
            int rewardAmount = rewardItem.getAmount();
            String rewardType = rewardItem.getType();
          }
        });
  }
  private void initializeMobileAdsSdk() {
    if (isMobileAdsInitializeCalled.getAndSet(true)) {
      return;
    }
    // Set your test devices.
    MobileAds.setRequestConfiguration(
        new RequestConfiguration.Builder()
            .setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
            .build());
    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              MobileAds.initialize(this, initializationStatus -> {});
              // Load an ad on the main thread.
              runOnUiThread(() -> loadRewardedInterstitialAd());
            })
        .start();
  }
}
Kotlin
rewardedInterstitialAd?.fullScreenContentCallback =
  object : FullScreenContentCallback() {
    override fun onAdDismissedFullScreenContent() {
      // Called when fullscreen content is dismissed.
      Log.d(TAG, "Ad was dismissed.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      rewardedInterstitialAd = null
    }
    override fun onAdFailedToShowFullScreenContent(adError: AdError) {
      // Called when fullscreen content failed to show.
      Log.d(TAG, "Ad failed to show.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      rewardedInterstitialAd = null
    }
    override fun onAdShowedFullScreenContent() {
      // Called when fullscreen content is shown.
      Log.d(TAG, "Ad showed fullscreen content.")
    }
    override fun onAdImpression() {
      // Called when an impression is recorded for an ad.
      Log.d(TAG, "Ad recorded an impression.")
    }
    override fun onAdClicked() {
      // Called when an ad is clicked.
      Log.d(TAG, "Ad was clicked.")
    }
  }
広告を表示する
リワード インタースティシャル広告を表示する際は、OnUserEarnedRewardListener オブジェクトを使用してリワード イベントを処理します。
Java
rewardedInterstitialAd.show(
    MainActivity.this,
    new OnUserEarnedRewardListener() {
      @Override
      public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
        Log.d(TAG, "The user earned the reward.");
        // Handle the reward.
        int rewardAmount = rewardItem.getAmount();
        String rewardType = rewardItem.getType();
      }
    });
Kotlin
rewardedInterstitialAd?.show(this) { rewardItem ->
  Log.d(TAG, "User earned the reward.")
  // Handle the reward.
  val rewardAmount = rewardItem.amount
  val rewardType = rewardItem.type
}
GitHub の例
次のステップ
次のトピックについて確認します。