本指南的適用對象為想透過 AdMob 利用 C++ 應用程式營利,但未使用 Firebase 的發布商。如果您打算在應用程式中加入 Firebase 或考慮使用 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 以上版本
- 請確認應用程式的版本檔案使用下列的值:
- 16 以上的
minSdkVersion
- 28 以上的
compileSdkVersion
- 16 以上的
iOS
- 使用 Xcode 13 以上版本
- 指定 iOS 10.0 以上版本
在 AdMob 帳戶中設定應用程式
完成下列步驟,即可將您的應用程式註冊為 AdMob 應用程式:
向 AdMob 註冊應用程式。這個步驟會建立具備專屬 AdMob 應用程式 ID 的 AdMob 應用程式,而本指南稍後會需要用到。
安裝 Google Mobile Ads C++ SDK
由於 Google Mobile Ads C++ SDK 位於 firebase::gma
命名空間,因此請下載 Firebase C++ SDK,然後將其解壓縮到您選擇的目錄中。
Firebase C++ SDK 並非特定平台,但需要特定平台的程式庫設定。
Android
在專案的
gradle.properties
檔案中,指定未壓縮 SDK 的位置:systemProp.firebase_cpp_sdk.dir=full-path-to-SDK
在專案的
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"
在您的模組 (應用程式層級) 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 }
在專案的
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}")
請同步處理應用程式,確保所有依附元件皆為必要的版本。
這樣就完成了!您的 C++ 應用程式設定為在沒有任何其他 Firebase 服務的情況下,使用 Google Mobile Ads C++ SDK。
iOS
本節中的步驟範例說明瞭如何將 Google Mobile Ads C++ SDK 新增至您的 iOS 專案。
執行下列指令,取得 CocoaPods 1 以上版本:
sudo gem install cocoapods --pre
從已解壓縮的 SDK 中加入 Google 行動廣告廣告連播。
如果你沒有 Podfile,請先建立 Podfile:
cd your-app-directory
pod init
在 Podfile 中加入 Google Mobile Ads C++ SDK 的 pod:
pod 'Google-Mobile-Ads-SDK'
安裝 Pod,然後在 Xcode 中開啟
.xcworkspace
檔案。pod install
open your-app.xcworkspace
將下列架構從 Firebase C++ SDK 新增至專案:
xcframeworks/firebase.xcframework
xcframeworks/firebase_gma.xcframework
這樣就完成了!您的 C++ 應用程式設定為在沒有任何其他 Firebase 服務的情況下,使用 Google Mobile Ads C++ SDK。
設定應用程式的 AdMob 應用程式 ID
Android
按照 Mobile Ads SDK Android 指南中的說明設定您的應用程式步驟 3,然後返回這個 C++ 入門指南網頁。
iOS
按照 Mobile Ads SDK iOS 指南中的指示,更新您的 Update Your Info.plist 步驟,然後返回這個 C++ 入門指南網頁。
初始化 Google Mobile Ads SDK
載入廣告之前,請呼叫 firebase::gma::Initialize()
來初始化 SDK,並在初始化完成後 (或 30 秒逾時後) 完成 firebase::Future
,藉此初始化 Google Mobile Ads C++ SDK。這項操作只需執行一次,最好在應用程式啟動時執行。
廣告可能在呼叫 Initialize()
時由 Google Mobile Ads C++ SDK 或中介服務合作夥伴 SDK 預先載入。如果您需要取得歐洲經濟區 (EEA) 使用者的同意聲明,請設定任何請求專用的標記 (例如 tag_for_child_directed_treatment
或 tag_for_under_age_of_consent
),或是在載入廣告之前採取動作,請務必在叫用 Google Mobile Ads C++ SDK 之前叫用 firebase::gma::SetRequestConfiguration()
。詳情請參閱指定目標指南。
以下提供如何呼叫 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
。接著,您的應用程式可以輪詢 Future
的 status()
,以確定初始化完成的時間。完成後,應用程式可叫用 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 提供多種廣告格式,您可以選擇最適合您應用程式的使用者體驗。
橫幅
顯示在裝置螢幕頂端或底部的矩形廣告。 橫幅廣告會在使用者與應用程式互動時顯示,而且可在一段時間後自動重新整理。如果是行動廣告新手,建議從這裡著手。
插頁式
這類全螢幕廣告會覆蓋應用程式介面,直到使用者關閉為止。 在應用程式執行流程中的自然暫停階段 (例如遊戲關卡之間或工作完成後),它們都最適合使用。
已獲得獎勵
獎勵使用者觀看短片,以及與可試玩廣告及問卷調查互動的廣告。用於透過免費遊戲應用程式營利。