事前準備
繼續操作前,請先執行下列工作:
- 設定 Google Mobile Ads SDK (Legacy)。
- 使用測試獎勵廣告單元 ID
/21775744923/example/rewarded。- 建構及測試應用程式時,請務必使用測試廣告,而非正式投放的實際廣告。如果未使用測試廣告單元 ID,Google 可能會將您的帳戶停權。
- 發布應用程式前,請將這個 ID 換成廣告單元 ID。
- 如要進一步瞭解 Google Mobile Ads SDK (Legacy) 測試廣告,請參閱「啟用測試廣告」。
載入獎勵廣告物件
如要載入獎勵廣告,請在 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
},
)
如果是 Google 放送的廣告,Google Mobile Ads SDK (Legacy) 會先呼叫 onUserEarnedReward 方法,再呼叫 onAdDismissedFullScreenContent 方法。如果是透過中介服務放送的廣告,回呼順序則由第三方廣告來源決定。如果廣告來源僅提供一個包含獎勵資訊的關閉回呼,中介服務轉接程式會先呼叫 onUserEarnedReward 方法,再呼叫 onAdDismissedFullScreenContent 方法。詳情請參閱「設定中介服務」。
選用:驗證伺服器端驗證 (SSV) 回呼
伺服器端驗證回呼時,如果應用程式需要額外資料,請使用獎勵廣告的自訂資料功能。在獎勵廣告物件設定的字串值,會傳遞至 SSV 回呼的 custom_data 查詢參數。如未設定自訂資料值,SSV 回呼就不會出現 custom_data 查詢參數值。
請參考以下程式碼範例,瞭解如何在請求廣告前,於獎勵廣告物件設定自訂資料:
Java
Kotlin
將 SAMPLE_CUSTOM_DATA_STRING 替換成自訂資料。
如要設定自訂獎勵字串,請務必在廣告顯示前完成。
GitHub 上的範例
後續步驟
請參閱下列主題: