插页式广告是全屏广告,它会覆盖整个应用界面, 被用户关闭它们通常在 例如 activity 之间或应用之间的暂停期间 游戏关卡当应用展示插页式广告时,用户可以自行选择 用户点按广告并继续访问目标网址,或关闭广告并返回 。 案例研究。
本指南介绍了如何将插页式广告植入到 Android 和 iOS 应用中 使用 Google 移动广告 C++ SDK
前提条件
- 完成入门指南。
- (仅限 Android)熟悉 JNI
jobject
引用(请参阅 Android JNI 提示)。
始终使用测试广告进行测试
在构建和测试应用时,请务必使用测试广告, 实际投放的广告。否则,可能会导致您的账号被暂停。
要加载测试广告,最简便的方法就是使用我们的专用测试广告单元 ID 插页式广告(因设备平台而异):
- Android:
ca-app-pub-3940256099942544/1033173712
- iOS:
ca-app-pub-3940256099942544/4411468910
这些测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告; 在编码、测试和调试时,您可以随意在自己的应用中使用它。 只需确保在发布 应用。
如需详细了解移动广告 SDK 的测试广告如何运作,请参阅 测试广告。
实现
植入插页式广告的主要步骤如下:
- 加载广告。
- 注册回调。
- 展示广告并处理其生命周期事件。
配置 InterstitialAd
插页式广告显示在 InterstitialAd
对象中,因此第一步
在应用中集成插页式广告的其中一个步骤是,创建并初始化
InterstitialAd
对象。
将以下头文件添加到应用的 C++ 代码中:
#include "firebase/gma/interstial_ad.h"
声明并实例化一个
InterstitialAd
对象:firebase::gma::InterstitialAd* interstitial_ad; interstitial_ad = new firebase::gma::InterstitialAd();
使用转换为
InterstitialAd
AdParent
类型。父视图是对 Android 的 JNIjobject
引用Activity
或指向 iOSUIView
的指针。// 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);
作为将 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 监控方法的完成状态
调用。
加载广告
广告加载是使用 LoadAd()
方法在
InterstitialAd
对象。加载方法要求您先初始化
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
是一次性对象。这意味着
该广告已展示,它不能再次展示。最佳做法是
OnAdDismissedFullScreenContent()
方法加载插页式广告
FullScreenContentListener
,以便系统以
上一个事件。
展示广告
插页式广告应在应用流程的自然停顿期间
(以游戏的不同关卡之间或用户完成一项任务后为例)。
虽然 FullScreenContentListener
可用于确定广告
显示其全屏内容,则 Show()
返回的 Future 还会发出信号
当广告成功展示时触发。
firebase::Future<void> result = interstitial_ad->Show();
最佳做法
- 考虑插页式广告是否适合您的应用。
- 在具有自然过渡点的应用中,插页式广告的效果最好。通过 结束应用内的任务,例如分享图片或完成游戏 会形成这样一个点因为用户希望在 用户可以轻松展示插页式广告, 体验。请务必考虑在应用工作流的哪些时间点 展示插页式广告以及用户可能会如何响应。
- 务必在展示插页式广告时暂停操作。
- 插页式广告有多种类型:文字广告、图片广告
视频等务必要确保您的应用在显示
但它也会暂停使用某些资源
请充分利用它们例如,当您调用
插页式广告时,请务必暂停应用产生的所有音频输出。
您可以使用
OnAdDismissedFullScreenContent
用户安装的FullScreenContentListener
时,系统会调用 已完成与广告的互动。此外,可以考虑暂时停止 所有密集计算任务(例如游戏循环) 。这样可以确保用户不会遇到速度过慢或 图片无响应或视频卡顿。 - 留出充足的加载时间。
- 正如确保在展示位置中展示插页式广告 合适的时间,同样重要的是确保用户无需等待 进行加载在您打算展示广告之前,提前加载广告可确保 在您的应用内 会来看看这个界面
- 不要向用户展示太多广告。
- 虽然提高插页式广告在应用中的展示频次似乎 例如增加收入的好方法,但这也会影响用户体验 和较低的点击率。确保用户不会过于频繁地 打断了他们,导致他们无法继续使用您的应用。
- 请勿使用“待加载完毕”之后展示插页式广告。
- 这可能会导致糟糕的用户体验。您应先预加载广告 使用起来非常简单
其他资源
GitHub 中的示例
- 查看示例的源代码 快速入门应用 。