已獲得獎勵

選取平台: Android iOS Unity Flutter

使用者可選擇與獎勵廣告互動,換取應用程式內獎勵。本指南說明如何將 AdMob 的獎勵廣告整合到 Flutter 應用程式中。

一律使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告。否則帳戶可能會遭到停權。

如要載入測試廣告,最簡單的方法是使用專為獎勵廣告提供的測試廣告單元 ID:

Android

ca-app-pub-3940256099942544/5224354917

iOS

ca-app-pub-3940256099942544/1712485313

測試廣告單元已設定為針對每個請求傳回測試廣告,您可以在編寫、測試及偵錯程式碼時,自由地在自己的應用程式中使用這些廣告單元。發布應用程式前,請務必將這些 ID 換成您自己的廣告單元 ID。

載入廣告

以下範例會載入獎勵廣告:

class RewardedExampleState extends State<RewardedExample> {
  RewardedAd? _rewardedAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/5224354917'
    : 'ca-app-pub-3940256099942544/1712485313';

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        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 = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/5224354917'
    : 'ca-app-pub-3940256099942544/1712485313';

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        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只能顯示一次。後續呼叫顯示畫面時,系統會觸發 onAdFailedToShowFullScreenContent

不再需要存取廣告時,必須將廣告物件捨棄。呼叫 dispose() 的最佳做法位於 FullScreenContentCallback.onAdDismissedFullScreenContentFullScreenContentCallback.onAdFailedToShowFullScreenContent 回呼中。

大功告成!您的應用程式現在可以顯示獎勵廣告。

GitHub 上的完整範例

獎勵廣告