リワード広告は、ユーザーが広告を操作することと引き換えにアプリ内で報酬を獲得できる広告です。このガイドでは、リワード広告を Ad Manager から Flutter アプリに統合する方法について説明します。
常にテスト広告でテストする
アプリを作成してテストする際は、実際の本番環境広告ではなく、テスト広告を使用してください。実際の広告を使用すると、アカウントが停止される可能性があります。
リワード広告の読み込みには、専用のテスト広告ユニット ID を使うと簡単です。
/6499/example/rewarded
テスト広告ユニットは、リクエストごとにテスト広告を返すように構成されており、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、このテスト用 ID は、アプリを公開する前に必ずご自身の広告ユニット ID に置き換えてください。
広告を読み込む
次の例では、リワード広告を読み込みます。
class RewardedExampleState extends State<RewardedExample> { RewardedAd? _rewardedAd; // TODO: replace this test ad unit with your own ad unit. final adUnitId = '/6499/example/rewarded'; /// Loads a rewarded ad. void loadAd() { RewardedAd.loadWithAdManagerAdRequest( adUnitId: adUnitId, adManagerAdRequest: const AdManagerAdRequest(), 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 = '/6499/example/rewarded'; /// Loads a rewarded ad. void loadAd() { RewardedAd.loadWithAdManagerAdRequest( adUnitId: adUnitId, adManagerAdRequest: const AdManagerAdRequest(), 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
コールバックにあります。
これで、これで、アプリにリワード広告を表示できるようになりました。