تبلیغات بومی

نمایش NativeAd

وقتی یک تبلیغ بومی بارگیری می‌شود، Google Mobile Ads SDK شنونده را برای قالب آگهی مربوطه فراخوانی می‌کند. سپس برنامه شما مسئول نمایش آگهی است، اگرچه لزوماً نباید فوراً این کار را انجام دهد. برای آسان‌تر کردن نمایش قالب‌های تبلیغاتی تعریف‌شده توسط سیستم، SDK منابع مفیدی را ارائه می‌دهد که در زیر توضیح داده شده است.

کلاس NativeAdView را تعریف کنید

یک کلاس NativeAdView تعریف کنید. این کلاس یک کلاس ViewGroup است و ظرف سطح بالای کلاس NativeAdView است. هر نمای تبلیغات بومی حاوی دارایی های تبلیغاتی بومی است، مانند عنصر نمای MediaView یا عنصر نمای Title ، که باید فرزند شی NativeAdView باشد.

طرح بندی XML

یک XML NativeAdView به پروژه خود اضافه کنید:

<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
    android:orientation="vertical">
        <LinearLayout
        android:orientation="horizontal">
          <ImageView
          android:id="@+id/ad_app_icon" />
          <TextView
            android:id="@+id/ad_headline" />
        </LinearLayout>
        <!--Add remaining assets such as the image and media view.-->
    </LinearLayout>
</com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView>

Jetpack Compose

شامل ماژول JetpackComposeDemo/compose-util که شامل کمک کننده هایی برای نوشتن NativeAdView و دارایی های آن است.

با استفاده از ماژول compose-util ، یک NativeAdView بنویسید:

  import com.google.android.gms.compose_util.NativeAdAttribution
  import com.google.android.gms.compose_util.NativeAdView

  @Composable
  /** Display a native ad with a user defined template. */
  fun DisplayNativeAdView(nativeAd: NativeAd) {
      NativeAdView {
          // Display the ad attribution.
          NativeAdAttribution(text = context.getString("Ad"))
          // Add remaining assets such as the image and media view.
        }
    }

aad بومی بارگذاری شده را مدیریت کنید

هنگامی که یک تبلیغ بومی بارگیری می‌شود، رویداد برگشت به تماس را مدیریت کنید، نمای تبلیغات بومی را افزایش دهید و آن را به سلسله مراتب مشاهده اضافه کنید:

کاتلین

// Build an ad request with native ad options to customize the ad.
val adTypes = listOf(NativeAd.NativeAdType.NATIVE)
val adRequest = NativeAdRequest
  .Builder("/21775744923/example/native", adTypes)
  .build()

val adCallback =
  object : NativeAdLoaderCallback {
    override fun onNativeAdLoaded(nativeAd: NativeAd) {
      activity?.runOnUiThread {

        val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
        val adView = nativeAdBinding.root
        val frameLayout = myActivityLayout.nativeAdPlaceholder

        // Populate and register the native ad asset views.
        displayNativeAd(nativeAd, nativeAdBinding)

        // Remove all old ad views and add the new native ad
        // view to the view hierarchy.
        frameLayout.removeAllViews()
        frameLayout.addView(adView)
      }
    }
  }

// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback)

جاوا

// Build an ad request with native ad options to customize the ad.
List<NativeAd.NativeAdType> adTypes = Arrays.asList(NativeAd.NativeAdType.NATIVE);
NativeAdRequest adRequest = new NativeAdRequest
                                .Builder("/21775744923/example/native", adTypes)
                                .build();

NativeAdLoaderCallback adCallback = new NativeAdLoaderCallback() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
      if (getActivity() != null) {
        getActivity()
          .runOnUiThread(() -> {
            // Inflate the native ad view and add it to the view hierarchy.
            NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
            NativeAdView adView = (NativeAdView) nativeAdBinding.getRoot();
            FrameLayout frameLayout = myActivityLayout.nativeAdPlaceholder;

            // Populate and register the native ad asset views.
            displayNativeAd(nativeAd, nativeAdBinding);

            // Remove all old ad views and add the new native ad
            // view to the view hierarchy.
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        });
      }
    }
};

// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback);

توجه داشته باشید که همه دارایی‌های یک آگهی بومی معین باید در طرح NativeAdView ارائه شوند. وقتی دارایی‌های بومی خارج از طرح‌بندی نمای تبلیغاتی بومی ارائه می‌شوند، Google Mobile Ads SDK سعی می‌کند هشداری را ثبت کند.

کلاس‌های ad view همچنین روش‌هایی را ارائه می‌دهند که برای ثبت نمای مورد استفاده برای هر دارایی جداگانه و یکی برای ثبت خود شی NativeAd استفاده می‌شوند. ثبت نماها به این روش به SDK اجازه می دهد تا به طور خودکار وظایفی مانند:

  • ضبط کلیک ها
  • ضبط برداشت زمانی که اولین پیکسل روی صفحه قابل مشاهده است
  • نمایش هم‌پوشانی AdChoices برای خلاقیت‌های پشتیبان بومی — در حال حاضر محدود به گروه منتخبی از ناشران است

همپوشانی AdChoices

هنگامی که یک آگهی پرکننده بازگردانده می شود، یک پوشش AdChoices به عنوان نمای تبلیغ توسط SDK اضافه می شود. اگر برنامه شما از پس‌پرده‌های تبلیغاتی بومی استفاده می‌کند، در گوشه دلخواه خود در نمای تبلیغاتی بومی خود فضایی برای نشان‌واره AdChoices درج شده به‌طور خودکار بگذارید. همچنین، مهم است که پوشش AdChoices دیده شود، بنابراین رنگ های پس زمینه و تصاویر را به طور مناسب انتخاب کنید. برای اطلاعات بیشتر در مورد ظاهر و عملکرد پوشش، به دستورالعمل‌های اجرای تبلیغات بومی برنامه‌ریزی شده مراجعه کنید.

تخصیص آگهی برای تبلیغات بومی برنامه‌ریزی‌شده

هنگام نمایش تبلیغات بومی برنامه‌ریزی شده، باید یک انتساب آگهی را نمایش دهید تا نشان دهد که این نما یک تبلیغ است. در دستورالعمل های خط مشی ما بیشتر بیاموزید.

نمونه کد

این مراحل برای نمایش یک آگهی بومی است:

  1. یک نمونه از کلاس NativeAdView ایجاد کنید.
  2. برای هر دارایی تبلیغاتی که باید نمایش داده شود:

    1. نمای دارایی را با دارایی موجود در شیء تبلیغ پر کنید.
    2. نمای دارایی را با کلاس NativeAdView ثبت کنید.
  3. شیء تبلیغ را با کلاس NativeAdView ثبت کنید.

در اینجا یک تابع مثال است که NativeAd نمایش می دهد:

کاتلین

private fun displayNativeAd(nativeAd: NativeAd, nativeAdBinding : NativeAdBinding) {
  // Set the native ad view elements.
  val nativeAdView = nativeAdBinding.root
  nativeAdView.advertiserView = nativeAdBinding.adAdvertiser
  nativeAdView.bodyView = nativeAdBinding.adBody
  nativeAdView.callToActionView = nativeAdBinding.adCallToAction
  nativeAdView.headlineView = nativeAdBinding.adHeadline
  nativeAdView.iconView = nativeAdBinding.adAppIcon
  nativeAdView.priceView = nativeAdBinding.adPrice
  nativeAdView.starRatingView = nativeAdBinding.adStars
  nativeAdView.storeView = nativeAdBinding.adStore

  // Set the view element with the native ad assets.
  nativeAdBinding.adAdvertiser.text = nativeAd.advertiser
  nativeAdBinding.adBody.text = nativeAd.body
  nativeAdBinding.adCallToAction.text = nativeAd.callToAction
  nativeAdBinding.adHeadline.text = nativeAd.headline
  nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable)
  nativeAdBinding.adPrice.text = nativeAd.price
  nativeAd.starRating?.toFloat().let { value ->
    nativeAdBinding.adStars.rating = value
  }
  nativeAdBinding.adStore.text = nativeAd.store

  // Hide views for assets that don't have data.
  nativeAdBinding.adAdvertiser.visibility = getAssetViewVisibility(nativeAd.advertiser)
  nativeAdBinding.adBody.visibility = getAssetViewVisibility(nativeAd.body)
  nativeAdBinding.adCallToAction.visibility = getAssetViewVisibility(nativeAd.callToAction)
  nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
  nativeAdBinding.adAppIcon.visibility = getAssetViewVisibility(nativeAd.icon)
  nativeAdBinding.adPrice.visibility = getAssetViewVisibility(nativeAd.price)
  nativeAdBinding.adStars.visibility = getAssetViewVisibility(nativeAd.starRating)
  nativeAdBinding.adStore.visibility = getAssetViewVisibility(nativeAd.store)

  // Inform the Google Mobile Ads SDK that you have finished populating
  // the native ad views with this native ad.
  nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia)
}

/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return [View.VISIBLE] if the asset is not null, [View.INVISIBLE] otherwise.
*/
private fun getAssetViewVisibility(asset: Any?): Int {
  return if (asset == null) View.INVISIBLE else View.VISIBLE
}

جاوا

private void displayNativeAd(ad: NativeAd, nativeAdBinding : NativeAdBinding) {
  // Set the native ad view elements.
  NativeAdView nativeAdView = nativeAdBinding.getRoot();
  nativeAdView.setAdvertiserView(nativeAdBinding.adAdvertiser);
  nativeAdView.setBodyView(nativeAdBinding.adBody);
  nativeAdView.setCallToActionView(nativeAdBinding.adCallToAction);
  nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
  nativeAdView.setIconView(nativeAdBinding.adAppIcon);
  nativeAdView.setPriceView(nativeAdBinding.adPrice);
  nativeAdView.setStarRatingView(nativeAdBinding.adStars);
  nativeAdView.setStoreView(nativeAdBinding.adStore);

  // Set the view element with the native ad assets.
  nativeAdBinding.adAdvertiser.setText(nativeAd.getAdvertiser());
  nativeAdBinding.adBody.setText(nativeAd.getBody());
  nativeAdBinding.adCallToAction.setText(nativeAd.getCallToAction());
  nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
  if (nativeAd.getIcon() != null) {
      nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.getIcon().getDrawable());
  }
  nativeAdBinding.adPrice.setText(nativeAd.getPrice());
  if (nativeAd.getStarRating() != null) {
      nativeAdBinding.adStars.setRating(nativeAd.getStarRating().floatValue());
  }
  nativeAdBinding.adStore.setText(nativeAd.getStore());

  // Hide views for assets that don't have data.
  nativeAdBinding.adAdvertiser.setVisibility(getAssetViewVisibility(nativeAd.getAdvertiser()));
  nativeAdBinding.adBody.setVisibility(getAssetViewVisibility(nativeAd.getBody()));
  nativeAdBinding.adCallToAction.setVisibility(getAssetViewVisibility(nativeAd.getCallToAction()));
  nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
  nativeAdBinding.adAppIcon.setVisibility(getAssetViewVisibility(nativeAd.getIcon()));
  nativeAdBinding.adPrice.setVisibility(getAssetViewVisibility(nativeAd.getPrice()));
  nativeAdBinding.adStars.setVisibility(getAssetViewVisibility(nativeAd.getStarRating()));
  nativeAdBinding.adStore.setVisibility(getAssetViewVisibility(nativeAd.getStore()));

  // Inform the Google Mobile Ads SDK that you have finished populating
  // the native ad views with this native ad.
  nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia);
}

/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return {@link View#VISIBLE} if the asset is not null, {@link View#INVISIBLE} otherwise.
*/
private int getAssetViewVisibility(Object asset) {
    return (asset == null) ? View.INVISIBLE : View.VISIBLE;
}

در اینجا وظایف فردی وجود دارد:

  1. طرح را باد کنید

    کاتلین

     // Remove all old ad views when loading a new native ad.
     binding.nativeViewContainer.removeAllViews()
    
     // Inflate the native ad view and add it to the view hierarchy.
     val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
     binding.nativeViewContainer.addView(nativeAdBinding.root)
    

    جاوا

     // Remove all old ad views when loading a new native ad.
     binding.nativeViewContainer.removeAllViews();
    
     // Inflate the native ad view and add it to the view hierarchy.
     NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
     binding.nativeViewContainer.addView(nativeAdBinding.getRoot());
    

    این کد یک طرح بندی XML را که حاوی نماهایی برای نمایش یک آگهی بومی است و سپس مکان مرجعی به NativeAdView را افزایش می دهد. توجه داشته باشید که اگر NativeAdView موجود در قطعه یا فعالیت شما وجود دارد، می‌توانید مجدداً از NativeAdView استفاده کنید، یا حتی یک نمونه را به صورت پویا بدون استفاده از فایل طرح‌بندی ایجاد کنید.

  2. نماهای دارایی را پر و ثبت کنید

    این کد نمونه، نمای مورد استفاده برای نمایش عنوان را تعیین می کند، متن آن را با استفاده از دارایی رشته ارائه شده توسط شی آگهی تنظیم می کند و آن را با شی NativeAdView ثبت می کند:

    کاتلین

     nativeAdView.headlineView = nativeAdBinding.adHeadline
     nativeAdBinding.adHeadline.text = nativeAd.headline
     nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
    

    جاوا

     nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
     nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
     nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
    

    این فرآیند مکان یابی نما، تنظیم مقدار آن، و ثبت آن در کلاس نمای تبلیغاتی باید برای هر یک از دارایی های ارائه شده توسط شی تبلیغاتی بومی که برنامه نمایش می دهد تکرار شود.

  3. رسیدگی به کلیک ها

    هیچ کنترل کننده کلیک سفارشی را روی هیچ نمایی در داخل یا در نمای تبلیغاتی بومی اجرا نکنید. کلیک‌ها روی دارایی‌های مشاهده آگهی توسط SDK انجام می‌شود تا زمانی که بازدیدهای دارایی را به درستی پر کرده و ثبت کنید، همانطور که در بخش قبل بحث شد.

    برای گوش دادن به کلیک‌ها، پاسخ تماس کلیکی Google Mobile Ads SDK را اجرا کنید:

    کاتلین

     private fun setEventCallback(nativeAd: NativeAd) {
       nativeAd.adEventCallback =
         object : NativeAdEventCallback {
           override fun onAdClicked() {
             Log.d(Constant.TAG, "Native ad recorded a click.")
           }
         }
     }
    

    جاوا

     private void setEventCallback(NativeAd nativeAd) {
       nativeAd.setAdEventCallback(new NativeAdEventCallback() {
           @Override
           public void onAdClicked() {
             Log.d(Constant.TAG, "Native ad recorded a click.");
           }
       });
     }
    
  4. MediaView را ثبت کنید

    اگر می‌خواهید یک دارایی تصویر اصلی را در طرح‌بندی آگهی بومی خود بگنجانید، باید از دارایی MediaView به جای دارایی ImageView استفاده کنید.

    کاتلین

    // Get the media asset view.
    val mediaView = nativeAdBinding.adMedia
    

    جاوا

    // Get the media asset view.
    MediaView mediaView = nativeAdBinding.adMedia;
    

    ImageScaleType

    کلاس MediaView دارای ویژگی ImageScaleType هنگام نمایش تصاویر است. اگر می خواهید نحوه مقیاس بندی یک تصویر را در MediaView تغییر دهید، ImageView.ScaleType مربوطه را با استفاده از متد setImageScaleType() MediaView تنظیم کنید:

    کاتلین

    nativeAdViewBinding.mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
    

    جاوا

    nativeAdViewBinding.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
    

    MediaContent

    کلاس MediaContent داده های مربوط به محتوای رسانه ای تبلیغ بومی را نگه می دارد که با استفاده از کلاس MediaView نمایش داده می شود. هنگامی که ویژگی MediaView mediaContent با یک نمونه MediaContent تنظیم می شود:

    • اگر دارایی ویدیویی در دسترس باشد، بافر شده و در MediaView شروع به پخش می‌کند. با بررسی hasVideoContent() می‌توانید متوجه شوید که یک دارایی ویدیو در دسترس است.

    • اگر تبلیغ حاوی دارایی ویدیو نباشد، دارایی mainImage دانلود شده و در داخل MediaView قرار می گیرد.

    اگر از disableImageDownloading(true) استفاده می شود، mainImage null است و باید ویژگی mainImage را روی تصویر دانلود شده دستی خود تنظیم کنید. توجه داشته باشید که این تصویر تنها زمانی استفاده خواهد شد که هیچ دارایی ویدیویی در دسترس نباشد.

  5. شیء تبلیغاتی بومی را ثبت کنید

    این مرحله نهایی، شیء تبلیغاتی بومی را با نمای مسئول نمایش آن، همراه با نمای دارایی محتوای رسانه، ثبت می‌کند.

    کاتلین

     // Inform the Google Mobile Ads SDK that you have finished populating
     // the native ad views with this native ad and media content asset.
     nativeAdView.registerNativeAd(nativeAd, mediaView)
    

    جاوا

     // Inform the Google Mobile Ads SDK that you have finished populating
     // the native ad views with this native ad and media content asset.
     nativeAdView.registerNativeAd(nativeAd, mediaView);
    

تبلیغ را نابود کنید

پس از اتمام نمایش آگهی بومی خود، باید آن را از بین ببرید تا آگهی به درستی زباله جمع آوری شود.

کاتلین

nativeAd.destroy()

جاوا

nativeAd.destroy();

کد تبلیغات بومی را تست کنید

آگهی های فروش مستقیم

اگر می‌خواهید آزمایش کنید تبلیغات بومی با فروش مستقیم چگونه هستند، می‌توانید از این شناسه واحد تبلیغات Ad Manager استفاده کنید:

/21775744923/example/native

برای ارائه نمونه تبلیغات نصب برنامه و محتوا و همچنین یک قالب آگهی بومی سفارشی با دارایی های زیر پیکربندی شده است:

  • عنوان (متن)
  • تصویر اصلی (تصویر)
  • عنوان (متن)

شناسه قالب برای قالب تبلیغات بومی سفارشی 10063170 است.

تبلیغات پشتیبان بومی

پر کردن Ad Exchange به گروه منتخبی از ناشران محدود می شود. برای آزمایش رفتار تبلیغات پشتیبان بومی، از این واحد تبلیغاتی Ad Manager استفاده کنید:

/21775744923/example/native-backfill

این نمونه تبلیغات نصب برنامه و محتوایی را ارائه می دهد که شامل پوشش AdChoices است.

به یاد داشته باشید قبل از پخش زنده، کد خود را برای مراجعه به واحد تبلیغات واقعی و شناسه های الگو به روز کنید.

مراحل بعدی

موضوعات زیر را بررسی کنید:

مثال

برنامه مثالی را که استفاده از نسل بعدی Google Mobile Ads SDK را نشان می دهد، دانلود و اجرا کنید.

،

نمایش NativeAd

وقتی یک تبلیغ بومی بارگیری می‌شود، Google Mobile Ads SDK شنونده را برای قالب آگهی مربوطه فراخوانی می‌کند. سپس برنامه شما مسئول نمایش آگهی است، اگرچه لزوماً نباید فوراً این کار را انجام دهد. برای آسان‌تر کردن نمایش قالب‌های تبلیغاتی تعریف‌شده توسط سیستم، SDK منابع مفیدی را ارائه می‌دهد که در زیر توضیح داده شده است.

کلاس NativeAdView را تعریف کنید

یک کلاس NativeAdView تعریف کنید. این کلاس یک کلاس ViewGroup است و ظرف سطح بالای کلاس NativeAdView است. هر نمای تبلیغات بومی حاوی دارایی های تبلیغاتی بومی است، مانند عنصر نمای MediaView یا عنصر نمای Title ، که باید فرزند شی NativeAdView باشد.

طرح بندی XML

یک XML NativeAdView به پروژه خود اضافه کنید:

<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
    android:orientation="vertical">
        <LinearLayout
        android:orientation="horizontal">
          <ImageView
          android:id="@+id/ad_app_icon" />
          <TextView
            android:id="@+id/ad_headline" />
        </LinearLayout>
        <!--Add remaining assets such as the image and media view.-->
    </LinearLayout>
</com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView>

Jetpack Compose

شامل ماژول JetpackComposeDemo/compose-util که شامل کمک کننده هایی برای نوشتن NativeAdView و دارایی های آن است.

با استفاده از ماژول compose-util ، یک NativeAdView بنویسید:

  import com.google.android.gms.compose_util.NativeAdAttribution
  import com.google.android.gms.compose_util.NativeAdView

  @Composable
  /** Display a native ad with a user defined template. */
  fun DisplayNativeAdView(nativeAd: NativeAd) {
      NativeAdView {
          // Display the ad attribution.
          NativeAdAttribution(text = context.getString("Ad"))
          // Add remaining assets such as the image and media view.
        }
    }

aad بومی بارگذاری شده را مدیریت کنید

هنگامی که یک تبلیغ بومی بارگیری می‌شود، رویداد برگشت به تماس را مدیریت کنید، نمای تبلیغات بومی را افزایش دهید و آن را به سلسله مراتب مشاهده اضافه کنید:

کاتلین

// Build an ad request with native ad options to customize the ad.
val adTypes = listOf(NativeAd.NativeAdType.NATIVE)
val adRequest = NativeAdRequest
  .Builder("/21775744923/example/native", adTypes)
  .build()

val adCallback =
  object : NativeAdLoaderCallback {
    override fun onNativeAdLoaded(nativeAd: NativeAd) {
      activity?.runOnUiThread {

        val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
        val adView = nativeAdBinding.root
        val frameLayout = myActivityLayout.nativeAdPlaceholder

        // Populate and register the native ad asset views.
        displayNativeAd(nativeAd, nativeAdBinding)

        // Remove all old ad views and add the new native ad
        // view to the view hierarchy.
        frameLayout.removeAllViews()
        frameLayout.addView(adView)
      }
    }
  }

// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback)

جاوا

// Build an ad request with native ad options to customize the ad.
List<NativeAd.NativeAdType> adTypes = Arrays.asList(NativeAd.NativeAdType.NATIVE);
NativeAdRequest adRequest = new NativeAdRequest
                                .Builder("/21775744923/example/native", adTypes)
                                .build();

NativeAdLoaderCallback adCallback = new NativeAdLoaderCallback() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
      if (getActivity() != null) {
        getActivity()
          .runOnUiThread(() -> {
            // Inflate the native ad view and add it to the view hierarchy.
            NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
            NativeAdView adView = (NativeAdView) nativeAdBinding.getRoot();
            FrameLayout frameLayout = myActivityLayout.nativeAdPlaceholder;

            // Populate and register the native ad asset views.
            displayNativeAd(nativeAd, nativeAdBinding);

            // Remove all old ad views and add the new native ad
            // view to the view hierarchy.
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        });
      }
    }
};

// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback);

توجه داشته باشید که همه دارایی‌های یک آگهی بومی معین باید در طرح NativeAdView ارائه شوند. وقتی دارایی‌های بومی خارج از طرح‌بندی نمای تبلیغاتی بومی ارائه می‌شوند، Google Mobile Ads SDK سعی می‌کند هشداری را ثبت کند.

کلاس‌های ad view همچنین روش‌هایی را ارائه می‌دهند که برای ثبت نمای مورد استفاده برای هر دارایی جداگانه و یکی برای ثبت خود شی NativeAd استفاده می‌شوند. ثبت نماها به این روش به SDK اجازه می دهد تا به طور خودکار وظایفی مانند:

  • ضبط کلیک ها
  • ضبط برداشت زمانی که اولین پیکسل روی صفحه قابل مشاهده است
  • نمایش هم‌پوشانی AdChoices برای خلاقیت‌های پشتیبان بومی — در حال حاضر محدود به گروه منتخبی از ناشران است

همپوشانی AdChoices

هنگامی که یک آگهی پرکننده بازگردانده می شود، یک پوشش AdChoices به عنوان نمای تبلیغ توسط SDK اضافه می شود. اگر برنامه شما از پس‌پرده‌های تبلیغاتی بومی استفاده می‌کند، در گوشه دلخواه خود در نمای تبلیغاتی بومی خود فضایی برای نشان‌واره AdChoices درج شده به‌طور خودکار بگذارید. همچنین، مهم است که پوشش AdChoices دیده شود، بنابراین رنگ های پس زمینه و تصاویر را به طور مناسب انتخاب کنید. برای اطلاعات بیشتر در مورد ظاهر و عملکرد پوشش، به دستورالعمل‌های اجرای تبلیغات بومی برنامه‌ریزی شده مراجعه کنید.

تخصیص آگهی برای تبلیغات بومی برنامه‌ریزی‌شده

هنگام نمایش تبلیغات بومی برنامه‌ریزی شده، باید یک انتساب آگهی را نمایش دهید تا نشان دهد که این نما یک تبلیغ است. در دستورالعمل های خط مشی ما بیشتر بیاموزید.

نمونه کد

این مراحل برای نمایش یک آگهی بومی است:

  1. یک نمونه از کلاس NativeAdView ایجاد کنید.
  2. برای هر دارایی تبلیغاتی که باید نمایش داده شود:

    1. نمای دارایی را با دارایی موجود در شیء تبلیغ پر کنید.
    2. نمای دارایی را با کلاس NativeAdView ثبت کنید.
  3. شیء تبلیغ را با کلاس NativeAdView ثبت کنید.

در اینجا یک تابع مثال است که NativeAd نمایش می دهد:

کاتلین

private fun displayNativeAd(nativeAd: NativeAd, nativeAdBinding : NativeAdBinding) {
  // Set the native ad view elements.
  val nativeAdView = nativeAdBinding.root
  nativeAdView.advertiserView = nativeAdBinding.adAdvertiser
  nativeAdView.bodyView = nativeAdBinding.adBody
  nativeAdView.callToActionView = nativeAdBinding.adCallToAction
  nativeAdView.headlineView = nativeAdBinding.adHeadline
  nativeAdView.iconView = nativeAdBinding.adAppIcon
  nativeAdView.priceView = nativeAdBinding.adPrice
  nativeAdView.starRatingView = nativeAdBinding.adStars
  nativeAdView.storeView = nativeAdBinding.adStore

  // Set the view element with the native ad assets.
  nativeAdBinding.adAdvertiser.text = nativeAd.advertiser
  nativeAdBinding.adBody.text = nativeAd.body
  nativeAdBinding.adCallToAction.text = nativeAd.callToAction
  nativeAdBinding.adHeadline.text = nativeAd.headline
  nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable)
  nativeAdBinding.adPrice.text = nativeAd.price
  nativeAd.starRating?.toFloat().let { value ->
    nativeAdBinding.adStars.rating = value
  }
  nativeAdBinding.adStore.text = nativeAd.store

  // Hide views for assets that don't have data.
  nativeAdBinding.adAdvertiser.visibility = getAssetViewVisibility(nativeAd.advertiser)
  nativeAdBinding.adBody.visibility = getAssetViewVisibility(nativeAd.body)
  nativeAdBinding.adCallToAction.visibility = getAssetViewVisibility(nativeAd.callToAction)
  nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
  nativeAdBinding.adAppIcon.visibility = getAssetViewVisibility(nativeAd.icon)
  nativeAdBinding.adPrice.visibility = getAssetViewVisibility(nativeAd.price)
  nativeAdBinding.adStars.visibility = getAssetViewVisibility(nativeAd.starRating)
  nativeAdBinding.adStore.visibility = getAssetViewVisibility(nativeAd.store)

  // Inform the Google Mobile Ads SDK that you have finished populating
  // the native ad views with this native ad.
  nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia)
}

/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return [View.VISIBLE] if the asset is not null, [View.INVISIBLE] otherwise.
*/
private fun getAssetViewVisibility(asset: Any?): Int {
  return if (asset == null) View.INVISIBLE else View.VISIBLE
}

جاوا

private void displayNativeAd(ad: NativeAd, nativeAdBinding : NativeAdBinding) {
  // Set the native ad view elements.
  NativeAdView nativeAdView = nativeAdBinding.getRoot();
  nativeAdView.setAdvertiserView(nativeAdBinding.adAdvertiser);
  nativeAdView.setBodyView(nativeAdBinding.adBody);
  nativeAdView.setCallToActionView(nativeAdBinding.adCallToAction);
  nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
  nativeAdView.setIconView(nativeAdBinding.adAppIcon);
  nativeAdView.setPriceView(nativeAdBinding.adPrice);
  nativeAdView.setStarRatingView(nativeAdBinding.adStars);
  nativeAdView.setStoreView(nativeAdBinding.adStore);

  // Set the view element with the native ad assets.
  nativeAdBinding.adAdvertiser.setText(nativeAd.getAdvertiser());
  nativeAdBinding.adBody.setText(nativeAd.getBody());
  nativeAdBinding.adCallToAction.setText(nativeAd.getCallToAction());
  nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
  if (nativeAd.getIcon() != null) {
      nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.getIcon().getDrawable());
  }
  nativeAdBinding.adPrice.setText(nativeAd.getPrice());
  if (nativeAd.getStarRating() != null) {
      nativeAdBinding.adStars.setRating(nativeAd.getStarRating().floatValue());
  }
  nativeAdBinding.adStore.setText(nativeAd.getStore());

  // Hide views for assets that don't have data.
  nativeAdBinding.adAdvertiser.setVisibility(getAssetViewVisibility(nativeAd.getAdvertiser()));
  nativeAdBinding.adBody.setVisibility(getAssetViewVisibility(nativeAd.getBody()));
  nativeAdBinding.adCallToAction.setVisibility(getAssetViewVisibility(nativeAd.getCallToAction()));
  nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
  nativeAdBinding.adAppIcon.setVisibility(getAssetViewVisibility(nativeAd.getIcon()));
  nativeAdBinding.adPrice.setVisibility(getAssetViewVisibility(nativeAd.getPrice()));
  nativeAdBinding.adStars.setVisibility(getAssetViewVisibility(nativeAd.getStarRating()));
  nativeAdBinding.adStore.setVisibility(getAssetViewVisibility(nativeAd.getStore()));

  // Inform the Google Mobile Ads SDK that you have finished populating
  // the native ad views with this native ad.
  nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia);
}

/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return {@link View#VISIBLE} if the asset is not null, {@link View#INVISIBLE} otherwise.
*/
private int getAssetViewVisibility(Object asset) {
    return (asset == null) ? View.INVISIBLE : View.VISIBLE;
}

در اینجا وظایف فردی وجود دارد:

  1. طرح را باد کنید

    کاتلین

     // Remove all old ad views when loading a new native ad.
     binding.nativeViewContainer.removeAllViews()
    
     // Inflate the native ad view and add it to the view hierarchy.
     val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
     binding.nativeViewContainer.addView(nativeAdBinding.root)
    

    جاوا

     // Remove all old ad views when loading a new native ad.
     binding.nativeViewContainer.removeAllViews();
    
     // Inflate the native ad view and add it to the view hierarchy.
     NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
     binding.nativeViewContainer.addView(nativeAdBinding.getRoot());
    

    این کد یک طرح بندی XML را که حاوی نماهایی برای نمایش یک آگهی بومی است و سپس مکان مرجعی به NativeAdView را افزایش می دهد. توجه داشته باشید که اگر NativeAdView موجود در قطعه یا فعالیت شما وجود دارد، می‌توانید مجدداً از NativeAdView استفاده کنید، یا حتی یک نمونه را به صورت پویا بدون استفاده از فایل طرح‌بندی ایجاد کنید.

  2. نماهای دارایی را پر و ثبت کنید

    این کد نمونه، نمای مورد استفاده برای نمایش عنوان را تعیین می کند، متن آن را با استفاده از دارایی رشته ارائه شده توسط شی آگهی تنظیم می کند و آن را با شی NativeAdView ثبت می کند:

    کاتلین

     nativeAdView.headlineView = nativeAdBinding.adHeadline
     nativeAdBinding.adHeadline.text = nativeAd.headline
     nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
    

    جاوا

     nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
     nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
     nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
    

    این فرآیند مکان یابی نما، تنظیم مقدار آن، و ثبت آن در کلاس نمای تبلیغاتی باید برای هر یک از دارایی های ارائه شده توسط شی تبلیغاتی بومی که برنامه نمایش می دهد تکرار شود.

  3. رسیدگی به کلیک ها

    هیچ کنترل کننده کلیک سفارشی را روی هیچ نمایی در داخل یا در نمای تبلیغاتی بومی اجرا نکنید. کلیک‌ها روی دارایی‌های مشاهده آگهی توسط SDK انجام می‌شود تا زمانی که بازدیدهای دارایی را به درستی پر کرده و ثبت کنید، همانطور که در بخش قبل بحث شد.

    برای گوش دادن به کلیک‌ها، پاسخ تماس کلیکی Google Mobile Ads SDK را اجرا کنید:

    کاتلین

     private fun setEventCallback(nativeAd: NativeAd) {
       nativeAd.adEventCallback =
         object : NativeAdEventCallback {
           override fun onAdClicked() {
             Log.d(Constant.TAG, "Native ad recorded a click.")
           }
         }
     }
    

    جاوا

     private void setEventCallback(NativeAd nativeAd) {
       nativeAd.setAdEventCallback(new NativeAdEventCallback() {
           @Override
           public void onAdClicked() {
             Log.d(Constant.TAG, "Native ad recorded a click.");
           }
       });
     }
    
  4. MediaView را ثبت کنید

    اگر می‌خواهید یک دارایی تصویر اصلی را در طرح‌بندی آگهی بومی خود بگنجانید، باید از دارایی MediaView به جای دارایی ImageView استفاده کنید.

    کاتلین

    // Get the media asset view.
    val mediaView = nativeAdBinding.adMedia
    

    جاوا

    // Get the media asset view.
    MediaView mediaView = nativeAdBinding.adMedia;
    

    ImageScaleType

    کلاس MediaView دارای ویژگی ImageScaleType هنگام نمایش تصاویر است. اگر می خواهید نحوه مقیاس بندی یک تصویر را در MediaView تغییر دهید، ImageView.ScaleType مربوطه را با استفاده از متد setImageScaleType() MediaView تنظیم کنید:

    کاتلین

    nativeAdViewBinding.mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
    

    جاوا

    nativeAdViewBinding.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
    

    MediaContent

    کلاس MediaContent داده های مربوط به محتوای رسانه ای تبلیغ بومی را نگه می دارد که با استفاده از کلاس MediaView نمایش داده می شود. هنگامی که ویژگی MediaView mediaContent با یک نمونه MediaContent تنظیم می شود:

    • اگر دارایی ویدیویی در دسترس باشد، بافر شده و در MediaView شروع به پخش می‌کند. با بررسی hasVideoContent() می‌توانید متوجه شوید که یک دارایی ویدیو در دسترس است.

    • اگر تبلیغ حاوی دارایی ویدیو نباشد، دارایی mainImage دانلود شده و در داخل MediaView قرار می گیرد.

    اگر از disableImageDownloading(true) استفاده می شود، mainImage null است و باید ویژگی mainImage را روی تصویر دانلود شده دستی خود تنظیم کنید. توجه داشته باشید که این تصویر تنها زمانی استفاده خواهد شد که هیچ دارایی ویدیویی در دسترس نباشد.

  5. شیء تبلیغاتی بومی را ثبت کنید

    این مرحله نهایی، شیء تبلیغاتی بومی را با نمای مسئول نمایش آن، همراه با نمای دارایی محتوای رسانه، ثبت می‌کند.

    کاتلین

     // Inform the Google Mobile Ads SDK that you have finished populating
     // the native ad views with this native ad and media content asset.
     nativeAdView.registerNativeAd(nativeAd, mediaView)
    

    جاوا

     // Inform the Google Mobile Ads SDK that you have finished populating
     // the native ad views with this native ad and media content asset.
     nativeAdView.registerNativeAd(nativeAd, mediaView);
    

تبلیغ را نابود کنید

پس از اتمام نمایش آگهی بومی خود، باید آن را از بین ببرید تا آگهی به درستی زباله جمع آوری شود.

کاتلین

nativeAd.destroy()

جاوا

nativeAd.destroy();

کد تبلیغات بومی را تست کنید

آگهی های فروش مستقیم

اگر می‌خواهید آزمایش کنید تبلیغات بومی با فروش مستقیم چگونه هستند، می‌توانید از این شناسه واحد تبلیغات Ad Manager استفاده کنید:

/21775744923/example/native

برای ارائه نمونه تبلیغات نصب برنامه و محتوا و همچنین یک قالب آگهی بومی سفارشی با دارایی های زیر پیکربندی شده است:

  • عنوان (متن)
  • تصویر اصلی (تصویر)
  • عنوان (متن)

شناسه قالب برای قالب تبلیغات بومی سفارشی 10063170 است.

تبلیغات پشتیبان بومی

پر کردن Ad Exchange به گروه منتخبی از ناشران محدود می شود. برای آزمایش رفتار تبلیغات پشتیبان بومی، از این واحد تبلیغاتی Ad Manager استفاده کنید:

/21775744923/example/native-backfill

این نمونه تبلیغات نصب برنامه و محتوایی را ارائه می دهد که شامل پوشش AdChoices است.

به یاد داشته باشید قبل از پخش زنده، کد خود را برای مراجعه به واحد تبلیغات واقعی و شناسه های الگو به روز کنید.

مراحل بعدی

موضوعات زیر را بررسی کنید:

مثال

برنامه مثالی را که استفاده از نسل بعدی Google Mobile Ads SDK را نشان می دهد، دانلود و اجرا کنید.