插页式激励广告(Beta 版)

插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户 用户才能选择观看插页式激励广告。本指南介绍了如何 集成 AdMob 的插页式激励广告 转换为 Flutter 应用。

前提条件

  • Flutter 插件 1.1.0 或更高版本。
  • 完成入门指南。您的 Flutter 应用应该已经导入了 Google 移动广告 Flutter 插件。

务必用测试广告进行测试

在构建和测试应用时,请务必使用测试广告, 实际投放的广告。否则,可能会导致您的账号被中止。

要加载测试广告,最简便的方法就是使用我们的专用测试广告单元 ID 插页式激励广告:

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

测试广告单元已配置为针对每个请求返回测试广告 您可以在自己应用的编码、测试和调试过程中随意使用它们。 只需确保在发布前用您自己的广告单元 ID 替换这些测试广告单元 。

加载广告

以下示例加载了一个插页式激励广告:

class RewardedInterstitialExampleState extends State<RewardedInterstitialExample> {
  RewardedInterstitialAd? _rewardeInterstitialdAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/5354046379'
    : 'ca-app-pub-3940256099942544/6978759866';

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedInterstitialAd.load(
        adUnitId: adUnitId,
        adRequest: const AdRequest(),
        adLoadCallback: RewardedInterstitialAdLoadCallback(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
            debugPrint('$ad loaded.');
            // Keep a reference to the ad so you can show it later.
            _rewardedInterstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('RewardedInterstitialAd failed to load: $error');
          },
        ));
  }
}

插页式激励广告事件

通过使用 FullScreenContentCallback,您可以监听各种广告生命周期事件,例如广告何时展示或何时关闭。请在展示广告之前设置 RewardedInterstitialAd.fullScreenContentCallback,以便接收这些事件的相关通知。以下示例实现了每个方法,并将消息记录到控制台:

class RewardedInterstitialExampleState extends State<RewardedInterstitialExample> {
  RewardedInterstitialAd? _rewardedInterstitialAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/5354046379'
    : 'ca-app-pub-3940256099942544/6978759866';

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedInterstitialAd.load(
        adUnitId: adUnitId,
        adRequest: const AdRequest(),
        adLoadCallback: RewardedInterstitialAdLoadCallback(
          // 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.
            _rewardedInterstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugprint('RewardedInterstitialAd failed to load: $error');
          },
        ));
  }
}

展示广告

RewardedInterstitialAd 以叠加层的形式显示在所有应用内容之上 并且是静态放置的因此无法将其添加到 Flutter widget 树。 您可以通过调用 show() 来选择展示广告的时间。 RewardedInterstitialAd.show() 接受一个 OnUserEarnedRewardCallback, 在用户获得奖励时调用。请务必实施此功能,奖励 观看广告的用户

_rewardedInterstitialAd.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) {
  // Reward the user for watching an ad.
});

调用 show() 后,以这种方式展示的 Ad 便无法移除 并且需要用户输入RewardedInterstitialAd 仅可展示一次。随后会触发要展示的广告调用 onAdFailedToShowFullScreenContent

当不再需要广告时,必须处置该广告。调用 dispose() 的最佳实践是,在 FullScreenContentCallback.onAdDismissedFullScreenContentFullScreenContentCallback.onAdFailedToShowFullScreenContent 回调中调用。

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

GitHub 上的完整示例

插页式激励广告