リワード広告は、ユーザーが広告を操作することと引き換えにアプリ内で報酬を獲得できる広告です。このガイドでは、リワード広告を AdMob Flutter アプリに組み込む方法について説明します。
テストは必ずテスト広告で行う
アプリの作成とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。
以下のリワード広告向けのテスト専用広告ユニット ID を使うと、テスト広告を簡単に読み込むことができます。
Android
ca-app-pub-3940256099942544/5224354917
iOS
ca-app-pub-3940256099942544/1712485313
テスト広告ユニットは、リクエストごとにテスト広告を返すように設定されており、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、テスト用 ID は、アプリを公開する前に必ずご自身の広告ユニット ID に置き換えてください。
広告を読み込む
次のサンプルは、リワード広告を読み込みます。
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = Platform.isAndroid ? 'ca-app-pub-3940256099942544/5224354917' : 'ca-app-pub-3940256099942544/1712485313'; /// Loads a rewarded ad. void loadAd() { RewardedAd.load( adUnitId: adUnitId, request: const AdRequest(), adLoadCallback: RewardedAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { debugPrint('$ad loaded.'); // Keep a reference to the ad so you can show it later. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('RewardedAd failed to load: $error'); }, )); } }
リワード広告イベント数
FullScreenContentCallback
を使用すると、広告の表示や閉じられたライフサイクル イベントをリッスンできます。これらのイベントの通知を受け取るには、広告を表示する前に RewardedAd.fullScreenContentCallback
を設定してください。各メソッドを実装してメッセージのログをコンソールに記録する場合のサンプルを、次に示します。
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = Platform.isAndroid ? 'ca-app-pub-3940256099942544/5224354917' : 'ca-app-pub-3940256099942544/1712485313'; /// Loads a rewarded ad. void loadAd() { RewardedAd.load( adUnitId: adUnitId, request: const AdRequest(), adLoadCallback: RewardedAdLoadCallback( // 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. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { debugPrint('RewardedAd failed to load: $error'); }, )); } }
表示広告
RewardedAd
は、すべてのアプリ コンテンツの上にオーバーレイとして表示され、静的に配置されます。そのため、Flutter ウィジェット ツリーに追加することはできません。広告を表示するタイミングを選択するには、show()
を呼び出します。RewardedAd.show()
は、ユーザーが特典を獲得したときに呼び出される OnUserEarnedRewardCallback
を受け取ります。広告を視聴したユーザーに報酬を付与することを忘れないでください。
_rewardedAd.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) { // Reward the user for watching an ad. });
show()
が呼び出されると、この方法で表示された Ad
はプログラムで削除できず、ユーザー入力が必要になります。RewardedAd
は 1 回だけ表示できます。それ以降の呼び出しは、onAdFailedToShowFullScreenContent
をトリガーします。
広告にアクセスする必要がなくなったときには、広告を破棄する必要があります。dispose()
を呼び出すタイミングに関するベスト プラクティスは、FullScreenContentCallback.onAdDismissedFullScreenContent
コールバックと FullScreenContentCallback.onAdFailedToShowFullScreenContent
コールバックです。
これで完了です。これで、アプリにリワード広告を表示できるようになりました。