开始使用

<ph type="x-smartling-placeholder">


本指南适用于希望在不采用 AdMob 的情况下通过 C++ 应用创收的发布商 使用 Firebase如果您打算在应用中添加 Firebase,或者 请参阅AdMob 与 Firebase版本 指南。

要将 Google 移动广告 C++ SDK 集成到应用中, 展示广告并赚取收入集成 SDK 后,您可以 选择一种广告格式(如插页式广告或激励广告), 实施。

Google 移动广告 C++ SDK 封装了 Google 移动广告 iOS 版和 Android 版 SDK, 并且仅适用于上述平台Google 移动广告 C++ SDK 使 使用 Firebase C++ 结构来支持异步操作, 位于 firebase::gma 命名空间中。

如果这是您第一次使用本指南,建议您 下载 Google 移动广告 C++ 测试并完成 app

前提条件

Android

  • 使用 Android Studio 3.2 或更高版本
  • 确保应用的 build 文件使用以下值:
    • minSdkVersion 为 16 或更高版本
    • compileSdkVersion 为 28 或更高版本

iOS

  • 使用 Xcode 13 或更高版本
  • 定位到 iOS 10.0 或更高版本

在您的 AdMob 账号中设置应用

完成以下步骤,将您的应用注册为 AdMob 应用:

  1. 登录注册 AdMob 账号。

  2. 通过以下平台注册您的应用: AdMob。这一步将创建 AdMob 具有唯一 AdMob 应用 ID 的应用 本指南后面部分会用到它。

安装 Google 移动广告 C++ SDK

由于 Google 移动广告 C++ SDK 位于 firebase::gma 命名空间中, 下载 Firebase C++ SDK, 然后将其解压缩到您选择的目录中。

Firebase C++ SDK 并不局限于特定平台,但需要 针对具体平台的库配置。

Android

我们建议使用 CMake,但您可以在我们的 常规 Firebase C++ SDK 使用入门 libfirebase_app.alibfirebase_gma.a 关联到您的应用。

  1. 在项目的 gradle.properties 文件中,指定 解压缩后的 SDK:

    systemProp.firebase_cpp_sdk.dir=FULL_PATH_TO_SDK
    
  2. 在项目的 settings.gradle 文件中,添加以下内容:

    def firebase_cpp_sdk_dir = System.getProperty('firebase_cpp_sdk.dir')
    
    gradle.ext.firebase_cpp_sdk_dir = "$firebase_cpp_sdk_dir"
    includeBuild "$firebase_cpp_sdk_dir"
    
  3. 在您的模块(应用级)Gradle 文件中,通常 app/build.gradle - 添加以下内容,其中包括 库依赖项。

    android.defaultConfig.externalNativeBuild.cmake {
      arguments "-DFIREBASE_CPP_SDK_DIR=$gradle.firebase_cpp_sdk_dir"
    }
    
    # Add the dependency for the Google Mobile Ads C++ SDK
    apply from: "$gradle.firebase_cpp_sdk_dir/Android/firebase_dependencies.gradle"
    firebaseCpp.dependencies {
      gma
    }
    
  4. 在项目的 CMakeLists.txt 文件中,添加以下内容。

    # Add Firebase libraries to the target using the function from the SDK.
    add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
    
    # Add the Google Mobile Ads C++ SDK.
    
    # The Firebase C++ library `firebase_app` is required,
    # and it must always be listed last.
    
    set(firebase_libs
      firebase_gma
      firebase_app
    )
    
    target_link_libraries(${target_name} "${firebase_libs}")
    
  5. 同步您的应用,以确保所有依赖项都具有必要的版本。

iOS

本部分中的步骤说明了如何将 Google 移动广告 C++ SDK 添加到您的 iOS 项目中。

  1. 运行以下命令来获取 CocoaPods 1 或更高版本:

    sudo gem install cocoapods --pre
    
  2. 从解压缩的 SDK 添加 Google 移动广告广告连播。

    1. 如果您没有 Podfile,请创建一个:

      cd APP_DIRECTORY
      pod init
      
    2. 在 Podfile 中,将 Google 移动广告 C++ SDK 的 Pod(即 Google User Messaging Platform SDK 和最低 Firebase 核心 SDK (GMA C++ SDK 需要):

      pod 'Firebase/CoreOnly'
      pod 'Google-Mobile-Ads-SDK'
      pod 'GoogleUserMessagingPlatform'
      
    3. 安装 Pod,然后在 Xcode 中打开 .xcworkspace 文件。

      pod install
      open APP.xcworkspace
      
    4. 将 Firebase C++ SDK 中的以下框架添加到项目中:

      • xcframeworks/firebase.xcframework
      • xcframeworks/firebase_gma.xcframework

您已准备就绪!您的 C++ 应用已配置为使用 Google 移动广告 C++ SDK 而无需任何其他 Firebase 服务。

配置应用的 AdMob 应用 ID

Android

按照移动广告 SDK 的说明配置应用中的第 3 步操作 Android 指南 然后再返回到此页面

iOS

按照移动广告 SDK 中的说明,执行更新您的 Info.plist 操作。 iOS 指南,然后再返回 此页面。

初始化 Google 移动广告 SDK

加载广告之前,请先让应用初始化 Google 移动广告 C++ SDK,方法是 调用 firebase::gma::Initialize(),它会初始化 SDK 并完成 在初始化完成后(或初始化 30 秒后)返回 firebase::Future 超时)。此操作仅需执行一次,最好是在应用启动时执行。

广告可能会由 Google 移动广告 C++ SDK 或中介合作伙伴 SDK 预加载 调用 Initialize() 时返回的值。如果您需要获得 欧洲经济区 (EEA),请设置任何特定于请求的标志(例如 tag_for_child_directed_treatmenttag_for_under_age_of_consent),或者 否则,请在加载广告前采取措施,请确保通过调用 firebase::gma::SetRequestConfiguration(),然后再初始化 Google 移动 广告 C++ SDK。如需更多信息,请参阅我们的 定位指南。

以下示例展示了如何调用 Initialize()

Android

// Initialize the Google Mobile Ads library
firebase::InitResult result;
Future<AdapterInitializationStatus> future =
  firebase::gma::Initialize(jni_env, j_activity, &result);

if (result != kInitResultSuccess) {
  // Initialization immediately failed, most likely due to a missing
  // dependency. Check the device logs for more information.
  return;
}

// Monitor the status of the future.
// See "Use a Future to monitor the completion status of a method call" below.
if (future.status() == firebase::kFutureStatusComplete &&
    future.error() == firebase::gma::kAdErrorCodeNone) {
  // Initialization completed.
} else {
  // Initialization on-going, or an error has occurred.
}

iOS

// Initialize the Google Mobile Ads library.
firebase::InitResult result;
Future<AdapterInitializationStatus> future =
  firebase::gma::Initialize(&result);

if (result != kInitResultSuccess) {
  // Initialization immediately failed, most likely due to a missing
  // dependency. Check the device logs for more information.
  return;
}

// Monitor the status of the future.
// See "Use a Future to monitor the completion status of a method call" below.
if (future.status() == firebase::kFutureStatusComplete &&
    future.error() == firebase::gma::kAdErrorCodeNone) {
  // Initialization completed.
} else {
  // Initialization on-going, or an error has occurred.
}

使用 Future 监控方法调用的完成状态

Future提供了一种方法来确定 异步方法调用。

例如,当您的应用调用 firebase::gma::Initialize() 时, 创建并返回 firebase::Future。然后,您的应用就可以轮询 Futurestatus(),用于确定初始化何时完成。 完成后,您的应用可以调用 result() 来获取生成的结果 AdapterInitializationStatus

返回 Future 的方法有对应的“上次结果”方法, 应用可用于检索针对指定操作的最新 Future。对于 firebase::gma::Initialize() 有一个名为 firebase::gma::InitializeLastResult(),该方法会返回您的应用的 Future 可用于检查上次调用 firebase::gma::Initialize() 的状态。

如果 Future 的状态为已完成,并且其错误代码为 firebase::gma::kAdErrorCodeNone,则操作已完成 成功。

您还可以注册在 Future 完成时要调用的回调。在 在某些情况下,回调将在不同的线程中运行,因此,请确保 代码是线程安全的此代码段使用一个函数指针作为 回调:

// Registers the OnCompletion callback. user_data is a pointer that is passed verbatim
// to the callback as a void*. This allows you to pass any custom data to the callback
// handler. In this case, the app has no data, so you must pass nullptr.
firebase::gma::InitializeLastResult().OnCompletion(OnCompletionCallback,
  /*user_data=*/nullptr);

// The OnCompletion callback function.
static void OnCompletionCallback(
  const firebase::Future<AdapterInitializationStatus>& future, void* user_data) {
  // Called when the Future is completed for the last call to firebase::gma::Initialize().
  // If the error code is firebase::gma::kAdErrorCodeNone,
  // then the SDK has been successfully initialized.
  if (future.error() == firebase::gma::kAdErrorCodeNone) {
    // success!
  } else {
    // failure.
  }
}

选择广告格式

Google 移动广告 C++ SDK 现已导入,您可以随时实现 。AdMob 提供了多种不同的广告格式,您可以从中选择 最适合您应用的用户体验

横幅广告是在设备屏幕的顶部或底部展示的矩形广告。 用户与应用互动时,横幅广告会停留在屏幕上 一段时间后自动刷新如果您是刚开始使用移动设备 都是很好的切入点

植入横幅广告

插页式广告

全屏广告,它会覆盖整个应用界面,直到用户将其关闭。 这类广告最适合在应用执行流程的自然停顿点, 例如游戏的不同关卡之间,或刚完成一项任务之后。

植入插页式广告

激励广告

奖励观看短视频以及与试玩视频互动的用户的广告 。用于通过免费游戏应用变现。

植入激励广告