インタースティシャル広告は、ホストアプリのインターフェース上に全画面表示される広告です。通常は、アプリの操作が切り替わる合間や、ゲームのステージが切り替わる合間など、アプリの操作中に画面が切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先に移動するか、広告を閉じてアプリに戻るかを選択できます。
このガイドでは、インタースティシャル広告を Flutter アプリに組み込む方法について説明します。
テストは必ずテスト広告で行う
アプリの作成とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。
次に示すインタースティシャル テスト専用広告ユニット ID を使うと、テスト広告を簡単に読み込むことができます。
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
テスト広告ユニットは、リクエストごとにテスト広告を返すように設定されており、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、テスト用 ID は、アプリを公開する前に必ずご自身の広告ユニット ID に置き換えてください。
広告を読み込む
次の例では、インタースティシャル広告を読み込みます。
class InterstitialExampleState extends State<InterstitialExample> { InterstitialAd? _interstitialAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = Platform.isAndroid ? 'ca-app-pub-3940256099942544/1033173712' : 'ca-app-pub-3940256099942544/4411468910'; /// Loads an interstitial ad. void loadAd() { InterstitialAd.load( adUnitId: adUnitId, request: const AdRequest(), adLoadCallback: InterstitialAdLoadCallback( // 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('InterstitialAd failed to load: $error'); }, )); } }
インタースティシャル広告のイベント
FullScreenContentCallback
を使用すると、広告の表示や閉じられたライフサイクル イベントをリッスンできます。これらのイベントの通知を受け取るには、広告を表示する前に InterstitialAd.fullScreenContentCallback
を設定してください。この例では、各メソッドを実装しています。
class InterstitialExampleState extends State<InterstitialExample> { InterstitialAd? _interstitialAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = Platform.isAndroid ? 'ca-app-pub-3940256099942544/1033173712' : 'ca-app-pub-3940256099942544/4411468910'; /// Loads an interstitial ad. void loadAd() { InterstitialAd.load( adUnitId: adUnitId, request: const AdRequest(), adLoadCallback: InterstitialAdLoadCallback( // 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('InterstitialAd failed to load: $error'); }, )); } }
インタースティシャル広告を表示する
InterstitialAd
は、すべてのアプリ コンテンツの上に Overlay
として表示され、静的に配置されます。そのため、Flutter ウィジェット ツリーに追加することはできません。広告を表示するタイミングを選択するには、show()
を呼び出します。
_interstitiaAd.show();
show()
が呼び出されると、この方法で表示された Ad
はプログラムで閉じることができず、ユーザー入力が必要になります。InterstitialAd
は 1 回だけ表示できます。それ以降の呼び出しは、onAdFailedToShowFullScreenContent
をトリガーします。
広告にアクセスする必要がなくなったときには、広告を破棄する必要があります。dispose()
を呼び出すタイミングに関するベスト プラクティスは、FullScreenContentCallback.onAdDismissedFullScreenContent
コールバックと FullScreenContentCallback.onAdFailedToShowFullScreenContent
コールバックです。
これで完了です。これで、アプリにインタースティシャル広告を表示する準備が整いました。
次のステップ
- インタースティシャル広告に関するベスト プラクティスとインタースティシャル広告に関するガイドラインをご覧ください。
- インタースティシャル広告の事例紹介をご覧ください。
- まだ作成していない場合は、AdMob 管理画面で独自のインタースティシャル広告ユニットを作成してください。