1 つのリワード インタースティシャル広告を読み込む

リワード インタースティシャル広告は、アプリの画面が変わる自然なタイミングで自動的に表示される広告に対して報酬を提供できるフォーマットです。リワード広告とは異なり、ユーザーはリワード インタースティシャル広告を表示するためにオプトインする必要はありません。 詳しくは、リワード インタースティシャルに関するガイドラインをご覧ください。

このガイドでは、Android アプリにリワード インタースティシャル広告を組み込む方法を説明します。

始める前に

先に進む前に、次のことを実施してください。

  • GMA Next-Gen SDK を設定します。
  • テスト用のリワード インタースティシャル広告ユニット ID ca-app-pub-3940256099942544/5354046379 を使用します。
    • アプリの開発とテストでは、配信中の実際の広告ではなく、必ずテスト広告を使用してください。テスト広告ユニット ID を使用しない場合、アカウントが停止される可能性があります。
    • アプリを公開する前に、この ID を広告ユニット ID に置き換えてください。
    • GMA Next-Gen SDK のテスト広告について詳しくは、テスト広告を有効にするをご覧ください。

広告を読み込む

次の例では、1 つの広告を読み込んでいます。

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

// 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;
      }
    });

テストの際は、テスト広告ユニット ID ca-app-pub-3940256099942544/5354046379 を使用します。

広告を表示する

リワード インタースティシャル広告を表示するには、show メソッドを呼び出します。リワード イベントを処理するには、OnUserEarnedRewardListener オブジェクトを使用します。

次の例では、広告を表示しています。

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

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();
        }
      });
}

広告イベントをリッスンする

広告を表示する前に、広告のライフサイクル イベントをリッスンします。次の例では、広告イベントのコールバックを登録しています。

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

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.
        }
      });
}

任意: サーバーサイド認証(SSV)コールバックを検証する

サーバーサイド認証(SSV)コールバックでユーザーとアプリのコンテキストを渡すには、広告を表示する前に広告オブジェクトに検証オプションを設定します。SSV コールバックはセッション状態なしで Google からサーバーに届くため、次のオプションの文字列を添付します。Google は、これらの文字列を SSV コールバックの署名付きクエリ パラメータとして変更せずに転送します。

  • user_id クエリ パラメータ: クレジットを付与するユーザー(内部アカウント ID など)を識別します。
  • custom_data クエリ パラメータ: 報酬のプレースメント、ゲームレベル、サーバー生成のノンスなどのアプリデータを渡します。

値を設定しない場合、Google は SSV コールバックからそのクエリ パラメータを省略します。

次の例では、広告を表示する前に、広告オブジェクトにカスタムデータを設定しています。

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

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);
      }
    });

SAMPLE_CUSTOM_DATA_STRING は、実際のカスタムデータに置き換えてください。

キーポイント: custom_data クエリ パラメータはパーセント エンコードされています。値を解析する前に、サーバーで文字列を URL デコードし、コールバック署名を検証してから、user_id または custom_data クエリ パラメータを信頼します。