インタースティシャル広告は、ホストアプリのインターフェース上に全画面表示される広告です。通常は、次のアクティビティに移行する前や、ゲームレベルをクリアした後の合間など、アプリの操作中に画面が切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先 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
コールバックにあります。
これで、これで、アプリにインタースティシャル広告を表示する準備が整いました。
次のステップ
- インタースティシャル広告に関するおすすめの方法とインタースティシャル広告に関するガイドラインをご覧ください。
- インタースティシャル広告の事例紹介をご覧ください。