AdSize를 만들고 AdParent 상위 뷰를 사용하여 광고 뷰를 초기화합니다. 상위 뷰는 Android Activity에 대한 JNI jobject 참조 또는 AdParent 유형으로 캐스팅된 iOS UIView 포인터입니다.
// 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);
변수로 미래를 유지하는 대신 AdView 객체에서 InitializeLastResult()를 호출하여 초기화 작업의 상태를 주기적으로 확인할 수 있습니다. 이는 전역 게임 루프에서 초기화 프로세스를 추적하는 데 유용할 수 있습니다.
// 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.}
AdRequest 객체는 단일 광고 요청을 나타내며 타겟팅과 같은 정보의 속성을 포함합니다.
광고 표시
마지막으로 Show()를 호출하여 화면에 광고를 표시합니다. 이 메서드는 광고가 초기화된 후 언제든지 호출될 수 있습니다.
firebase::Future<void>result=ad_view->Show();
광고 이벤트
Google 모바일 광고 C++ SDK는 광고 뷰의 상태 변경에 대한 알림을 받을 수 있도록 확장하여 AdView::SetListener()에 전달할 수 있는 AdListener 클래스를 제공합니다.
AdListener의 메서드 확장은 선택사항이므로 원하는 메서드만 구현하면 됩니다. 다음은 모든 AdListener 메서드 클래스를 확장하는 클래스의 구현 예시입니다.
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);
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-06(UTC)"],[[["\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 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- 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\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\nConfigure 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\nSet 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\nLoad 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\nDisplay 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\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\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\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\nExample 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\nSuccess 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/)."]]