開始使用

本指南適用於希望透過 AdMob 且未使用 Firebase 的 C++ 應用程式營利的發布商。如果您打算在應用程式中納入 Firebase (或目前正在考慮使用 Firebase),請改為參閱本指南的「AdMob 和 Firebase」版本。

將 Google Mobile Ads C++ SDK 整合到應用程式,是顯示廣告及賺取收益的第一步。整合 SDK 後,您可以選擇廣告格式 (例如插頁式廣告或獎勵),然後按照步驟導入。

Google Mobile Ads C++ SDK 會包裝 Google 行動廣告 iOS 和 Android SDK,且僅適用於這些平台。Google Mobile Ads C++ SDK 使用 Firebase C++ 結構支援非同步作業,因此位於 firebase::gma 命名空間中。

如果這是您第一次閱讀本指南,建議您下載並使用 Google 行動廣告 C++ 測試應用程式,並按照指示操作。

必要條件

Android

  • 使用 Android Studio 3.2 以上版本
  • 請確認應用程式的版本檔案使用下列的值:
    • 16 以上的 minSdkVersion
    • 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

  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. 請同步處理應用程式,確保所有依附元件皆為必要的版本。

這樣就完成了!您的 C++ 應用程式已設為使用 Google Mobile Ads C++ SDK,而不需要任何其他 Firebase 服務。

iOS

本節的步驟就是將 Google Mobile Ads C++ SDK 加進 iOS 專案的範例。

  1. 執行下列指令,取得 CocoaPods 1 以上版本:

    sudo gem install cocoapods --pre
    
  2. 從已解壓縮的 SDK 新增 Google 行動廣告廣告連播。

    1. 如果你沒有 Podfile,請先建立 Podfile:

      cd your-app-directory
      pod init
      
    2. 在 Podfile 中,新增 Google Mobile Ads C++ SDK 的廣告連播:

        pod 'Google-Mobile-Ads-SDK'
      
    3. 安裝 Pod,然後在 Xcode 中開啟 .xcworkspace 檔案。

      pod install
      open your-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,然後返回這個 C++ 開始使用頁面。

iOS

按照 Mobile Ads SDK iOS 指南中的「更新 Info.plist」步驟,然後返回這個 C++ 入門指南頁面。

初始化 Google Mobile Ads SDK

載入應用程式前,請呼叫 firebase::gma::Initialize()初始化 SDK,並完成 firebase::Future 初始化 (或 30 秒之後) 完成,藉此初始化 Google Mobile Ads C++ SDK。這個步驟只需進行一次,最好是應用程式啟動時。

Google Mobile Ads C++ SDK 或中介服務合作夥伴 SDK 呼叫 Initialize() 時,可能會預先載入廣告。如果您需要取得歐洲經濟區 (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 提供多種廣告格式,可讓您選擇最符合應用程式使用者體驗的格式。

顯示在裝置螢幕頂端或底部的矩形廣告。 在使用者與應用程式互動時,橫幅廣告會停留在畫面上,並在特定時間後自動重新整理。如果你是第一次放送行動廣告,建議先從這些地方著手。

導入橫幅廣告

插頁式

全螢幕廣告,蓋住應用程式介面,直到使用者關閉為止。 這類函式最適合在應用程式執行作業的自然暫停時使用,例如在遊戲的關卡之間或工作完成後。

導入插頁式廣告

已獲得獎勵

吸引使用者觀看短片、與可試玩廣告及問卷調查互動的廣告。用於透過免費遊戲應用程式營利。

導入獎勵廣告