開始使用

本指南適用於想在不使用 Firebase 的情況下,透過 AdMob 使用 C++ 應用程式營利的發布商。如果您打算在應用程式中加入 Firebase,或是考慮這麼做,請改為參閱本指南的「AdMob (搭配 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++ 測試應用程式

先備知識

Android

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

iOS

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

在 AdMob 帳戶中設定應用程式

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

  1. 登入註冊 AdMob 帳戶。

  2. 向 AdMob 註冊應用程式。這個步驟會使用專屬的 AdMob 應用程式 ID 建立 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 入門指南中找到 ndk-build 的操作說明,將 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 Mobile Ads 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、Google User Messaging Platform SDK 和最低 Firebase 核心 SDK (GMA C++ SDK 規定) 的 Pod:

      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

按照 Mobile Ads SDK Android 指南的說明,按照「設定應用程式」的步驟 3 操作,然後返回這個頁面。

iOS

按照 Mobile Ads SDK iOS 指南的說明更新 Info.plist 步驟,再返回本頁。

初始化 Google Mobile Ads SDK

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

呼叫 Initialize() 時,Google Mobile Ads C++ SDK 或中介服務合作夥伴 SDK 可預先載入廣告。如果您需要取得歐洲經濟區 (EEA) 使用者的同意聲明、設定任何要求專屬標記 (例如 tag_for_child_directed_treatmenttag_for_under_age_of_consent),或是需要在載入廣告前採取行動,請務必先叫用 firebase::gma::SetRequestConfiguration(),再初始化 Google Mobile Ads 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 提供多種廣告格式,您可以選擇最適合應用程式使用者體驗的廣告格式。

顯示在裝置螢幕頂端或底部的矩形廣告。橫幅廣告會在使用者與應用程式互動時持續顯示在畫面上,並在一段時間後自動重新整理。如果你是行動廣告新手,建議從這裡著手。

導入橫幅廣告

插頁式

全螢幕廣告會覆蓋應用程式介面,直到使用者關閉為止。 最適合用於應用程式執行流程中的自然暫停狀態,例如遊戲各關卡之間,或任務完成後才使用。

導入插頁式廣告

已獲得獎勵

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

導入獎勵廣告