リワード

プラットフォームを選択: Android iOS Unity Flutter

リワード広告は、ユーザーが広告を操作することと引き換えにアプリ内で報酬を獲得できる広告です。このガイドでは、アド マネージャーのリワード広告を Flutter アプリに統合する方法について説明します。

常にテスト広告でテストする

アプリの開発とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告でテストすると、アカウントが停止される場合があります。

下記のリワード広告向けのテスト専用広告ユニット ID を使うと、テスト広告を簡単に読み込むことができます。

  • /21775744923/example/rewarded

テスト広告ユニットは、すべてのリクエストに対してテスト広告を返すように設定されており、アプリのコーディング、テスト、デバッグで使用することができます。なお、この広告ユニットの ID は、アプリを公開する前に必ずご自身の広告ユニット ID に置き換えてください。

広告を読み込む

リワード広告を読み込む場合のサンプルを次に示します。

class RewardedExampleState extends State<RewardedExample> {
  RewardedAd? _rewardedAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = '/21775744923/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 = '/21775744923/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 のコールバック時です。

これで、アプリにリワード広告を表示できるようになりました。