Firebase C++ SDK v9.1.0 的發布引入了新的 Google 行動廣告 C++ SDK。
Google 行動廣告C++ SDK 是一個新的API 介面,融合了2021 年和2022 年對適用於iOS 和Android 的Firebase AdMob C++ SDK 所做的重大突破性更改,包括刪除已棄用的API,以及處理全螢幕廣告時的新流程類型。
舊的 Firebase AdMob C++ SDK ( firebase::admob
) 已被標記為已棄用,並且今後不會收到任何更新或錯誤修復。
在 Firebase AdMob C++ SDK 棄用期間,新版 Google 行動廣告 C++ SDK ( firebase::gma
) 和舊版 Firebase AdMob C++ SDK ( firebase::admob
) 都會保留在 Firebase C++ SDK 建置中。
舊版 API 刪除
以下 API 已從 Google 行動廣告 C++ SDK 中全部刪除。
RewardedVideoAd
AdMob 的RewardedVideoAd
命名空間已替換為RewardedAd
類別。 RewardedAd
行為與InterstitialAd
類似,但包含一個額外的RewardedAdListener
來接收項目獎勵的通知。
NativeExpressAds
AdMob 的NativeExpressAd
已在每個 Firebase AdMob C++ SDK 中標記為已棄用。因此, NativeExpressAd
未包含在新的 Google 行動廣告 C++ SDK 中。
SDK命名空間變更
SDK已遷移到新的命名空間,並且具有新的目錄結構:
命名空間firebase::gma
新的 Google 行動廣告 C++ SDK 的原始碼位於firebase::gma
命名空間。較舊的firebase::admob
命名空間已與 Firebase AdMob C++ SDK 一起被棄用。
目錄結構
頭檔已移至建置存檔內的新目錄:
已棄用的 Firebase AdMob C++ SDK | 新的 Google 行動廣告 C++ SDK |
---|---|
include/firebase/admob | include/firebase/gma |
圖書館
Firebase AdMob C++ SDK 將作為 Firebase C++ SDK 建置檔案中的靜態函式庫提供:
iOS系統
已棄用的 Firebase AdMob C++ SDK | 新的 Google 行動廣告 C++ SDK |
---|---|
firebase_admob.xcframework | firebase_gma.xcframework |
安卓
已棄用的 Firebase AdMob C++ SDK | 新的 Google 行動廣告 C++ SDK |
---|---|
libfirebase_admob.a | libfirebase_gma.a |
類別、枚舉和結構遷移
下表列出了已更改或已刪除的特定類別、枚舉和結構。總結如下:
-
BannerView
已重新命名為AdView
。 -
NativeAdExpressView
已刪除。 -
RewardedVideo
命名空間被RewardedAd
類別取代。 -
PresentationState
枚舉和偵聽器被刪除並替換為AdListener
和FullScreenContent
偵聽器。 以下參數作為
AdRequests
中的每個廣告配置參數被刪除:- 測試設備ID的配置
- 根據年齡定位廣告
相反,現在可以在
RequestConfiguration
中配置這些參數,這是一個全域設置,將影響所有後續廣告載入。
已棄用的firebase::admob namespace | 新的firebase::gma namespace |
---|---|
AdSizeType (枚舉) | AdSize::Type (枚舉) |
BannerView | AdView |
BannerView::Listener | AdListener AdViewBoundingBoxListener PaidEventListener |
BannerView::Position | AdView::Position |
BannerView::PresentationState | 已刪除 |
ChildDirectedTreatmentState | RequestConfiguration::TagForChildDirectedTreatment |
Gender (列舉) | 已刪除 |
InterstitialAd::Listener | FullScreenContentListener PaidEventListener |
KeyValuePair | 已刪除 |
NativeExpressAdView | 已刪除 |
PollableRewardListener | 已刪除 |
RewardItem | AdReward |
RewardedVideoAd (命名空間) | RewardedAd (類) |
RewardedVideoAd::Listener | FullScreenContentListener PaidEventListener UserEarnedRewardListener |
AdMobError (枚舉) | AdErrorCode (枚舉) |
RewardItem | AdReward |
SDK初始化
每個 Google 行動廣告 C++ SDK 初始化函數都會立即傳回兩個狀態指示燈:
可選的輸出參數表示在初始化過程開始之前是否發生依賴性錯誤。
傳回參數是對
firebase::Future
參考。Future
包含裝置上中介適配器非同步初始化的結果。
雖然一旦初始化函數返回,就可以呼叫 Google 行動廣告 C++ SDK 來載入 AdMob 提供的廣告,但其他廣告網路在其對應的中介適配器完全初始化之前不會提供廣告。此過程異步發生。因此,如果您在應用程式中使用廣告中介,我們建議您等待Future
解析後再嘗試載入任何廣告。
前
firebase::App* app = ::firebase::App::Create();
firebase::InitResult result = firebase::admob::Initialize(*app, kAdMobAppID);
if (result != kInitResultSuccess) {
// Initialization immediately failed, most likely due to a missing dependency.
// Check the device logs for more information.
return;
}
後
using firebase::App;
using firebase::Future;
using firebase::gma::AdapterInitializationStatus;
App* app = ::firebase::App::Create();
firebase::InitResult result;
Future<AdapterInitializationStatus> future =
firebase::gma::Initialize(*app, &result);
if (result != kInitResultSuccess) {
// Initialization immediately failed, most likely due to a missing dependency.
// Check the device logs for more information.
return;
}
// Poll the future to wait for its completion either in this
// thread, or as part of your game loop by calling
// firebase::gma::InitializeLastResult();
while (future.status() == firebase::kFutureStatusPending) {
// Initialization on-going, continue to wait.
}
// future.status() is either kFutureStatusComplete or there’s an error
if (future.status() == firebase::kFutureStatusComplete &&
future.error() == firebase::gma::AdErrorCodeNone) {
AdapterInitializationStatus* status = future.result();
// Check status for any mediation adapters you wish to use.
// ..
} else {
// Handle initialization error.
}
AdView
中AdSize
的更改
AdSize
現在包含常見橫幅廣告尺寸的靜態成員,並支援AnchorAdaptive
和InlineAdaptive
廣告尺寸,它們具有基於給定寬度和螢幕當前方向的動態高度。
加入firebase::gma::AdSize 靜態AdSize 常數 | |
---|---|
AdSize::kBanner | 行動行銷協會 (MMA) 橫幅廣告尺寸(320x50 密度無關像素) |
AdSize::kFullBanner | 互動式廣告局 (IAB) 全橫幅廣告尺寸(468x60 與密度無關的像素) |
AdSize::kLargeBanner | 更高版本的kBanner ,通常為 320x100 |
AdSize::kLeaderboard | 互動式廣告局 (IAB) 排行榜廣告尺寸(728x90 與密度無關的像素) |
AdSize::kMediumRectangle | 互動式廣告局 (IAB) 中矩形廣告尺寸(300x250 與密度無關的像素) |
firebase::gma::AdSize 中的靜態方法可協助建構AdSize 實例 | |
---|---|
GetLandscapeAnchoredAdaptiveBannerAdSize | 建立具有給定寬度和 Google 最佳化高度的AdSize ,以在橫向模式中建立橫幅廣告 |
GetPortraitAnchoredAdaptiveBannerAdSize | 建立具有給定寬度和 Google 最佳化高度的AdSize ,以在縱向模式中建立橫幅廣告 |
GetCurrentOrientationAnchoredAdaptiveBannerAdSize | 建立具有給定寬度和 Google 最佳化高度的AdSize ,以在給定當前方向的情況下建立橫幅廣告 |
GetInlineAdaptiveBannerAdSize | 在給定最大高度的情況下創建最適合橫幅廣告的AdSize 此 |
GetLandscapeInlineAdaptiveBannerAdSize | 建立具有給定寬度和裝置橫向高度的InlineAdaptive AdSize |
GetPortraitInlineAdaptiveBannerAdSize | 建立具有給定寬度和裝置縱向高度的InlineAdaptive AdSize 。 |
GetCurrentOrientationInlineAdaptiveBannerAdSize | 在給定當前介面方向和特定寬度的情況下返回InlineAdaptive AdSize 便捷方法。 |
前
firebase::admob::BannerView* banner_view = new firebase::admob::BannerView();
firebase::admob::AdSize ad_size;
ad_size.ad_size_type = firebase::admob::kAdSizeStandard;
ad_size.width = 320;
ad_size.height = 50;
// ad_parent is a reference to an iOS UIView or an Android Activity.
// banner_ad_unit is your ad unit id for banner ads.
banner_view->Initialize(ad_parent, banner_ad_unit, ad_size);
後
firebase::gma::AdView* ad_view = new firebase::gma::AdView();
// ad_parent is a reference to an iOS UIView or an Android Activity.
// banner_ad_unit is your ad unit id for banner ads.
banner_view->Initialize(ad_parent, banner_ad_unit, firebase::gma::AdSize.kBanner);
AdRequest
和全域配置
測試設備 ID、 TagForChildDirectedTreatment
和TagForUnderAgeOfConsent
(之前由生日處理)已從AdRequest
中刪除,現在是全域RequestConfiguration
的一部分。應用程式可以在應用程式生命週期的早期呼叫firebase::gma::SetRequestConfiguration()
來配置這些值。配置完成後,所有後續廣告載入操作都將遵循這些設定。
firebase::gma::AdRequest
仍然存在,因為它提供用於加載廣告的上下文訊息,包括關鍵字和可選的內容 URL。
AdMob 的AdRequest
C 風格結構已被替換為具有方法的類,這些方法在定義和附加到各種資訊清單時可提供更好的使用者體驗。
以下是AdRequest
顯著變化:
- Extras 現在與中介適配器類別名稱關聯。傳送至 AdMob 服務的附加內容應使用如下定義的預設類別名稱。
- 請求廣告時,應用程式可能會傳遞其所提供內容的 URL。這使得關鍵字定位能夠將廣告與正在顯示的其他內容相符。
前
firebase::admob::AdRequest request;
// Keywords to be used in targeting.
const char* keywords[] = {"GMA", "C++", "Fun"};
request.keyword_count = sizeof(keywords) / sizeof(keywords[0]);
request.keywords = keywords;
// "Extra" key value pairs.
static const firebase::admob::KeyValuePair extras[] = {
{"extra_name", "extra_value"}};
request.extras_count = sizeof(extras) / sizeof(extras[0]);
request.extras = kRequestExtras;
// Devices that should be served test ads.
const char* test_device_ids[] ={ "123", "4567", "890" };
request.test_device_id_count =
sizeof(test_device_ids) / sizeof(test_device_ids[0]);
request.test_device_ids = test_device_ids;
// Sample birthday to help determine the age of the user.
request.birthday_day = 10;
request.birthday_month = 11;
request.birthday_year = 1975;
// Load Ad with the AdRequest.
後
// Do once after Google Mobile Ads C++ SDK initialization.
// These settings will affect all Ad Load operations.
firebase::gma::RequestConfiguration configuration;
configuration.max_ad_content_rating =
firebase::gma::RequestConfiguration::kMaxAdContentRatingPG;
configuration.tag_for_child_directed_treatment =
firebase::gma::RequestConfiguration::kChildDirectedTreatmentTrue;
configuration.tag_for_under_age_of_consent =
firebase::gma::RequestConfiguration::kUnderAgeOfConsentFalse;
configuration.test_device_ids.push_back("1234");
configuration.test_device_ids.push_back("4567");
configuration.test_device_ids.push_back("890");
firebase::gma::SetRequestConfiguration(configuration);
// Then, more information must be provided via an AdRequest when
// loading individual ads.
firebase::gma::AdRequest ad_request;
// "Extra" key value pairs.
ad_request.add_keyword("GMA");
ad_request.add_keyword("C++");
ad_request.add_keyword("Fun");
// Content URL.
ad_request.set_content_url("www.example.com");
// Mediation Adapter Extras.
#if defined(Android)
const char* ad_network_extras_class_name =
"com/google/ads/mediation/admob/AdMobAdapter";
#else // iOS
const char* ad_network_extras_class_name = "GADExtras";
#endif
ad_request.add_extra(ad_network_extras_class_name, "extra_name", "extra_value");
// Load Ad with the AdRequest. See next section.
AdResults
LoadAd
現在傳回一個Future
,其中包含所有AdView
、 InterstitialAd
和RewardedAd
廣告類型的AdResult
物件。如果廣告要求成功完成,則AdResult::is_successful
方法傳回true
,否則false
。
失敗時, AdResult
包含一個AdError
對象,其中包含有關問題的服務等級訊息,包括錯誤代碼、錯誤訊息和域字串。
前
firebase::Future<AdResult> future;
void load_ad() {
// Assume an already created AdRequest object.
future = ad_view->LoadAd(ad_request);
}
void your_game_loop() {
if (future.status() == firebase::kFutureStatusComplete) {
if(future.error() != firebase::admob::kAdMobErrorNone) {
// There was either an internal SDK issue that caused the Future to
// fail its completion, or AdMob failed to fulfill the ad request.
// Details are unknown other than the Future’s error code returned
// from future.error().
} else {
// The ad loaded successfully.
}
}
}
後
firebase::Future<AdResult> future;
void load_ad() {
// Assumes a previously created AdRequest object.
// See "AdRequest and Global Configuration" above.
future = ad_view->LoadAd(ad_request);
}
void your_game_loop() {
// Check the future status in your game loop:
if (future.status() == firebase::kFutureStatusComplete) {
if(future.error() != firebase::admob::kAdErrorCodeNone) {
// There was an internal SDK issue that caused the Future to fail.
} else {
// Future completed successfully. Check the GMA result.
const AdResult* ad_result = future.result();
if ( ad_result->is_successful() != true ) {
// GMA failed to serve an ad. Gather information about the error.
const AdError& ad_error = ad_result->ad_error();
AdErrorCode error_code = ad_error.code();
const std::string error_domain = ad_error.domain();
const std::string error_message = ad_error.message();
} else {
// The ad loaded successfully.
}
}
}
}
AdView
中的AdListener
事件
AdMob 的BannerView::Listener
類別已替換為 Google 行動廣告 C++ SDK 中的兩個不同的偵聽器類別:
-
AdListener
追蹤廣告生命週期和使用者互動事件。 - 當調整
AdView
大小或移動 AdViewBoundingBoxListener 時,將會呼叫AdViewBoundingBoxListener
。
AdMob OnPresentationStateChanged
回呼 Google 行動廣告映射
新的 Google 行動廣告 C++ SDK 中不包含firebase::admob::BannerView::PresentationState
枚舉類型和OnPresentationStateChanged
偵聽器方法。
以下是偵測AdView
生命週期中呈現狀態變化的替代方法:
firebase::admob::BannerView::Listener OnPresentationStateChanged 事件 | firebase::gma::AdListener 對應項 |
---|---|
kPresentationStateHidden | 當AdListener::OnAdClosed 被呼叫時,或當AdView::Hide() 成功完成其非同步操作時 |
kPresentationStateVisibleWithoutAd | 沒有任何。嘗試呼叫已卸載的AdView AdView::Show() 將導致錯誤。 |
kPresentationStateVisibleWithAd | 當AdListener::OnAdOpened 被呼叫時,或當AdView::Show() 成功完成廣告的非同步操作時 |
kPresentationStateOpenedPartialOverlay | 呼叫AdListener::OnAdOpened() 後查詢邊界框以決定所顯示廣告的大小和位置。或者,查詢AdView 的位置和AdSize 和/或透過AdViewBoundingBoxListener 監視邊界框。 |
kPresentationStateCoveringUI | 請參閱上面的kPresentationStateOpenedPartialOverlay |
RewardedAd
現在是一個類
已棄用的 Firebase AdMob C++ SDK 透過firebase::admob::rewarded_ad
命名空間中的一組函數促進獎勵廣告。這些函數已合併到一個新的RewardedAd
類別中,該類別透過與InterstitialAd
類似的 API 介面來提供廣告(請參閱下一節)。
InterstitialAd
和RewardedAd
監聽器
插頁式廣告和激勵廣告都被視為全螢幕廣告。可以安裝新的FullScreenContentListener
來偵聽這些廣告類型的廣告生命週期事件,並且可以安裝單獨的PaidEventListener
來追蹤 AdMob 服務何時認為發生了付費事件。
RewardedAd
有一個額外的監聽器來監控使用者獲得的獎勵事件。
新的全螢幕廣告回檔方法
FullScreenContentListener 方法 | PaidEventListener 方法 | UserEarnedRewardListener 方法 |
---|---|---|
OnAdClicked | OnPaidEvent | OnUserEarnedReward |
OnAdDismissedFullScreenContent | ||
OnAdFailedToShowFullScreenContent | ||
OnAdImpression | ||
OnAdShowedFullScreenContent |
方法已更改/刪除/替換
下表列出了新版 Google 行動廣告 C++ SDK 中更改的具體方法。列出參數的方法仍然存在,但其簽名已更改。
班級 | Firebase AdMob C++ SDK API | Google 行動廣告 C++ SDK API | 筆記 |
---|---|---|---|
BannerView | MoveTo | AdView::SetPosition | |
presentation_state | 已刪除 | 由AdViewListener 事件以及AdView::Show 和AdView::Hide 未來結果處理。 | |
SetListener | AdView::SetAdListener AdView::SetBoundingBoxListener AdView::SetPaidEventListener | 新的偵聽器設計提高了偵測AdView 生命週期事件的保真度。 | |
Listener::OnPresentationStateChanged | 已刪除 | 請參閱上面的BannerView::SetListener 。 | |
Listener::OnBoundingBoxChanged | AdViewBoundingBoxListener::OnBoundingBoxChanged | ||
插頁式廣告 | Initialize(AdParent parent, const char* ad_unit_id) | Initialize(AdParent parent) | ad_unit_id 參數現在是LoadAd 操作的一部份。 |
LoadAd(const AdRequest& request) | LoadAd(const char* ad_unit_id, const AdRequest& request) | ||
presentation_state | 已刪除 | presentation_state 枚舉已被刪除。使用FullScreenContentListener 。 | |
SetListener | SetFullScreenContentListener SetPaidEventListener | ||
Destroy | 已刪除 | 清理資源現在是RewardedAd 析構函數的一部分。 | |
RewardedAd (正式地 RewardedVideoAd ) | Initialize | Initialize(AdParent parent) | AdParent 之前已傳遞給Show ,但現在是初始化的一部分。 |
presentation_state | 已刪除 | presentation_state 枚舉已被刪除。使用FullScreenContentListener 。 | |
SetListener | SetFullScreenContentListener SetPaidEventListener Show | 顯示RewardedAd 時也會定義UserEarnedReward 偵聽器。見下文。 | |
Show(AdParent parent) | Show(UserEarnedRewardListener* listener) |