नेटिव ऐडवांस्ड

प्लैटफ़ॉर्म चुनें: Android iOS

नेटिव विज्ञापन दिखाना

जब कोई नेटिव विज्ञापन लोड होता है, तो Google Mobile Ads SDK, उससे जुड़े विज्ञापन फ़ॉर्मैट के लिए, लिसनर को ट्रिगर करता है. इसके बाद, विज्ञापन दिखाने की ज़िम्मेदारी आपके ऐप्लिकेशन की होती है. हालांकि, यह ज़रूरी नहीं है कि वह तुरंत विज्ञापन दिखाए. सिस्टम से तय किए गए विज्ञापन फ़ॉर्मैट को आसानी से दिखाने के लिए, SDK टूल कुछ काम के संसाधन उपलब्ध कराता है. इनके बारे में यहां बताया गया है.

NativeAdView क्लास तय करना

NativeAdView क्लास तय करें. यह क्लास एक ViewGroup क्लास है और NativeAdView क्लास के लिए टॉप लेवल कंटेनर है. हर नेटिव विज्ञापन व्यू में नेटिव विज्ञापन ऐसेट होती हैं. जैसे, MediaView व्यू एलिमेंट या Title व्यू एलिमेंट, जो NativeAdView ऑब्जेक्ट का चाइल्ड होना चाहिए.

एक्सएमएल लेआउट

अपने प्रोजेक्ट में एक्सएमएल NativeAdView जोड़ें:

<com.google.android.gms.ads.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.gms.ads.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 को मैनेज करना

जब कोई नेटिव विज्ञापन लोड होता है, तो कॉलबैक इवेंट को मैनेज करें, नेटिव विज्ञापन व्यू को फ़ुल फ़ॉर्मैट में दिखाएं, और उसे व्यू हैरारकी में जोड़ें:

Java

AdLoader.Builder builder = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
    .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
        @Override
        public void onNativeAdLoaded(NativeAd nativeAd) {
            // Assumes you have a placeholder FrameLayout in your View layout
            // (with ID fl_adplaceholder) where the ad is to be placed.
            FrameLayout frameLayout =
                findViewById(R.id.fl_adplaceholder);
            // Assumes that your ad layout is in a file call native_ad_layout.xml
            // in the res/layout folder
            NativeAdView adView = (NativeAdView) getLayoutInflater()
                .inflate(R.layout.native_ad_layout, null);
            // This method sets the assets into the ad view.
            populateNativeAdView(nativeAd, adView);
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        }
});

Kotlin

val builder = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
    .forNativeAd { nativeAd ->
        // Assumes you have a placeholder FrameLayout in your View layout
        // (with ID fl_adplaceholder) where the ad is to be placed.
        val frameLayout: FrameLayout = findViewById(R.id.fl_adplaceholder)
        // Assumes that your ad layout is in a file call native_ad_layout.xml
        // in the res/layout folder
        val adView = layoutInflater
                .inflate(R.layout.native_ad_layout, null) as NativeAdView
        // This method sets the assets into the ad view.
        populateNativeAdView(nativeAd, adView)
        frameLayout.removeAllViews()
        frameLayout.addView(adView)
    }

Jetpack Compose

@Composable
/** Load and display a native ad. */
fun NativeScreen() {
  var nativeAd by remember { mutableStateOf<NativeAd?>(null) }
  val context = LocalContext.current
  var isDisposed by remember { mutableStateOf(false) }

  DisposableEffect(Unit) {
    // Load the native ad when we launch this screen
    loadNativeAd(
      context = context,
      onAdLoaded = { ad ->
        // Handle the native ad being loaded.
        if (!isDisposed) {
          nativeAd = ad
        } else {
          // Destroy the native ad if loaded after the screen is disposed.
          ad.destroy()
        }
      },
    )
    // Destroy the native ad to prevent memory leaks when we dispose of this screen.
    onDispose {
      isDisposed = true
      nativeAd?.destroy()
      nativeAd = null
    }
  }

  // Display the native ad view with a user defined template.
  nativeAd?.let { adValue -> DisplayNativeAdView(adValue) }
}

fun loadNativeAd(context: Context, onAdLoaded: (NativeAd) -> Unit) {
  val adLoader =
    AdLoader.Builder(context, NATIVE_AD_UNIT_ID)
      .forNativeAd { nativeAd -> onAdLoaded(nativeAd) }
      .withAdListener(
        object : AdListener() {
          override fun onAdFailedToLoad(error: LoadAdError) {
            Log.e(TAG, "Native ad failed to load: ${error.message}")
          }

          override fun onAdLoaded() {
            Log.d(TAG, "Native ad was loaded.")
          }

          override fun onAdImpression() {
            Log.d(TAG, "Native ad recorded an impression.")
          }

          override fun onAdClicked() {
            Log.d(TAG, "Native ad was clicked.")
          }
        }
      )
      .build()
  adLoader.loadAd(AdRequest.Builder().build())
}

ध्यान दें कि किसी नेटिव विज्ञापन की सभी ऐसेट, NativeAdView लेआउट में रेंडर की जानी चाहिए. जब नेटिव एसेट, नेटिव विज्ञापन व्यू लेआउट के बाहर रेंडर की जाती हैं, तो Google Mobile Ads SDK, चेतावनी को लॉग करने की कोशिश करता है.

विज्ञापन व्यू क्लास में, हर ऐसेट के लिए इस्तेमाल किए गए व्यू को रजिस्टर करने के तरीके भी दिए जाते हैं. साथ ही, NativeAd ऑब्जेक्ट को रजिस्टर करने का एक तरीका भी दिया जाता है. व्यू को इस तरह से रजिस्टर करने से, SDK टूल अपने-आप ये काम कर सकता है:

  • क्लिक रिकॉर्ड करना
  • स्क्रीन पर पहला पिक्सल दिखने पर इंप्रेशन रिकॉर्ड करना
  • AdChoices ओवरले दिखाना

AdChoices ओवरले

SDK टूल, हर विज्ञापन व्यू में AdChoices ओवरले जोड़ता है. अपने नेटिव विज्ञापन व्यू के पसंदीदा कोने में जगह छोड़ें, ताकि AdChoices का लोगो अपने-आप दिख सके. साथ ही, यह ज़रूरी है कि AdChoices ओवरले आसानी से दिखे. इसलिए, बैकग्राउंड के लिए सही रंग और इमेज चुनें. ओवरले के दिखने और काम करने के तरीके के बारे में ज़्यादा जानने के लिए, नेटिव विज्ञापन फ़ील्ड की जानकारी देखें.

विज्ञापन एट्रिब्यूशन

आपको विज्ञापन एट्रिब्यूशन दिखाना होगा, ताकि यह पता चल सके कि व्यू कोई विज्ञापन है. इस बारे में ज़्यादा जानने के लिए, नीति के दिशा-निर्देशों को पढ़ें.

कोड का उदाहरण

नेटिव विज्ञापन दिखाने का तरीका यहां बताया गया है:

  1. NativeAdView क्लास का इंस्टेंस बनाएं.
  2. दिखाई जाने वाली हर विज्ञापन ऐसेट के लिए:

    1. विज्ञापन ऑब्जेक्ट में मौजूद ऐसेट की मदद से, ऐसेट व्यू को पॉप्युलेट करें.
    2. ऐसेट व्यू को NativeAdView क्लास के साथ रजिस्टर करें.
  3. अगर आपके नेटिव विज्ञापन लेआउट में बड़ी मीडिया ऐसेट शामिल है, तो MediaView को रजिस्टर करें.

  4. विज्ञापन ऑब्जेक्ट को NativeAdView क्लास के साथ रजिस्टर करें.

यहां एक उदाहरण फ़ंक्शन दिया गया है, जो NativeAd दिखाता है:

Java

private void displayNativeAd(ViewGroup parent, NativeAd ad) {

  // Inflate a layout and add it to the parent ViewGroup.
  LayoutInflater inflater = (LayoutInflater) parent.getContext()
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  NativeAdView adView = (NativeAdView) inflater
          .inflate(R.layout.ad_layout_file, parent);

  // Locate the view that will hold the headline, set its text, and call the
  // NativeAdView's setHeadlineView method to register it.
  TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline);
  headlineView.setText(ad.getHeadline());
  adView.setHeadlineView(headlineView);

  // Repeat the process for the other assets in the NativeAd
  // using additional view objects (Buttons, ImageViews, etc).

  // If the app is using a MediaView, it should be
  // instantiated and passed to setMediaView. This view is a little different
  // in that the asset is populated automatically, so there's one less step.
  MediaView mediaView = (MediaView) adView.findViewById(R.id.ad_media);
  adView.setMediaView(mediaView);

  // Call the NativeAdView's setNativeAd method to register the
  // NativeAdObject.
  adView.setNativeAd(ad);

  // Ensure that the parent view doesn't already contain an ad view.
  parent.removeAllViews();

  // Place the AdView into the parent.
  parent.addView(adView);
}

Kotlin

fun displayNativeAd(parent: ViewGroup, ad: NativeAd) {

  // Inflate a layout and add it to the parent ViewGroup.
  val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
          as LayoutInflater
  val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView

  // Locate the view that will hold the headline, set its text, and use the
  // NativeAdView's headlineView property to register it.
  val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
  headlineView.text = ad.headline
  adView.headlineView = headlineView

  // Repeat the process for the other assets in the NativeAd using
  // additional view objects (Buttons, ImageViews, etc).

  val mediaView = adView.findViewById<MediaView>(R.id.ad_media)
  adView.mediaView = mediaView

  // Call the NativeAdView's setNativeAd method to register the
  // NativeAdObject.
  adView.setNativeAd(ad)

  // Ensure that the parent view doesn't already contain an ad view.
  parent.removeAllViews()

  // Place the AdView into the parent.
  parent.addView(adView)
}

Jetpack Compose

@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
  val context = LocalContext.current
  Box(modifier = Modifier.padding(8.dp).wrapContentHeight(Alignment.Top)) {
    // Call the NativeAdView composable to display the native ad.
    NativeAdView {
      // Inside the NativeAdView composable, display the native ad assets.
      Column(Modifier.align(Alignment.TopStart).wrapContentHeight(Alignment.Top)) {
        // Display the ad attribution.
        NativeAdAttribution(text = context.getString(R.string.attribution))
        Row {
          // If available, display the icon asset.
          nativeAd.icon?.let { icon ->
            NativeAdIconView(Modifier.padding(5.dp)) {
              icon.drawable?.toBitmap()?.let { bitmap ->
                Image(bitmap = bitmap.asImageBitmap(), "Icon")
              }
            }
          }
          Column {
            // If available, display the headline asset.
            nativeAd.headline?.let {
              NativeAdHeadlineView {
                Text(text = it, style = MaterialTheme.typography.headlineLarge)
              }
            }
            // If available, display the star rating asset.
            nativeAd.starRating?.let {
              NativeAdStarRatingView {
                Text(text = "Rated $it", style = MaterialTheme.typography.labelMedium)
              }
            }
          }
        }

        // If available, display the body asset.
        nativeAd.body?.let { NativeAdBodyView { Text(text = it) } }
        // Display the media asset.
        NativeAdMediaView(Modifier.fillMaxWidth().height(500.dp).fillMaxHeight())

        Row(Modifier.align(Alignment.End).padding(5.dp)) {
          // If available, display the price asset.
          nativeAd.price?.let {
            NativeAdPriceView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
              Text(text = it)
            }
          }
          // If available, display the store asset.
          nativeAd.store?.let {
            NativeAdStoreView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
              Text(text = it)
            }
          }
          // If available, display the call to action asset.
          // Note: The Jetpack Compose button implements a click handler which overrides the native
          // ad click handler, causing issues. Use the NativeAdButton which does not implement a
          // click handler. To handle native ad clicks, use the NativeAd AdListener onAdClicked
          // callback.
          nativeAd.callToAction?.let { callToAction ->
            NativeAdCallToActionView(Modifier.padding(5.dp)) { NativeAdButton(text = callToAction) }
          }
        }
      }
    }
  }
}

यहां अलग-अलग टास्क दिए गए हैं:

  1. लेआउट को बड़ा करना

    Java

    LayoutInflater inflater = (LayoutInflater) parent.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    NativeAdView adView = (NativeAdView) inflater
            .inflate(R.layout.ad_layout_file, parent);
    

    Kotlin

    val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
            as LayoutInflater
    val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView
    

    यह कोड, ऐसे एक्सएमएल लेआउट को इनफ़्लेट कर रहा है जिसमें नेटिव विज्ञापन दिखाने के लिए व्यू शामिल हैं. इसके बाद, NativeAdView का रेफ़रंस ढूंढ रहा है. ध्यान दें कि अगर आपके फ़्रैगमेंट या गतिविधि में कोई NativeAdView मौजूद है, तो आपके पास किसी मौजूदा NativeAdView का फिर से इस्तेमाल करने का विकल्प भी है. इसके अलावा, लेआउट फ़ाइल का इस्तेमाल किए बिना भी डाइनैमिक तौर पर कोई इंस्टेंस बनाया जा सकता है.

  2. ऐसेट व्यू को पॉप्युलेट और रजिस्टर करना

    यह सैंपल कोड, हेडलाइन दिखाने के लिए इस्तेमाल किए गए व्यू का पता लगाता है. साथ ही, विज्ञापन ऑब्जेक्ट की दी गई स्ट्रिंग ऐसेट का इस्तेमाल करके, उसका टेक्स्ट सेट करता है और उसे NativeAdView ऑब्जेक्ट के साथ रजिस्टर करता है:

    Java

    TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline);
    headlineView.setText(ad.getHeadline());
    adView.setHeadlineView(headlineView);
    

    Kotlin

    val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
    headlineView.text = ad.headline
    adView.headlineView = headlineView
    

    व्यू को ढूंढने, उसकी वैल्यू सेट करने, और विज्ञापन व्यू क्लास के साथ रजिस्टर करने की इस प्रोसेस को, नेटिव विज्ञापन ऑब्जेक्ट की दी गई हर उस एसेट के लिए दोहराया जाना चाहिए जिसे ऐप्लिकेशन दिखाएगा.

  3. क्लिक मैनेज करना

    नेटिव विज्ञापन व्यू के ऊपर या उसमें मौजूद किसी भी व्यू पर, कस्टम क्लिक हैंडलर लागू न करें. विज्ञापन व्यू ऐसेट पर होने वाले क्लिक को एसडीके टूल मैनेज करता है. ऐसा तब तक होता है, जब तक आपने ऐसेट व्यू को सही तरीके से भरकर रजिस्टर किया हो. इस बारे में पिछले सेक्शन में बताया गया है.

    क्लिक सुनने के लिए, Google Mobile Ads SDK का क्लिक कॉलबैक लागू करें:

    Java

    AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110")
        // ...
        .withAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(LoadAdError adError) {
                // Handle the failure by logging.
            }
            @Override
            public void onAdClicked() {
                // Log the click event or other custom behavior.
            }
        })
        .build();
    

    Kotlin

    val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
        // ...
        .withAdListener(object : AdListener() {
            override fun onAdFailedToLoad(adError: LoadAdError) {
                // Handle the failure.
            }
            override fun onAdClicked() {
                // Log the click event or other custom behavior.
            }
        })
        .build()
    
  4. MediaView को रजिस्टर करना

    अगर आपको अपने नेटिव विज्ञापन के लेआउट में मुख्य इमेज एसेट शामिल करनी है, तो आपको ImageView एसेट के बजाय MediaView एसेट का इस्तेमाल करना होगा.

    MediaView एक खास View है, जिसे मुख्य मीडिया ऐसेट, यानी वीडियो या इमेज दिखाने के लिए डिज़ाइन किया गया है.

    MediaView को एक्सएमएल लेआउट में तय किया जा सकता है या डाइनैमिक तौर पर बनाया जा सकता है. इसे किसी अन्य ऐसेट व्यू की तरह ही, NativeAdView की व्यू हैरारकी में रखा जाना चाहिए. MediaView का इस्तेमाल करने वाले ऐप्लिकेशन को, इसे NativeAdView के साथ रजिस्टर करना होगा:

    Java

     // Populate and register the media asset view.
     nativeAdView.setMediaView(nativeAdBinding.adMedia);
    

    Kotlin

     // Populate and register the media asset view.
     nativeAdView.mediaView = nativeAdBinding.adMedia
    

    ImageScaleType

    इमेज दिखाते समय, MediaView क्लास में ImageScaleType प्रॉपर्टी होती है. अगर आपको MediaView में इमेज को स्केल करने का तरीका बदलना है, तो MediaView के setImageScaleType() तरीके का इस्तेमाल करके, उससे जुड़ा ImageView.ScaleType सेट करें:

    Java

    mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
    

    Kotlin

    mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
    

    MediaContent

    MediaContent क्लास में, नेटिव विज्ञापन के मीडिया कॉन्टेंट से जुड़ा डेटा होता है. इसे MediaView क्लास का इस्तेमाल करके दिखाया जाता है. जब MediaView mediaContent प्रॉपर्टी को MediaContent इंस्टेंस के साथ सेट किया जाता है, तो:

    • अगर कोई वीडियो एसेट उपलब्ध है, तो उसे बफ़र किया जाता है और वह MediaView में चलने लगता है. hasVideoContent() देखकर पता लगाया जा सकता है कि कोई वीडियो ऐसेट उपलब्ध है या नहीं.

    • अगर विज्ञापन में कोई वीडियो ऐसेट नहीं है, तो mainImage ऐसेट को डाउनलोड किया जाता है और MediaView के अंदर डाला जाता है.

    डिफ़ॉल्ट रूप से, mainImage पहली डाउनलोड की गई इमेज ऐसेट होती है. अगर setReturnUrlsForImageAssets(true) का इस्तेमाल किया जाता है, तो mainImage की वैल्यू null होती है. साथ ही, आपको मैन्युअल तरीके से डाउनलोड की गई इमेज के लिए, mainImage प्रॉपर्टी को सेट करना होगा. ध्यान दें कि इस इमेज का इस्तेमाल सिर्फ़ तब किया जाएगा, जब कोई वीडियो एसेट उपलब्ध न हो.

  5. नेटिव विज्ञापन ऑब्जेक्ट रजिस्टर करना

    इस आखिरी चरण में, नेटिव विज्ञापन ऑब्जेक्ट को उस व्यू के साथ रजिस्टर किया जाता है जो उसे दिखाने के लिए ज़िम्मेदार होता है.

    Java

    adView.setNativeAd(ad);
    

    Kotlin

    adView.setNativeAd(ad)
    

विज्ञापन मिटाना

नेटिव विज्ञापन दिखाने के बाद, उसे मिटा देना चाहिए, ताकि विज्ञापन को सही तरीके से कचरे के तौर पर इकट्ठा किया जा सके.

Java

nativeAd.destroy();

Kotlin

nativeAd.destroy()

GitHub पर मौजूद उदाहरण

नेटिव विज्ञापनों के उदाहरण को लागू करने का पूरा तरीका:

Java Kotlin JetpackCompose

अगले चरण

इन विषयों के बारे में जानें: