插頁式獎勵廣告

選取平台: Android iOS Unity Flutter

插頁式獎勵廣告是一種獎勵廣告格式,可在應用程式自然轉換時自動顯示,向使用者發放獎勵。與獎勵廣告不同的是,插頁式獎勵廣告不需等使用者選擇觀看即可放送。本指南說明如何將 AdMob 的插頁式獎勵廣告整合至 Flutter 應用程式。

必備條件

  • Flutter 外掛程式 1.1.0 以上版本。
  • 完成入門指南步驟。您的 Flutter 應用程式已匯入 Google Mobile Ads Flutter 外掛程式。

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

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

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

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

測試廣告單元會在每次請求時傳回測試廣告。在編寫程式碼、測試及偵錯階段,您可以自由在應用程式中使用這些廣告單元,但發布前記得要換成自己的廣告單元 ID。

載入廣告

以下是載入插頁式獎勵廣告的程式碼範例:

RewardedInterstitialAd.load(
  adUnitId: "_adUnitId",
  request: const AdRequest(),
  rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
    onAdLoaded: (RewardedInterstitialAd ad) {
      // Called when an ad is successfully received.
      debugPrint('Ad was loaded.');
      // Keep a reference to the ad so you can show it later.
      _rewardedInterstitialAd = ad;
    },
    onAdFailedToLoad: (LoadAdError error) {
      // Called when an ad request failed.
      debugPrint('Ad failed to load with error: $error');
    },
  ),
);

_adUnitId 替換為廣告單元 ID。

插頁式獎勵廣告事件

您可以使用 FullScreenContentCallback 監聽生命週期事件,例如廣告顯示或關閉的時刻。只要在顯示廣告前設定 RewardedInterstitialAd.fullScreenContentCallback,即可接收這些事件通知。這個範例會導入每個方法,並在控制台中記下資訊。

ad.fullScreenContentCallback = FullScreenContentCallback(
  onAdShowedFullScreenContent: (ad) {
    // Called when the ad showed the full screen content.
    debugPrint('Ad showed full screen content.');
  },
  onAdFailedToShowFullScreenContent: (ad, err) {
    // Called when the ad failed to show full screen content.
    debugPrint('Ad failed to show full screen content with error: $err');
    // Dispose the ad here to free resources.
    ad.dispose();
  },
  onAdDismissedFullScreenContent: (ad) {
    // Called when the ad dismissed full screen content.
    debugPrint('Ad was dismissed.');
    // Dispose the ad here to free resources.
    ad.dispose();
  },
  onAdImpression: (ad) {
    // Called when an impression occurs on the ad.
    debugPrint('Ad recorded an impression.');
  },
  onAdClicked: (ad) {
    // Called when a click is recorded for an ad.
    debugPrint('Ad was clicked.');
  },
);

多媒體廣告

RewardedInterstitialAd 會重疊在所有應用程式內容上,而且位置固定,因此無法加入 Flutter 小工具樹狀結構。您可以藉由呼叫 show() 來選擇何時顯示廣告。RewardedInterstitialAd.show() 需要接收 OnUserEarnedRewardCallback 回呼函式,該函式會在使用者獲得獎勵時觸發。請務必實作這個函式,並在使用者觀看廣告後給予獎勵。

_rewardedInterstitialAd?.show(
  onUserEarnedReward: (AdWithoutView view, RewardItem rewardItem) {
    debugPrint('Reward amount: ${rewardItem.amount}');
  },
);

呼叫 show() 後,以這種方式顯示的 Ad 無法以程式輔助移除,必須由使用者操作才能關閉。RewardedInterstitialAd 只能顯示一次,再次呼叫 show() 會觸發 onAdFailedToShowFullScreenContent

不再需要某則廣告時,請務必釋放相關資源。最佳做法是在 FullScreenContentCallback.onAdDismissedFullScreenContentFullScreenContentCallback.onAdFailedToShowFullScreenContent 回呼中呼叫 dispose()

[選用] 驗證伺服器端驗證 (SSV) 回呼

如果應用程式需在伺服器端驗證回呼中加入額外資料,請使用獎勵廣告的自訂資料功能。在獎勵廣告物件上設定的任何字串值,都會傳遞至 SSV 回呼的 custom_data 查詢參數。如未設定任何自訂資料值,SSV 回呼就不會包含 custom_data 查詢參數值。

下列程式碼範例示範如何在載入插頁式獎勵廣告後設定 SSV 選項:

RewardedInterstitialAd.load(
  adUnitId: "_adUnitId",
  request: AdRequest(),
  rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
    onAdLoaded: (ad) {
      ServerSideVerificationOptions _options =
          ServerSideVerificationOptions(
              customData: 'SAMPLE_CUSTOM_DATA_STRING');
      ad.setServerSideOptions(_options);
      _rewardedInterstitialAd = ad;
    },
    onAdFailedToLoad: (error) {},
  ),
);

SAMPLE_CUSTOM_DATA_STRING 替換成自訂資料。

GitHub 上的完整範例

插頁式獎勵廣告