開始使用


本指南適用於想要使用 AdMob 的 C++ 應用程式營利的發布商 或 Firebase 應用程式如果您打算在應用程式中納入 Firebase 請參考這篇文章 Firebase 版本 指南。

將 Google Mobile Ads C++ SDK 整合至應用程式,是達成以下目標的第一步: 來顯示廣告及賺取收益整合 SDK 後 選擇廣告格式 (例如插頁式廣告或獎勵廣告),然後按照步驟 實際執行這些工作

Google Mobile Ads C++ SDK 納入了 Google Mobile Ads iOS 和 Android SDK, 而且只能在這些平台上使用Google Mobile Ads C++ SDK 使用 Firebase C++ 建構支援非同步作業,因此位於 在 firebase::gma 命名空間中。

如果您是第一次瀏覽本指南,建議 下載,然後利用 Google Mobile Ads C++ 測試 app

必要條件

Android

  • 使用 Android Studio 3.2 以上版本
  • 請確認應用程式的版本檔案使用下列值:
    • minSdkVersion 16 以上
    • compileSdkVersion 28 以上

iOS

  • 使用 Xcode 13 以上版本
  • 指定 iOS 10.0 以上版本

在 AdMob 帳戶中設定應用程式

完成下列步驟,將應用程式註冊為 AdMob 應用程式:

  1. 登入申請 AdMob 帳戶。

  2. 向 AdMob:這個步驟會建立 AdMob 含有專屬 AdMob 應用程式 ID 的應用程式 以便進行說明

安裝 Google Mobile Ads C++ SDK

由於 Google Mobile Ads 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:新增以下內容,包括 Google Mobile Ads C++ SDK 的程式庫依附元件。

    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 Mobile Ads 廣告連播。

    1. 如果您尚未建立 Podfile,請建立一個:

      cd APP_DIRECTORY
      pod init
      
    2. 請在 Podfile 中新增 Google Mobile Ads 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 Mobile Ads C++ SDK 不必使用任何其他 Firebase 服務

設定應用程式的 AdMob 應用程式 ID

Android

按照設定應用程式步驟 3 的指示,按照 Mobile Ads SDK 的說明 Android 指南 再回到這個頁面

iOS

按照 Mobile Ads SDK 的說明進行更新 Info.plist 步驟 再回來查看

初始化 Google Mobile Ads SDK

載入廣告前,請先設定應用程式初始化 Google Mobile Ads C++ SDK,方法是: 呼叫 firebase::gma::Initialize(),以便初始化 SDK 並完成 完成初始化 (或 30 秒後則為 firebase::Future) 逾時)。這項操作只需進行一次,最好在應用程式啟動時。

Google Mobile Ads C++ SDK 或中介服務合作夥伴 SDK 可能會預先載入廣告 呼叫 Initialize() 時。如需向 歐洲經濟區 (EEA),請設定所有請求的標記 (例如 tag_for_child_directed_treatmenttag_for_under_age_of_consent),或 否則請在載入廣告前進行 初始化 Google 行動服務前 firebase::gma::SetRequestConfiguration() 廣告 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 Mobile Ads C++ SDK 現已匯入完成,您隨時可以導入 廣告。AdMob 提供多種廣告格式,您可以選用 選出最適合您應用程式的使用者體驗

顯示在裝置螢幕頂端或底部的矩形廣告。 橫幅廣告會在使用者與應用程式互動時持續顯示在畫面上, 並重新整理。如果你剛開始使用行動裝置 都是很好的起點

導入橫幅廣告

插頁式

全螢幕廣告會覆蓋應用程式介面,直到使用者關閉為止。 最適合用於應用程式執行流程中的自然暫停點,例如 或是在任務結束後才剛結束

導入插頁式廣告

已獲得獎勵

在使用者觀看短片及與可試玩廣告互動時提供獎勵的廣告 廣告和問卷調查用於透過免費遊戲應用程式營利。

導入獎勵廣告