Migrer les annonces natives

Cette page compare les implémentations d'annonces natives entre le SDK Mobile Ads actuel et celui de nouvelle génération.

Charger une annonce

Le tableau suivant compare les principales différences d'implémentation entre les SDK actuels et de nouvelle génération pour les annonces mobiles lors du chargement d'une annonce native :

Objectif d'implémentation Actuel Nouvelle génération
Spécifier les types d'annonces natives Grâce à des méthodes uniques sur AdLoader. Dans la demande d'annonce.
Gérer le rappel de réussite du chargement des annonces Chaque type d'annonce native possède un rappel distinct. Une seule interface NativeAdLoaderCallback gère le chargement des annonces pour tous les types d'annonces natives.
Gérer le rappel d'échec du chargement des annonces dans un écouteur distinct. Dans NativeAdLoaderCallback.

Charger une annonce native

Les exemples suivants chargent une annonce native dans les SDK Mobile Ads actuels et de nouvelle génération :

Actuel

Kotlin

val adLoader =
  AdLoader.Builder(this, AD_UNIT_ID)
    .forNativeAd(object : NativeAd.OnNativeAdLoadedListener {
      override fun onNativeAdLoaded(nativeAd: NativeAd) {
        // Native ad loaded.
      }
    })
    .withAdListener(
      object : AdListener() {
        override fun onAdFailedToLoad(loadAdError: LoadAdError) {
          // Native ad failed to load.
        }
      }
    )
    .build()

adLoader.loadAd(AdRequest.Builder().build())

Java

AdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID)
    .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
      @Override
      public void onNativeAdLoaded(NativeAd nativeAd) {
        // Native ad loaded.
      }
    })
    .withAdListener(new AdListener() {
      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Native ad failed to load.
      }
    })
    .build();

adLoader.loadAd(new AdRequest.Builder().build());
Nouvelle génération

Kotlin

NativeAdLoader.load(
  NativeAdRequest.Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.NATIVE)).build(),
  object : NativeAdLoaderCallback {
    override fun onNativeAdLoaded(nativeAd: NativeAd) {
      // Native ad loaded.
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      // Native ad failed to load.
    }
  }
)

Java

NativeAdLoader.load(
  new NativeAdRequest.Builder(AD_UNIT_ID, List.of(NativeAd.NativeAdType.NATIVE)).build(),
  new NativeAdLoaderCallback() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
      // Native ad loaded.
    }

    @Override
    public void onAdFailedToLoad(LoadAdError adError) {
      // Native ad failed to load.
    }
  }
);

Charger une annonce native personnalisée

Les exemples suivants chargent une annonce native personnalisée dans les SDK Mobile Ads actuels et de nouvelle génération :

Actuel

Kotlin

val adLoader =
  AdLoader.Builder(this, AD_UNIT_ID)
    .forCustomFormatAd(CUSTOM_FORMAT_ID,
      object: NativeCustomFormatAd.OnCustomFormatAdLoadedListener {
        override fun onCustomFormatAdLoaded(nativeCustomFormatAd: NativeCustomFormatAd) {
          // Custom native ad loaded.
        }
      },
      object: NativeCustomFormatAd.OnCustomClickListener {
        override fun onCustomClick(
          nativeCustomFormatAd: NativeCustomFormatAd,
          assetName: String
        ) {
          // Custom native ad recorded a click.
        }
      })
    .withAdListener(
      object : AdListener() {
        override fun onAdFailedToLoad(loadAdError: LoadAdError) {
          // Custom native ad failed to load.
        }
      }
    )
    .build()

adLoader.loadAd(AdRequest.Builder().build())

Java

AdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID)
    .forCustomFormatAd(CUSTOM_FORMAT_ID,
        new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() {
          @Override
          public void onCustomFormatAdLoaded(NativeCustomFormatAd nativeCustomFormatAd) {
            // Custom native ad loaded.
          }
        },
        new NativeCustomFormatAd.OnCustomClickListener() {
          @Override
          public void onCustomClick(NativeCustomFormatAd nativeCustomFormatAd, String assetName) {
            // Custom native ad recorded a click.
          }
        })
    .withAdListener(new AdListener() {
      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Custom native ad failed to load.
      }
    })
    .build();

adLoader.loadAd(new AdRequest.Builder().build());
Nouvelle génération

Kotlin

NativeAdLoader.load(
  NativeAdRequest
    .Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.CUSTOM_NATIVE))
    .setCustomFormatIds(listOf(CUSTOM_FORMAT_ID))
    .build(),
  object : NativeAdLoaderCallback {
    override fun onCustomNativeAdLoaded(customNativeAd: CustomNativeAd) {
      // Custom native ad loaded.
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      // Custom native ad failed to load.
    }
  }
)

Java

NativeAdLoader.load(
    new NativeAdRequest.Builder(AD_UNIT_ID, List.of(NativeAdType.CUSTOM_NATIVE))
        .setCustomFormatIds(List.of(CUSTOM_FORMAT_ID))
        .build(),
    new NativeAdLoaderCallback() {
      @Override
      public void onCustomNativeAdLoaded(CustomNativeAd customNativeAd) {
        // Custom native ad loaded.
      }

      @Override
      public void onAdFailedToLoad(LoadAdError adError) {
        // Custom native ad failed to load.
      }
    }
);

Définir les options des annonces natives

Les exemples suivants définissent les options d'annonces natives dans les SDK Mobile Ads actuels et de nouvelle génération :

Actuel

Dans le SDK Mobile Ads actuel, définissez les options d'annonce native sur AdLoader.Builder.

Kotlin

val videoOptions = VideoOptions.Builder().setStartMuted(true).build()

val adLoader =
  AdLoader.Builder(this, AD_UNIT_ID)
    .withNativeAdOptions(NativeAdOptions.Builder().setVideoOptions(videoOptions).build())
    .build()

Java

VideoOptions videoOptions = new VideoOptions.Builder()
    .setStartMuted(true)
    .build();

AdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID)
    .withNativeAdOptions(new NativeAdOptions.Builder()
        .setVideoOptions(videoOptions)
        .build())
    .build();
Nouvelle génération

Dans le SDK Next Gen Mobile Ads, définissez les options d'annonce native sur NativeAdRequest.Builder.

Kotlin

val videoOptions = VideoOptions.Builder().setStartMuted(true).build()

val nativeAdRequest = NativeAdRequest
  .Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.NATIVE))
  .setVideoOptions(videoOptions)
  .build()

Java

VideoOptions videoOptions = new VideoOptions.Builder().setStartMuted(true).build();

NativeAdRequest nativeAdRequest = new NativeAdRequest
    .Builder(AD_UNIT_ID, List.of(NativeAd.NativeAdType.NATIVE))
    .setVideoOptions(videoOptions)
    .build();

Définir des rappels d'événements d'annonces natives

Les exemples suivants définissent les rappels d'événements d'annonces natives dans les SDK Mobile Ads actuels et de nouvelle génération :

Actuel

Les rappels d'événements d'annonces natives doivent être enregistrés avant le chargement de l'annonce.

Kotlin

val adLoader =
  AdLoader.Builder(this, AD_UNIT_ID)
    .forNativeAd(object : NativeAd.OnNativeAdLoadedListener {
      override fun onNativeAdLoaded(nativeAd: NativeAd) {
        // Native ad loaded.
      }
    })
    .withAdListener(
      object : AdListener() {
        override fun onAdOpened() {
          // Native ad opened an overlay that covered the screen.
        }

        override fun onAdClosed() {
          // Native ad closed.
        }

        override fun onAdImpression() {
          // Native ad recorded an impression.
        }

        override fun onAdClicked() {
          // Native ad recorded a click.
        }
      }
    )
    .build()

adLoader.loadAd(AdRequest.Builder().build())

Java

AdLoader adLoader = new AdLoader.Builder(this, AD_UNIT_ID)
    .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
      @Override
      public void onNativeAdLoaded(NativeAd nativeAd) {
        // Native ad loaded.
      }
    })
    .withAdListener(new AdListener() {
      @Override
      public void onAdOpened() {
        // Native ad opened an overlay that covered the screen.
      }

      @Override
      public void onAdClosed() {
        // Native ad closed.
      }

      @Override
      public void onAdImpression() {
        // Native ad recorded an impression.
      }

      @Override
      public void onAdClicked() {
        // Native ad recorded a click.
      }
    })
    .build();

adLoader.loadAd(new AdRequest.Builder().build());
Nouvelle génération

Le SDK Next Gen Mobile Ads permet d'enregistrer des rappels d'événements d'annonces une fois l'annonce native chargée.

Kotlin

NativeAdLoader.load(
  NativeAdRequest
    .Builder(AD_UNIT_ID, listOf(NativeAd.NativeAdType.NATIVE))
    .build(),
  object : NativeAdLoaderCallback {
    override fun onNativeAdLoaded(nativeAd: NativeAd) {
      // Native ad loaded.
      nativeAd.adEventCallback =
        object : NativeAdEventCallback {
          override fun onAdShowedFullScreenContent() {
            // Native ad showed full screen content.
            // Current SDK equivalent: onAdOpened()
          }

          override fun onAdDismissedFullScreenContent() {
            // Native ad dismissed full screen content.
            // Current SDK equivalent: onAdClosed()
          }

          override fun onAdFailedToShowFullScreenContent(
            fullScreenContentError: FullScreenContentError
          ) {
            // Native ad failed to show full screen content.
            // Current SDK equivalent: N/A
          }

          override fun onAdImpression() {
            // Native ad recorded an impression.
          }

          override fun onAdClicked() {
            // Native ad recorded a click.
          }
        }
    }
  }
)

Java

NativeAdLoader.load(
    new NativeAdRequest.Builder(AD_UNIT_ID, List.of(NativeAd.NativeAdType.NATIVE))
        .build(),
    new NativeAdLoaderCallback() {
      @Override
      public void onNativeAdLoaded(NativeAd nativeAd) {
        // Native ad loaded.
        nativeAd.setAdEventCallback(new NativeAdEventCallback() {
          @Override
          public void onAdShowedFullScreenContent() {
            // Native ad showed full screen content.
            // Current SDK equivalent: onAdOpened()
          }

          @Override
          public void onAdDismissedFullScreenContent() {
            // Native ad dismissed full screen content.
            // Current SDK equivalent: onAdClosed()
          }

          @Override
          public void onAdFailedToShowFullScreenContent(FullScreenContentError fullScreenContentError) {
            // Native ad failed to show full screen content.
            // Current SDK equivalent: N/A
          }

          @Override
          public void onAdImpression() {
            // Native ad recorded an impression.
          }

          @Override
          public void onAdClicked() {
            // Native ad recorded a click.
          }
        });
      }
    }
);

Diffuser une annonce

Cette section décrit les différences d'affichage d'une annonce native entre les SDK Mobile Ads actuels et de nouvelle génération.

Utiliser un nom de package SDK nouvelle génération

Lorsque vous créez un NativeAdView en XML, utilisez le nom du package SDK nouvelle génération :

Actuel

<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">
    <!-- Layout assets such as the media view and call to action. -->
</com.google.android.gms.ads.nativead.NativeAdView>
Nouvelle génération

<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">
    <!-- Layout assets such as the media view and call to action. -->
</com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView>

Enregistrez le composant de contenu multimédia avec NativeAdView.

Les exemples suivants enregistrent le composant de contenu multimédia avec NativeAdView dans les SDK Mobile Ads actuels et de nouvelle génération :

Actuel

Le SDK Mobile Ads actuel s'attend à ce que la vue du contenu multimédia soit enregistrée avec la vue de l'annonce native avant l'enregistrement de l'annonce native. Toutefois, l'API n'applique pas ce comportement.

Kotlin

private fun displayNativeAd(nativeAd: NativeAd) {
  // Inflate the NativeAdView layout.
  val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)

  // Add the NativeAdView to the view hierarchy.
  binding.nativeViewContainer.addView(nativeAdBinding.root)
  val nativeAdView = nativeAdBinding.root

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

  // Register the native ad with the NativeAdView.
  nativeAdView.setNativeAd(nativeAd)
}

Java

private void displayNativeAd(NativeAd nativeAd) {
  // Inflate the NativeAdView layout
  NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());

  // Add the NativeAdView to the view hierarchy
  binding.nativeViewContainer.addView(nativeAdBinding.getRoot());
  NativeAdView nativeAdView = nativeAdBinding.getRoot();

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

  // Register the native ad with the NativeAdView
  nativeAdView.setNativeAd(nativeAd);
}
Nouvelle génération

Le SDK Next Gen Mobile Ads impose l'enregistrement de la vue du contenu multimédia avec la vue de l'annonce native en même temps que l'annonce native.

Kotlin

private fun displayNativeAd(nativeAd: NativeAd) {
  // Inflate the NativeAdView layout.
  val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)

  // Add the NativeAdView to the view hierarchy.
  binding.nativeViewContainer.addView(nativeAdBinding.root)
  val nativeAdView = nativeAdBinding.root

  // Populate and register the asset views.
  // ...

  // Register the native ad and media content asset with the NativeAdView.
  val mediaView = nativeAdBinding.adMedia
  nativeAdView.registerNativeAd(nativeAd, mediaView)
}

Java

private void displayNativeAd(NativeAd nativeAd) {
  // Inflate the NativeAdView layout.
  NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());

  // Add the NativeAdView to the view hierarchy.
  binding.nativeViewContainer.addView(nativeAdBinding.getRoot());
  NativeAdView nativeAdView = nativeAdBinding.getRoot();

  // Populate and register the asset views.
  // ...

  // Register the native ad and media content asset with the NativeAdView.
  MediaView mediaView = nativeAdBinding.adMedia;
  nativeAdView.registerNativeAd(nativeAd, mediaView);
}