獎勵廣告是一種可讓使用者選擇互動,以換取應用程式內獎勵的廣告形式。本指南說明如何在 Flutter 應用程式中整合 Ad Manager 的獎勵廣告。
請務必使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告;若未遵守此規定,可能導致帳戶遭到停權。
如要載入測試廣告,最簡單的方法是使用獎勵廣告專用的測試廣告單元 ID:
/21775744923/example/rewarded
測試廣告單元會在每次請求時傳回測試廣告。在編寫程式碼、測試及偵錯階段,您可以自由在應用程式中使用這些廣告單元,但發布前記得要換成自己的廣告單元 ID。
載入廣告
以下是載入獎勵廣告的程式碼範例:
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/21775744923/example/rewarded'; /// Loads a rewarded ad. void loadAd() { RewardedAd.loadWithAdManagerAdRequest( adUnitId: adUnitId, adManagerAdRequest: const AdManagerAdRequest(), adLoadCallback: RewardedAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { debugPrint('$ad loaded.'); // Keep a reference to the ad so you can show it later. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('RewardedAd failed to load: $error'); }, )); } }
獎勵廣告事件
您可以使用 FullScreenContentCallback
監聽生命週期事件,例如廣告顯示或關閉的時刻。只要在顯示廣告前設定 RewardedAd.fullScreenContentCallback
,即可接收這些事件通知。以下示範如何實作每個方法,並將訊息記錄到控制台:
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/21775744923/example/rewarded'; /// Loads a rewarded ad. void loadAd() { RewardedAd.loadWithAdManagerAdRequest( adUnitId: adUnitId, adManagerAdRequest: const AdManagerAdRequest(), adLoadCallback: RewardedAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { ad.fullScreenContentCallback = FullScreenContentCallback( // Called when the ad showed the full screen content. onAdShowedFullScreenContent: (ad) {}, // Called when an impression occurs on the ad. onAdImpression: (ad) {}, // Called when the ad failed to show full screen content. onAdFailedToShowFullScreenContent: (ad, err) { // Dispose the ad here to free resources. ad.dispose(); }, // Called when the ad dismissed full screen content. onAdDismissedFullScreenContent: (ad) { // Dispose the ad here to free resources. ad.dispose(); }, // Called when a click is recorded for an ad. onAdClicked: (ad) {}); debugPrint('$ad loaded.'); // Keep a reference to the ad so you can show it later. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('RewardedAd failed to load: $error'); }, )); } }
顯示廣告
RewardedAd
會重疊在所有應用程式內容上,而且位置固定,因此無法加入 Flutter 小工具樹狀結構。您可以藉由呼叫 show()
來選擇何時顯示廣告。RewardedAd.show()
需要接收 OnUserEarnedRewardCallback
回呼函式,該函式會在使用者獲得獎勵時觸發。請務必實作這個函式,並在使用者觀看廣告後給予獎勵。
_rewardedAd.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) { // Reward the user for watching an ad. });
呼叫 show()
後,以這種方式顯示的 Ad
無法以程式輔助移除,必須由使用者操作才能關閉。RewardedAd
只能顯示一次,再次呼叫 show() 會觸發 onAdFailedToShowFullScreenContent
。
不再需要某則廣告時,請務必釋放相關資源。最佳做法是在 FullScreenContentCallback.onAdDismissedFullScreenContent
和 FullScreenContentCallback.onAdFailedToShowFullScreenContent
回呼中呼叫 dispose()
。
大功告成!您的應用程式已能順利顯示獎勵廣告。