本指南適用於想透過 AdMob 營利,但不想使用 Firebase 的 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 行動廣告 C++ 測試應用程式,並按照指南操作。
必要條件
Android
- 使用 Android Studio 3.2 以上版本
- 請確認應用程式的版本檔案為下列值:
- 16 以上版本
minSdkVersion
compileSdkVersion
28 以上版本
- 16 以上版本
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,但如要瞭解 ndk-build 的操作說明,請參閱一般 Firebase C++ SDK 入門指南,瞭解如何將 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,請先建立 Podfile:
cd APP_DIRECTORY
pod 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 install
open APP.xcworkspace
將 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 並完成 firebase::Future
,該物件將在初始化完成或 30 秒逾時後執行完畢。這項操作只需執行一次,最佳時機是應用程式啟動時。
呼叫 Initialize()
時,Google Mobile Ads C++ SDK 或中介服務合作夥伴 SDK 可能會預先載入廣告。如需向歐洲經濟區的使用者取得同意聲明,請設定適用的請求標記 (例如 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 提供多種廣告格式,您可以根據應用程式的使用者體驗,選擇最適合的格式。
橫幅廣告
顯示在裝置畫面頂端或底部的矩形廣告。 使用者操作應用程式時,橫幅廣告會持續停留在畫面上,並能定時自動更新內容。如果您剛接觸行動廣告,這是很好的入門選擇。
插頁式
完全覆蓋應用程式介面的全螢幕廣告,使用者需自行關閉。這類廣告最適合出現在應用程式執行流程中的自然停頓點,例如遊戲關卡之間或完成任務後。
已獲得獎勵
這類廣告能在使用者看完短片、與可試玩廣告互動,或完成問卷調查後提供獎勵;用於透過免費遊戲應用程式營利。