獎勵廣告

獎勵廣告 您可以使用應用程式內產品來獎勵使用者,讓他們與影片廣告、可試玩廣告和問卷調查互動,

先備知識

  • Google Mobile Ads SDK 19.7.0 以上版本。
  • 完成入門指南

一律使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非即時的實際廣告。否則可能導致帳戶遭到停權。

要載入測試廣告,最簡單的方法是使用 Android 獎勵廣告的專屬測試廣告單元 ID:

/6499/example/rewarded

這項機制經過特別設定,可針對每個請求傳回測試廣告,您可以在編寫程式碼、進行測試及偵錯時,自由用於自家的應用程式。但別忘了在發布應用程式前,用自己的廣告單元 ID 取代這個 ID。

如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」一文。

載入獎勵廣告物件

如要載入獎勵廣告,請在 RewardedAd 類別上呼叫靜態 load() 方法,並傳入 RewardedAdLoadCallback。這通常在 ActivityonCreate() 方法中執行。請注意,如同其他格式載入回呼,RewardedAdLoadCallback 會使用 LoadAdError 提供更精確的錯誤詳細資料。

Java

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {
  private RewardedAd rewardedAd;
  private final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
    RewardedAd.load(this, "/6499/example/rewarded",
      adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          // Handle the error.
          Log.d(TAG, loadAdError.toString());
          rewardedAd = null;
        }

        @Override
        public void onAdLoaded(@NonNull RewardedAd ad) {
          rewardedAd = ad;
          Log.d(TAG, "Ad was loaded.");
        }
    });
  }
}

Kotlin

class MainActivity : AppCompatActivity() {

  private var rewardedAd: RewardedAd? = null
  private final var TAG = "MainActivity"

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var adRequest = AdManagerAdRequest.Builder().build()
    RewardedAd.load(this,"/6499/example/rewarded", adRequest, object : RewardedAdLoadCallback() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        Log.d(TAG, adError?.toString())
        rewardedAd = null
      }

      override fun onAdLoaded(ad: RewardedAd) {
        Log.d(TAG, "Ad was loaded.")
        rewardedAd = ad
       }
    })
  }
}

設定 FullScreenContentCallback

FullScreenContentCallback 會處理與顯示 RewardedAd 相關的事件。顯示 RewardedAd 之前,請務必將回呼設為像這樣:

Java

rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
  @Override
  public void onAdClicked() {
    // Called when a click is recorded for an ad.
    Log.d(TAG, "Ad was clicked.");
  }

   @Override
  public void onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad dismissed fullscreen content.");
    rewardedAd = null;
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.");
    rewardedAd = null;
  }

  @Override
  public void onAdImpression() {
    // Called when an impression is recorded for an ad.
    Log.d(TAG, "Ad recorded an impression.");
  }

  @Override
  public void onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad showed fullscreen content.");
  }
});

Kotlin

rewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
  override fun onAdClicked() {
    // Called when a click is recorded for an ad.
    Log.d(TAG, "Ad was clicked.")
  }

  override fun onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad dismissed fullscreen content.")
    rewardedAd = null
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.")
    rewardedAd = null
  }

  override fun onAdImpression() {
    // Called when an impression is recorded for an ad.
    Log.d(TAG, "Ad recorded an impression.")
  }

  override fun onAdShowedFullScreenContent() {
    // Called when ad is shown.
    Log.d(TAG, "Ad showed fullscreen content.")
  }
}

放送廣告

顯示獎勵廣告時,您將使用 OnUserEarnedRewardListener 物件處理獎勵事件。

Java

if (rewardedAd != null) {
  Activity activityContext = MainActivity.this;
  rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

Kotlin

rewardedAd?.let { ad ->
  ad.show(this, OnUserEarnedRewardListener { rewardItem ->
    // Handle the reward.
    val rewardAmount = rewardItem.amount
    val rewardType = rewardItem.type
    Log.d(TAG, "User earned the reward.")
  })
} ?: run {
  Log.d(TAG, "The rewarded ad wasn't ready yet.")
}

常見問題

初始化呼叫是否有逾時?
10 秒後,即使中介服務聯播網尚未完成初始化,Google Mobile Ads SDK 仍會叫用 OnInitializationCompleteListener
如果我收到初始化回呼時,有些中介服務聯播網還沒準備就緒,該怎麼辦?

建議您在 OnInitializationCompleteListener 的回呼中載入廣告。即使中介服務聯播網尚未準備就緒,Google Mobile Ads SDK 仍會要求該聯播網提供廣告。因此,如果中介服務聯播網在逾時後完成初始化作業,仍可在該工作階段為日後的廣告請求提供服務。

您可以呼叫 MobileAds.getInitializationStatus(),繼續在應用程式工作階段中,輪詢所有轉接程式的初始化狀態。

如何找出特定中介服務聯播網尚未準備就緒的原因?

AdapterStatus.getDescription() 說明轉接程式為何還不能處理廣告請求。

是否一律會在 onAdDismissedFullScreenContent() 回呼之前呼叫 onUserEarnedReward() 回呼?

Google Ads 的所有 onUserEarnedReward() 呼叫都會在 onAdDismissedFullScreenContent() 之前發生。對於透過中介服務放送的廣告,回呼順序取決於第三方廣告聯播網 SDK 的實作方式。如果廣告聯播網 SDK 提供包含獎勵資訊的單一關閉回呼,中介服務轉接程式會在 onAdDismissedFullScreenContent() 之前叫用 onUserEarnedReward()

GitHub 上的範例

後續步驟

探索下列主題: