// my_ad_parent is a jobject reference// to an Android Activity or a pointer to an iOS UIView.firebase::gma::AdParentad_parent=static_cast<firebase::gma::AdParent>(my_ad_parent);firebase::Futureresult=ad_view->Initialize(ad_parent,kBannerAdUnit,firebase::gma::AdSize::kBanner);
// Monitor the status of the future in your game loop:firebase::Future<void>result=ad_view->InitializeLastResult();if(result.status()==firebase::kFutureStatusComplete){// Initialization completed.if(future.error()==firebase::gma::kAdErrorCodeNone){// Initialization successful.}else{// An error has occurred.}}else{// Initialization on-going.}
classExampleAdListener:publicfirebase::gma::AdListener{public:ExampleAdListener(){}voidOnAdClicked()override{// This method is invoked when the user clicks the ad.}voidOnAdClosed()override{// This method is invoked when the user closes the ad.}voidOnAdImpression()override{// This method is invoked when an impression is recorded for an ad.}voidOnAdOpened()override{// This method is invoked when an ad opens an overlay that covers the screen.}};ExampleAdListener*ad_listener=newExampleAdListener();ad_view->SetAdListener(ad_listener);
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eBanner ads are rectangular image or text ads that occupy a spot within an app's layout, refreshing automatically at intervals.\u003c/p\u003e\n"],["\u003cp\u003eTo implement banner ads, developers need to configure an AdView object, set its position, and load an ad request using the Google Mobile Ads C++ SDK.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can listen for ad events like clicks and impressions by extending the AdListener class and registering it with the AdView.\u003c/p\u003e\n"],["\u003cp\u003eStandard banner sizes are available, and custom sizes can be defined using the AdSize constructor with specified width and height.\u003c/p\u003e\n"],["\u003cp\u003eAlways test with test ads to avoid account suspension before publishing your app.\u003c/p\u003e\n"]]],["Banner ads, displayed at the top or bottom of an app screen, utilize `AdView` objects. First, initialize an `AdView` and set its position using `SetPosition`. Load ads with `LoadAd` using an `AdRequest`, respecting the refresh rate. Display ads with `Show`. Use test ad unit IDs during development to prevent account suspension. Implement `AdListener` to monitor ad state changes. Standard and custom banner sizes are supported, offering flexibility in dimensions.\n"],null,["# Banner Ads\n\nBanner ads occupy a spot within an app's layout, either at the top or bottom of\nthe device screen. They stay on screen while users are interacting with the app,\nand can refresh automatically after a certain period of time. If you're new to\nmobile advertising, they're a great place to start.\n[Case study](//admob.google.com/home/resources/fingersoft-uses-admob-in-app-purchases-to-make-the-most-of-hill-climb-racing/).\n\nPrerequisites\n-------------\n\n- Complete [Get started](/admob/cpp/quick-start).\n- *(Android only)* Familiarity working with JNI `jobject` references (see [Android JNI tips](//developer.android.com/training/articles/perf-jni)).\n\nAlways test with test ads\n-------------------------\n\nWhen building and testing your apps, make sure you use test ads rather than\nlive, production ads. Failure to do so can lead to suspension of your account.\n\nThe easiest way to load test ads is to use our dedicated test ad unit ID for\nbanners, which varies per device platform:\n\n- Android: `ca-app-pub-3940256099942544/6300978111`\n- iOS: `ca-app-pub-3940256099942544/2934735716`\n\nThese ad unit IDs have been specially configured to return test ads for every\nrequest, and you're free to use it in your own apps while coding, testing, and\ndebugging. Just make sure you replace it with your own ad unit ID before\npublishing your app.\n\nFor more information about how the Mobile Ads SDK's test ads work, see\n[Test Ads](/admob/cpp/test-ads).\n\nImplementation\n--------------\n\n### Configure an `AdView`\n\nBanner ads are displayed in `AdView` objects, so the first step toward\nintegrating banner ads is to create and position an `AdView`.\n\n1. Add the following header to your app's C++ code:\n\n ```c++\n #include \"firebase/gma/ad_view.h\"\n ```\n2. Declare and instantiate an `AdView` object:\n\n ```c++\n firebase::gma::AdView* ad_view;\n ad_view = new firebase::gma::AdView();\n ```\n3. Create an `AdSize` and initialize the ad view using the `AdParent` parent\n view. The parent view is a JNI `jobject` reference to an Android `Activity`\n or a pointer to an iOS `UIView` cast to an `AdParent` type:\n\n ```c++\n // my_ad_parent is a jobject reference\n // to an Android Activity or a pointer to an iOS UIView.\n firebase::gma::AdParent ad_parent = static_cast\u003cfirebase::gma::AdParent\u003e(my_ad_parent);\n firebase::Future result =\n ad_view-\u003eInitialize(ad_parent, kBannerAdUnit, firebase::gma::AdSize::kBanner);\n ```\n4. As an alternative to retaining the future as a variable, you can\n periodically check the status of the initialization operation by invoking\n `InitializeLastResult()` on the `AdView` object. This may be helpful for\n keeping track of the initialization process in your global game loop.\n\n // Monitor the status of the future in your game loop:\n firebase::Future\u003cvoid\u003e result = ad_view-\u003eInitializeLastResult();\n if (result.status() == firebase::kFutureStatusComplete) {\n // Initialization completed.\n if(future.error() == firebase::gma::kAdErrorCodeNone) {\n // Initialization successful.\n } else {\n // An error has occurred.\n }\n } else {\n // Initialization on-going.\n }\n\n5. For more information about working with `firebase::Future`, see\n [Use Futures to monitor the completion status of method\n calls](/admob/cpp/quick-start#futures).\n\n### Set the ad's position\n\nYou can set the position of the `AdView` any time after it's initialized: \n\n firebase::Future\u003cvoid\u003e result = ad_view-\u003eSetPosition(firebase::gma::AdView::kPositionTop);\n\n### Load an ad\n\nYou can load an ad once the `AdView` has been initialized: \n\n firebase::gma::AdRequest ad_request;\n firebase::Future\u003cfirebase::gma::AdResult\u003e load_ad_result = ad_view-\u003eLoadAd(my_ad_request);\n\n`AdRequest` objects represent a single ad request and contain properties for\ninformation like targeting.\n| **Note:** If your ad fails to load, you don't need to explicitly request another one as long as you've configured your ad unit to refresh. The Google Mobile Ads SDK respects any refresh rate you specified in the AdMob UI. If you haven't enabled refresh, you will need to issue a new request.\n\n### Display the ad\n\nFinally, display the ad on the screen by calling `Show()`. This method may be\ninvoked any time after the ad has been initialized: \n\n firebase::Future\u003cvoid\u003e result = ad_view-\u003eShow();\n\nAd events\n---------\n\nThe Google Mobile Ads C++ SDK provides an `AdListener` class that you can extend\nand pass to `AdView::SetListener()` in order to be notified of changes to the ad\nview's state.\n\nExtending methods in `AdListener` is optional, so you only need to implement the\nmethods you want. Below is an example implementation of a class that extends all\nof the `AdListener` methods class: \n\n```c++\nclass ExampleAdListener\n : public firebase::gma::AdListener {\n public:\n ExampleAdListener() {}\n void OnAdClicked() override {\n // This method is invoked when the user clicks the ad.\n }\n\n void OnAdClosed() override {\n // This method is invoked when the user closes the ad.\n }\n\n void OnAdImpression() override {\n // This method is invoked when an impression is recorded for an ad.\n }\n\n void OnAdOpened() override {\n // This method is invoked when an ad opens an overlay that covers the screen.\n }\n};\n\nExampleAdListener* ad_listener = new ExampleAdListener();\nad_view-\u003eSetAdListener(ad_listener);\n```\n\nBanner sizes\n------------\n\nThe table below lists the standard banner sizes.\n\n| Size in points (WxH) | Description | Availability | firebase::gma::AdSize constant |\n|--------------------------------------|--------------------------------------------------------|--------------------|--------------------------------|\n| 320x50 | Banner | Phones and Tablets | `kBanner` |\n| 320x100 | Large Banner | Phones and Tablets | `kLargeBanner` |\n| 300x250 | IAB Medium Rectangle | Phones and Tablets | `kMediumRectangle` |\n| 468x60 | IAB Full-Size Banner | Tablets | `kFullBanner` |\n| 728x90 | IAB Leaderboard | Tablets | `kLeaderboard` |\n| *Provided width* x *Adaptive height* | [Adaptive banner](/admob/cpp/banner/anchored-adaptive) | Phones and Tablets | N/A |\n\nCustom ad sizes\n---------------\n\nTo define a custom banner size, set your desired dimensions using the\n[`firebase::gma::AdSize`](/admob/cpp/reference/class/firebase/gma/ad-size)\nconstructor with width and height parameters, as shown here: \n\n```c++\nfirebase::gma::AdSize ad_size(/*width=*/320, /*height=*/50);\n```\n\nAdditional resources\n--------------------\n\n### Example in GitHub\n\n- View the source code of our [example quickstart app](//github.com/firebase/quickstart-cpp/tree/main/gma/testapp) in GitHub.\n\n### Success stories\n\n- [Sample use case](//admob.google.com/home/resources/fingersoft-uses-admob-in-app-purchases-to-make-the-most-of-hill-climb-racing/)."]]