必備條件
- Google Mobile Ads SDK 19.7.0 以上版本。
- 完成入門指南的步驟。
請一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告,否則可能導致帳戶遭停權。
如要載入測試廣告,最簡單的方法是使用 Android 獎勵廣告專用的測試廣告單元 ID:
/21775744923/example/rewarded
這類 ID 經特別設定,可針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,將其用於自己的應用程式。發布應用程式前,請務必將這類 ID 替換為自己的廣告單元 ID。
請參閱「測試廣告」,進一步瞭解 Google Mobile Ads SDK 測試廣告的運作方式。
載入獎勵廣告物件
如要載入獎勵廣告,請在 RewardedAd 類別中呼叫靜態 load() 方法,並傳入 RewardedAdLoadCallback。這通常是在 Activity 的 onCreate() 方法中完成。請注意,與其他格式載入回呼相似,RewardedAdLoadCallback
會運用 LoadAdError 提供更高保真的錯誤詳情。
Java
Kotlin
將 AD_UNIT_ID 替換為廣告單元 ID。
設定 FullScreenContentCallback
FullScreenContentCallback 會處理與顯示 RewardedAd 相關的事件。顯示 RewardedAd 前,請務必設定回呼,如下所示:
Java
rewardedAd.setFullScreenContentCallback(
    new FullScreenContentCallback() {
      @Override
      public void 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.
        rewardedAd = null;
      }
      @Override
      public void 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.
        rewardedAd = null;
      }
      @Override
      public void onAdShowedFullScreenContent() {
        // Called when fullscreen content is shown.
        Log.d(TAG, "Ad showed fullscreen content.");
      }
      @Override
      public void onAdImpression() {
        // Called when an impression is recorded for an ad.
        Log.d(TAG, "Ad recorded an impression.");
      }
      @Override
      public void onAdClicked() {
        // Called when an ad is clicked.
        Log.d(TAG, "Ad was clicked.");
      }
    });
Kotlin
rewardedAd?.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.
      rewardedAd = 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.
      rewardedAd = 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
rewardedAd.show(
    MainActivity.this,
    new OnUserEarnedRewardListener() {
      @Override
      public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
        Log.d(TAG, "User earned the reward.");
        // Handle the reward.
      }
    });
Kotlin
rewardedAd?.show(
  this,
  OnUserEarnedRewardListener { rewardItem ->
    Log.d(TAG, "User earned the reward.")
    // Handle the reward.
    val rewardAmount = rewardItem.amount
    val rewardType = rewardItem.type
  },
)
[選用] 驗證伺服器端驗證 (SSV) 回呼
伺服器端驗證回呼時,如果應用程式需要額外資料,請使用獎勵廣告的自訂資料功能。在獎勵廣告物件設定的字串值,會傳遞至 SSV 回呼的 custom_data 查詢參數。如未設定自訂資料值,SSV 回呼就不會出現 custom_data 查詢參數值。
請參考以下程式碼範例,瞭解如何在請求廣告前,於獎勵廣告物件設定自訂資料:
Java
Kotlin
將 SAMPLE_CUSTOM_DATA_STRING 替換成自訂資料。
如要設定自訂獎勵字串,請務必在廣告顯示前完成。