插頁式獎勵廣告是一種獎勵廣告格式,您可在應用程式自然轉換時自動顯示這類廣告,向使用者提供獎勵。與獎勵廣告不同的是,插頁式獎勵廣告不需要等使用者確定選擇觀看即可放送。
必要條件
- Google Mobile Ads SDK 19.2.0 以上版本。
- 完成入門指南。
導入作業
整合插頁式獎勵廣告的主要步驟如下:
- 載入廣告
- 註冊全螢幕事件回呼
- 處理獎勵回呼
- 顯示廣告
- [選用] 驗證 SSV 回呼
載入廣告
載入廣告時,請使用 RewardedInterstitialAd
類別的靜態 load()
方法。載入方法需要 Context、廣告單元 ID、AdRequest
物件,以及在廣告載入成功或失敗時收到通知的 RewardedInterstitialAdLoadCallback
。載入的 RewardedInterstitialAd
物件會以參數形式提供給 onRewardedInterstitialAdLoaded()
回呼。
以下範例說明如何在 MainActivity
中載入 RewardedInterstitialAd
。
Java
Kotlin
將 AD_UNIT_ID 替換為廣告單元 ID。
註冊回呼
如要接收簡報事件的通知,您必須將 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
查詢參數。如果未設定任何自訂資料值,SSV 回呼中就不會出現 custom_data
查詢參數值。
下列程式碼範例示範如何在要求廣告前,於有獎中介層廣告物件上設定自訂資料。
Java
Kotlin
將 SAMPLE_CUSTOM_DATA_STRING 換成自訂資料。
如要設定自訂獎勵字串,請務必在顯示廣告前完成設定。
GitHub 上的範例
後續步驟
請參閱下列主題: