插頁式獎勵廣告 (Beta 版)

插頁式獎勵廣告是一種獎勵廣告格式,可讓您在應用程式自然轉換時自動顯示廣告,並提供獎勵。與獎勵廣告不同的是,插頁式獎勵廣告不需要等使用者確定選擇觀看。本指南將說明如何將 AdMob 的獎勵插頁式廣告整合至 Flutter 應用程式。

必要條件

  • Flutter 外掛程式 1.1.0 以上版本。
  • 完成「開始使用」。Flutter 應用程式應已匯入 Google 行動廣告 Flutter 外掛程式。

請務必使用測試廣告進行測試

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

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

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

測試廣告單元會在每個請求中傳回測試廣告,您可以在編寫程式碼、測試和偵錯時,在自己的應用程式中自由使用這些廣告。請務必先將這些內容換成您自己的廣告單元 ID,再發布應用程式。

載入廣告

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

class RewardedInterstitialExampleState extends State<RewardedInterstitialExample> {
  RewardedInterstitialAd? _rewardeInterstitialdAd;

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

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedInterstitialAd.load(
        adUnitId: adUnitId,
        adRequest: const AdRequest(),
        adLoadCallback: RewardedInterstitialAdLoadCallback(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
            debugPrint('$ad loaded.');
            // Keep a reference to the ad so you can show it later.
            _rewardedInterstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('RewardedInterstitialAd failed to load: $error');
          },
        ));
  }
}

插頁式獎勵廣告事件

您可以使用 FullScreenContentCallback 監聽生命週期事件,例如廣告顯示或關閉的時間。請先設定 RewardedInterstitialAd.fullScreenContentCallback,再放送廣告,以便接收這些事件的通知。這個範例會實作每個方法,並將訊息記錄到控制台:

class RewardedInterstitialExampleState extends State<RewardedInterstitialExample> {
  RewardedInterstitialAd? _rewardedInterstitialAd;

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

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedInterstitialAd.load(
        adUnitId: adUnitId,
        adRequest: const AdRequest(),
        adLoadCallback: RewardedInterstitialAdLoadCallback(
          // 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.
            _rewardedInterstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugprint('RewardedInterstitialAd failed to load: $error');
          },
        ));
  }
}

多媒體廣告

RewardedInterstitialAd 會以重疊方式顯示在所有應用程式內容上方,並以靜態方式放置,因此無法新增至 Flutter 小工具樹狀結構。您可以呼叫 show(),選擇廣告顯示時間。RewardedInterstitialAd.show() 會採用 OnUserEarnedRewardCallback,並在使用者獲得獎勵時叫用。請務必實作這項功能,並在使用者觀看廣告時提供獎勵。

_rewardedInterstitialAd.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) {
  // Reward the user for watching an ad.
});

呼叫 show() 後,以這種方式顯示的 Ad 就無法以程式輔助方式移除,必須由使用者輸入。RewardedInterstitialAd 只能顯示一次。後續呼叫也會觸發 onAdFailedToShowFullScreenContent

不再需要放送的廣告時,必須予以處理。呼叫 dispose() 的最佳做法是在 FullScreenContentCallback.onAdDismissedFullScreenContentFullScreenContentCallback.onAdFailedToShowFullScreenContent 回呼中。

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

GitHub 上的完整範例

插頁式獎勵廣告