激励广告

所谓激励广告,指的是用户可以选择与之互动来换取应用内奖励的一种广告。本指南介绍了如何将 Ad Manager 激励广告植入到 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只能显示 一次。后续显示的调用将触发 onAdFailedToShowFullScreenContent

如果不再需要访问某个广告,则必须予以处置。最佳实践 调用 dispose() 的时间位于 FullScreenContentCallback.onAdDismissedFullScreenContentFullScreenContentCallback.onAdFailedToShowFullScreenContent 回调。

大功告成!现在,您的应用就可以展示激励广告了。