插頁式獎勵廣告是一種獎勵廣告格式,可在應用程式自然轉換時自動顯示,向使用者發放獎勵。與獎勵廣告不同的是,插頁式獎勵廣告不需等使用者選擇觀看即可放送。
必要條件
導入方式
整合插頁式獎勵廣告的主要步驟如下:
- 載入廣告
- 註冊全螢幕事件回呼
- 處理獎勵回呼
- 顯示廣告
- 選用:驗證 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; isLoadingAds = false; } @Override public void onAdFailedToLoad(LoadAdError loadAdError) { Log.d(TAG, "onAdFailedToLoad: " + loadAdError.getMessage()); rewardedInterstitialAd = null; isLoadingAds = false; } });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 isLoadingAds = false } override fun onAdFailedToLoad(adError: LoadAdError) { Log.d(TAG, "onAdFailedToLoad: ${adError.message}") rewardedInterstitialAd = null isLoadingAds = false } }, )將 AD_UNIT_ID 替換為廣告單元 ID。
註冊回呼
您需要將 FullScreenContentCallback 物件傳給廣告的 setter 方法,才能接收廣告顯示事件的通知。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 查詢參數。若沒有設定任何自訂資料值,SSV 回呼不會包含 custom_data 查詢參數值。
請參考以下程式碼範例,瞭解如何在請求廣告前,於插頁式獎勵廣告物件上設定自訂資料。
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 上的範例
後續步驟
請參閱下列主題: