必要條件
- Google Mobile Ads SDK 19.7.0 以上版本。
- 完成入門指南。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告。否則帳戶可能會遭到停權。
如要載入測試廣告,最簡單的方法是使用 Android 獎勵廣告專用的測試廣告單元 ID:
ca-app-pub-3940256099942544/5224354917
這個 ID 經過特別設定,可針對每項要求傳回測試廣告,您可以在編寫、測試及偵錯時,自由地在自己的應用程式中使用。發布應用程式前,請務必將其替換為您自己的廣告單元 ID。
如要進一步瞭解 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 換成自訂資料。
如要設定自訂獎勵字串,請務必在顯示廣告前完成設定。
常見問題
- 初始化呼叫是否會逾時?
- 10 秒後,即使中介服務聯播網尚未完成初始化,Google Mobile Ads SDK 也會叫用
OnInitializationCompleteListener
。 - 如果我收到初始化回呼時,部分中介服務聯播網尚未準備就緒,該怎麼辦?
建議在
OnInitializationCompleteListener
的回呼中載入廣告。即使中介服務聯播網尚未準備就緒,Google Mobile Ads SDK 仍會向該聯播網要求廣告。因此,如果中介服務聯播網在逾時後完成初始化,仍可在該工作階段中處理未來的廣告請求。您可以在整個應用程式工作階段中呼叫
MobileAds.getInitializationStatus()
,持續輪詢所有轉接程式的初始化狀態。- 如何找出特定中介服務聯播網未就緒的原因?
AdapterStatus.getDescription()
說明轉接程式為何尚未準備好處理廣告請求。onUserEarnedReward()
回呼是否一律會在onAdDismissedFullScreenContent()
回呼之前呼叫?如果是 Google 廣告,所有
onUserEarnedReward()
呼叫都會在onAdDismissedFullScreenContent()
之前發生。如果是透過中介服務放送的廣告,回呼順序是由第三方廣告聯播網 SDK 的實作方式決定。對於提供單一關閉回呼和獎勵資訊的廣告聯播網 SDK,中介服務介面卡會在onAdDismissedFullScreenContent()
之前叫用onUserEarnedReward()
。
GitHub 上的範例
後續步驟
請參閱下列主題: