本指南適用對象:有意運用 AdMob 在 C++ 應用程式中營利,但未使用 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 以上版本
- 請確認應用程式的版本檔案使用下列值:
minSdkVersion16 以上版本compileSdkVersion28 以上版本
iOS
- 使用 Xcode 13 以上版本
- 指定 iOS 10.0 以上版本
在 AdMob 帳戶中設定應用程式
請完成下列步驟,將應用程式註冊為 AdMob 營利應用程式:
向 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 入門指南,瞭解如何使用 ndk-build 將 libfirebase_app.a 和 libfirebase_gma.a 連結至應用程式。
在專案的
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}")同步處理應用程式,確保所有依附元件皆為必要版本。
iOS
本節步驟示範如何將 Google Mobile Ads C++ SDK 加入 iOS 專案。
執行下列指令,取得 CocoaPods 1 以上版本:
sudo gem install cocoapods --pre從解壓縮的 SDK 新增 Google Mobile Ads Pod。
如果還沒有 Podfile,請建立一個:
cd APP_DIRECTORYpod init在 Podfile 中,新增 Google Mobile Ads C++ SDK、Google User Messaging Platform SDK 和基本 Firebase Core SDK (GMA C++ SDK 必備) 的 Pod:
pod 'Firebase/CoreOnly' pod 'Google-Mobile-Ads-SDK' pod 'GoogleUserMessagingPlatform'安裝 Pod,然後在 Xcode 中開啟
.xcworkspace檔案。pod installopen APP.xcworkspace將 Firebase C++ SDK 的下列框架新增至專案:
xcframeworks/firebase.xcframeworkxcframeworks/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) 使用者取得同意聲明,請務必在初始化 Google Mobile Ads C++ SDK 前,叫用 firebase::gma::SetRequestConfiguration() 設定要求專屬的標記 (如 tag_for_child_directed_treatment 或 tag_for_under_age_of_consent),或在載入廣告前採取行動。詳情請參閱指定目標指南。
以下範例說明如何呼叫 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 提供多種廣告格式,您可以根據應用程式的使用者體驗,選擇最適合的格式。
橫幅廣告
顯示在裝置畫面頂端或底部的矩形廣告。使用者操作應用程式時,橫幅廣告會持續停留在畫面上,並能定時自動更新內容。對剛接觸行動廣告的新手來說,這是很好的入門選擇。
插頁式廣告
完全覆蓋應用程式介面的全螢幕廣告,使用者需自行關閉。這類廣告最適合出現在應用程式執行流程中的自然停頓點,例如遊戲關卡之間或完成任務後。
已獲得獎勵
這類廣告會在使用者看完短片、與可試玩廣告互動,或完成問卷調查後提供獎勵,適合用來透過免付費玩遊戲的應用程式營利。