インタースティシャル広告は、ホストアプリのインターフェースを覆うようにフルスクリーンで表示される広告です。通常は、次のアクティビティの間や、ゲームのレベルが切り替わる合間など、アプリの操作中に画面が自然に切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先に移動するか、広告を閉じてアプリに戻るかを選択できます。
このガイドでは、インタースティシャル広告を Flutter アプリに組み込む方法について説明します。
必ずテスト広告でテストする
アプリの作成とテストでは、実際の配信中の広告ではなくテスト広告を使用するようにしてください。実際の広告を使用すると、アカウントが停止される可能性があります。
次に示すインタースティシャル用のテスト専用広告ユニット ID を使用すると、テスト広告を簡単に読み込むことができます。
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
テスト広告ユニットは、リクエストごとにテスト広告を返すように構成されており、コーディング、テスト、デバッグを行う際、ご自身のアプリで自由に使用できます。アプリを公開する前に、必ず独自の広告ユニット 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 管理画面で作成します(まだ作成していない場合)。