插页式广告


插页式广告是全屏广告,它会覆盖整个应用界面, 被用户关闭这些广告通常会在应用流程的自然过渡点(例如,活动之间或游戏关卡之间的暂停时段)展示。当应用展示插页式广告时,用户可以自行选择 用户点按广告并继续访问目标网址,或关闭广告并返回 。 案例研究

本指南介绍了如何使用 Google Mobile Ads C++ SDK 将插页式广告集成到 Android 和 iOS 应用中。

前提条件

始终使用测试广告进行测试

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

对于插页式广告,加载测试广告最简便的方法就是使用我们的专用测试广告单元 ID,该 ID 因设备平台而异:

  • Android:ca-app-pub-3940256099942544/1033173712
  • iOS:ca-app-pub-3940256099942544/4411468910

这些测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告; 在编码、测试和调试时,您可以随意在自己的应用中使用它。 只需确保您会在发布应用前用自己的广告单元 ID 替换该测试广告单元 ID 即可。

如需详细了解移动广告 SDK 的测试广告如何运作,请参阅 测试广告

实现

植入插页式广告的主要步骤如下所示:

  1. 加载广告。
  2. 注册回调。
  3. 展示广告并处理其生命周期事件。

配置 InterstitialAd

插页式广告显示在 InterstitialAd 对象中,因此第一步 在应用中集成插页式广告的其中一个步骤是,创建并初始化 InterstitialAd 对象。

  1. 将以下头文件添加到应用的 C++ 代码中:

     #include "firebase/gma/interstial_ad.h"
    

  2. 声明并实例化一个 InterstitialAd 对象:

     firebase::gma::InterstitialAd* interstitial_ad;
     interstitial_ad = new firebase::gma::InterstitialAd();
    

  3. 使用转换为InterstitialAd AdParent 类型。父视图是对 Android 的 JNI jobject 引用 Activity 或指向 iOS UIView 的指针。

    // my_ad_parent is a jobject reference to an Android Activity or
    // a pointer to an iOS UIView.
    firebase::gma::AdParent ad_parent =
      static_cast<firebase::gma::AdParent>(my_ad_parent);
    firebase::Future<void> result = interstitial_ad->Initialize(ad_parent);
    
  4. 作为将 Future 保留为变量的替代方法,您可以定期 通过调用以下命令来检查初始化操作的状态: 针对 InterstitialAd 对象的 InitializeLastResult() 权限。这可能有助于跟踪全局游戏循环中的初始化过程。

    // Monitor the status of the future in your game loop:
    firebase::Future<void> result = interstitial_ad->InitializeLastResult();
    if (result.status() == firebase::kFutureStatusComplete) {
      // Initialization completed.
      if(future.error() == firebase::gma::kAdErrorCodeNone) {
        // Initialization successful.
      } else {
        // An error has occurred.
      }
    } else {
      // Initialization on-going.
    }
    

如需详细了解如何使用 firebase::Future,请参阅使用 Future 来监控方法调用的完成状态

加载广告

广告的加载是通过针对 InterstitialAd 对象使用 LoadAd() 方法完成的。加载方法要求您先初始化 InterstitialAd 对象,并且拥有广告单元 ID 和 AdRequest 对象。系统会返回 firebase::Future,您可以使用它来监控状态 加载操作的结果和结果。

以下代码展示了如何在 InterstitialAd 完成后加载广告 已成功初始化:

firebase::gma::AdRequest ad_request;
firebase::Future<firebase::gma::AdResult> load_ad_result;
load_ad_result = interstitial_ad->LoadAd(interstitial_ad_unit_id, ad_request);

注册回调

您必须扩展 FullScreenContentListener 类才能接收 有关插页式广告展示和生命周期事件的通知。您的自定义 FullScreenContentListener 子类可通过 InterstitialAd::SetFullScreenContentListener() 方法,它会接收 以及当广告成功展示或展示失败时 它已关闭

以下代码展示了如何扩展该类并将其分配给广告:

  class ExampleFullScreenContentListener
      : public firebase::gma::FullScreenContentListener {

   public:
    ExampleFullScreenContentListener() {}

    void OnAdClicked() override {
      // This method is invoked when the user clicks the ad.
    }

    void OnAdDismissedFullScreenContent() override {
     // This method is invoked when the ad dismisses full screen content.
    }

    void OnAdFailedToShowFullScreenContent(const AdError& error) override {
      // This method is invoked when the ad failed to show full screen content.
      // Details about the error are contained within the AdError parameter.
    }

    void OnAdImpression() override {
      // This method is invoked when an impression is recorded for an ad.
    }

    void OnAdShowedFullScreenContent() override {
      // This method is invoked when the ad showed its full screen content.
    }
  };

  ExampleFullScreenContentListener* full_screen_content_listener =
    new ExampleFullScreenContentListener();
  interstitial_ad->SetFullScreenContentListener(full_screen_content_listener);

InterstitialAd 是一次性对象。这意味着,插页式广告展示一次后就不能再展示了。最佳做法是在 FullScreenContentListenerOnAdDismissedFullScreenContent() 方法中加载另一个插页式广告,以便在上一个插页式广告关闭后,立即开始加载下一个插页式广告。

展示广告

插页式广告应在应用流程的自然停顿期间进行展示,例如游戏的不同关卡之间或用户完成一项任务之后,都是非常不错的展示时机。虽然 FullScreenContentListener 可用于确定广告何时展示其全屏内容,但 Show() 返回的 Future 也会在广告成功展示时发出信号。

  firebase::Future<void> result = interstitial_ad->Show();

最佳做法

考虑插页式广告这种广告类型是否适合您的应用。
在具有自然过渡点的应用中,插页式广告的效果最好。通过 结束应用内的任务,例如分享图片或完成游戏 会形成这样一个点因为用户希望在 用户可以轻松展示插页式广告 体验。请务必考虑在应用工作流的哪些时间点 展示插页式广告以及用户可能会如何响应。
务必在展示插页式广告时暂停操作。
插页式广告有多种类型:文字广告、图片广告 视频等确保应用在展示插页式广告时,也会暂停使用某些资源,以便供广告使用,这一点十分重要。例如,当您调用 插页式广告,请务必暂停应用产生的所有音频输出。 您可以使用 OnAdDismissedFullScreenContent 方法继续播放声音: 用户安装的 FullScreenContentListener 时,系统会调用 已完成与广告的互动。此外,可以考虑暂时停止 所有密集计算任务(例如游戏循环) 。这样可以确保用户不会遇到图像无响应、响应慢或视频卡顿的现象。
留出充足的加载时间。
确保在恰当的时间展示插页式广告十分重要,同样,确保用户无需等待广告加载也十分重要。在您打算展示广告之前,提前加载广告可确保 在您的应用内 会来看看这个界面
不要向用户展示太多广告。
虽然提高插页式广告在应用中的展示频次似乎 例如增加收入的好方法,但这也会影响用户体验 和较低的点击率。确保用户不会过于频繁地 打断了他们,导致他们无法继续使用您的应用。
请勿使用“待加载完毕”之后展示插页式广告。
这可能会导致用户体验不佳。应预先加载广告,以备需要展示时使用。

其他资源

GitHub 中的示例

“Mobile Ads Garage”视频教程

成功案例

后续步骤