插页式激励广告(Beta 版)

请选择平台Android iOS Unity Flutter

插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户无需自行选择即可观看插页式激励广告。

前提条件

  • Google 移动广告 SDK 19.2.0 或更高版本。

实现

植入插页式激励广告的主要步骤如下所示:

  • 加载广告
  • 注册全屏事件回调
  • 处理奖励回调
  • 展示广告
  • [可选] 验证 SSV 回调

加载广告

广告的加载是通过对 RewardedInterstitialAd 类使用静态 load() 方法完成的。该加载方法需要使用情境、您的广告单元 ID、AdRequest 对象和 RewardedInterstitialAdLoadCallback(以在广告加载成功或失败时收到通知)。已加载的 RewardedInterstitialAd 对象会以 onRewardedInterstitialAdLoaded() 回调中的参数的形式提供。

以下示例展示了如何在 MainActivity 中加载 RewardedInterstitialAd

Java

RewardedInterstitialAd.load(
    MainActivity.this,
    "AD_UNIT_ID",
    new AdRequest.Builder().build(),
    new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        Log.d(TAG, "Ad was loaded.");
        rewardedInterstitialAd = ad;
      }

      @Override
      public void onAdFailedToLoad(LoadAdError loadAdError) {
        Log.d(TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
        rewardedInterstitialAd = null;
      }
    });

Kotlin

RewardedInterstitialAd.load(
  this,
  "AD_UNIT_ID",
  AdRequest.Builder().build(),
  object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(rewardedAd: RewardedInterstitialAd) {
      Log.d(TAG, "Ad was loaded.")
      rewardedInterstitialAd = rewardedAd
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      Log.d(TAG, "onAdFailedToLoad: ${adError.message}")
      rewardedInterstitialAd = null
    }
  },
)

AD_UNIT_ID 替换为您的广告单元 ID。

注册回调

要接收有关展示事件的通知,您必须向广告的 setter 方法传递 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.");
      }
    });

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
}

[可选] 验证服务器端验证 (SSV) 回调

对于需要服务器端验证回调中额外数据的应用,应使用激励广告的自定义数据功能。在激励广告对象上设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会出现在 SSV 回调中。

以下代码示例演示了如何在请求广告之前对插页式激励广告对象设置自定义数据。

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

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

SAMPLE_CUSTOM_DATA_STRING 替换为您的自定义数据。

如果您要设置自定义奖励字符串,则必须在展示广告之前设置。

GitHub 上的示例

后续步骤

不妨探索以下主题: