インタースティシャル広告は、ホストアプリのインターフェース上に全画面表示される広告です。通常は、次のアクティビティに移行する前や、ゲームレベルをクリアした後の合間など、アプリの操作中に画面が切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先 URL に移動するか、広告を閉じてアプリに戻るかを選択することになります。
このガイドでは、インタースティシャル広告を Flutter アプリに統合する方法について説明します。
常にテスト広告でテストする
アプリの開発とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。
テスト広告を読み込む際は、次に示すテスト専用の広告ユニット ID を使うと簡単です。
/6499/example/interstitial
テスト広告ユニットは、すべてのリクエストに対してテスト広告を返すように構成されており、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、このテスト用 ID は、アプリを公開する前に必ずご自身の広告ユニット ID に置き換えてください。
広告を読み込む
次の例では、インタースティシャル広告を読み込みます。
class InterstitialExampleState extends State<InterstitialExample> { AdManagerInterstitialAd? _interstitialAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/6499/example/interstitial'; /// Loads an interstitial ad. void loadAd() { AdManagerInterstitialAd.load( adUnitId: adUnitId, request: const AdManagerAdRequest(), adLoadCallback: AdManagerInterstitialAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { debugPrint('$ad loaded.'); // Keep a reference to the ad so you can show it later. _interstitialAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('AdManagerInterstitialAd failed to load: $error'); }, )); } }
インタースティシャル広告イベント
FullScreenContentCallback
を使用すると、広告の表示や非表示など、ライフサイクル イベントをリッスンできます。これらのイベントの通知を受け取るには、広告を表示する前に AdManagerInterstitialAd.fullScreenContentCallback
を設定します。この例では、各メソッドを実装します。
class InterstitialExampleState extends State<InterstitialExample> { AdManagerInterstitialAd? _interstitialAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/6499/example/interstitial'; /// Loads an interstitial ad. void loadAd() { AdManagerInterstitialAd.load( adUnitId: adUnitId, request: const AdManagerAdRequest(), adLoadCallback: AdManagerInterstitialAdLoadCallback( // 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. _interstitialAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('AdManagerInterstitialAd failed to load: $error'); }, )); } }
インタースティシャル広告を表示する
AdManagerInterstitialAd
は、すべてのアプリ コンテンツの上に Overlay
として表示され、静的に配置されます。そのため、Flutter ウィジェット ツリーには追加できません。広告を表示するタイミングを選択するには、show()
を呼び出します。
_interstitiaAd.show();
show()
が呼び出された後に、この方法で表示された Ad
をプログラムで閉じることはできないため、ユーザー入力が必要です。AdManagerInterstitialAd
は 1 回しか表示できません。それ以降の表示呼び出しで onAdFailedToShowFullScreenContent
がトリガーされます。
広告へのアクセス権が必要なくなったら、その広告は廃棄しなければなりません。dispose()
を呼び出すタイミングは、FullScreenContentCallback.onAdDismissedFullScreenContent
コールバックと FullScreenContentCallback.onAdFailedToShowFullScreenContent
コールバックがおすすめです。
これで、これで、アプリにインタースティシャル広告を表示できるようになりました。
次のステップ
- インタースティシャル広告に関するおすすめの方法とインタースティシャル広告に関するガイドラインをご覧ください。
- インタースティシャル広告の事例紹介をご覧ください。