リワード広告は、ユーザーが広告を操作することと引き換えに、アプリ内で報酬を獲得できる広告です。このガイドでは、AdMob のリワード広告を Flutter アプリに統合する方法について説明します。
必ずテスト広告でテストする
アプリの作成とテストでは、実際の配信中の広告ではなくテスト広告を使用するようにしてください。実際の広告を使用すると、アカウントが停止される可能性があります。
リワード広告は、次に示す専用のテスト広告ユニット ID を使うと、最も簡単にテスト広告を読み込むことができます。
Android
ca-app-pub-3940256099942544/5224354917
iOS
ca-app-pub-3940256099942544/1712485313
テスト広告ユニットは、リクエストごとにテスト広告を返すように構成されており、コーディング、テスト、デバッグを行う際、ご自身のアプリで自由に使用できます。アプリを公開する前に、必ず独自の広告ユニット 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
コールバックで指定するものです。
これで、これで、アプリにリワード広告を表示できるようになりました。