تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تشغل "إعلانات البانر" مساحة ضمن تصميم التطبيق، إما في أعلى شاشة الجهاز أو أسفلها. وتظل معروضة على الشاشة أثناء تفاعل المستخدمين مع التطبيق، ويمكن إعادة تحميلها تلقائيًا بعد فترة زمنية محدّدة. إذا كنت جديدًا في مجال الإعلانات على الأجهزة الجوّالة، يمكنك البدء باستخدام هذه الشبكات.
دراسة حالة
(نظام التشغيل Android فقط) الإلمام بالعمل مع مراجع JNI jobject (راجِع نصائح حول JNI في Android).
اختبار الإعلانات دائمًا باستخدام الإعلانات الاختبارية
عند إنشاء تطبيقاتك واختبارها، احرص على استخدام إعلانات اختبارية بدلاً من الإعلانات المباشرة في مرحلة الإنتاج. وقد يؤدي عدم اتّخاذ هذا الإجراء إلى تعليق حسابك.
أسهل طريقة لتحميل الإعلانات الاختبارية هي استخدام معرّف وحدة إعلانية اختبارية مخصّصة لإعلانات البانر، ويختلف هذا المعرّف حسب نظام تشغيل الجهاز:
نظام تشغيل Android: ca-app-pub-3940256099942544/6300978111
نظام تشغيل iOS: ca-app-pub-3940256099942544/2934735716
تم إعداد معرّفات الوحدات الإعلانية هذه خصيصًا لعرض إعلانات اختبارية لكل طلب، ويمكنك استخدامها في تطبيقاتك الخاصة أثناء الترميز والاختبار وتصحيح الأخطاء. ما عليك سوى التأكّد من استبداله برقم تعريف وحدتك الإعلانية قبل نشر تطبيقك.
لمزيد من المعلومات عن طريقة عمل الإعلانات الاختبارية في حزمة تطوير البرامج (SDK) لإعلانات الأجهزة الجوّالة، يُرجى الاطّلاع على مقالة الإعلانات الاختبارية.
التنفيذ
إعداد AdView
يتم عرض "إعلانات البانر" في عناصر AdView، لذا فإنّ الخطوة الأولى نحو دمج "إعلانات البانر" هي إنشاء عنصر AdView وتحديد موضعه.
أنشِئ AdSize واضبط إعدادات عرض الإعلان باستخدام العرض الرئيسي AdParent. تمثّل طريقة العرض الرئيسية مرجع jobject JNI إلى Activity Android أو مؤشرًا إلى UIView iOS تم تحويله إلى نوع AdParent:
// 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);
كبديل للاحتفاظ بالمستقبل كمتغير، يمكنك التحقّق بشكل دوري من حالة عملية التهيئة من خلال استدعاء InitializeLastResult() على العنصر AdView. قد يكون ذلك مفيدًا لتتبُّع عملية الإعداد في حلقة لعبتك العامة.
// 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();
أحداث الإعلانات
توفّر حزمة تطوير البرامج (SDK) للغة C++ في "إعلانات Google على الأجهزة الجوّالة" الفئة AdListener التي يمكنك توسيعها وتمريرها إلى AdView::SetListener() من أجل تلقّي إشعارات بالتغييرات التي تطرأ على حالة عرض الإعلان.
توسيع الطرق في 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);
تاريخ التعديل الأخير: 2025-09-06 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\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/)."]]