La función Mantener oculto este anuncio brinda a los usuarios la posibilidad de cerrar los anuncios o dejar de verlos, e indicar cuáles no les interesan. Este es el aspecto de la versión predeterminada (no personalizada):
Con UnifiedNativeAd
, puedes implementar tu propia interfaz para permitir que los usuarios oculten los anuncios nativos. A continuación, te mostramos el procedimiento que debes seguir.
Solicita la versión personalizada de Mantener oculto este anuncio
Primero, debes habilitar la función personalizada para ocultar anuncios mediante el objeto setRequestCustomMuteThisAd
de la clase NativeAdOptions.Builder
a la hora de realizar solicitudes de anuncio:
Java
AdLoader adLoader = new AdLoader.Builder(context, "ad unit ID") .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() { @Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { // Show the ad. } }) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(int errorCode) { // Handle the failure by logging, altering the UI, and so on. } }) .withNativeAdOptions(new NativeAdOptions.Builder() .setRequestCustomMuteThisAd(true) .build()) .build(); adLoader.loadAd(new AdRequest.Builder().build());
Kotlin
val adLoader = AdLoader.Builder(context, "ad unit ID") .forUnifiedNativeAd { ad -> // Show the ad } .withAdListener(object : AdListener() { override fun onAdFailedToLoad(errorCode: Int) { // Handle the failure by logging, altering the UI, and so on. } }) .withNativeAdOptions(NativeAdOptions.Builder() .setRequestCustomMuteThisAd(true) .build()) .build() adLoader.loadAd(AdRequest.Builder().build())
Comprueba si es posible personalizar Mantener oculto este anuncio
Cuando se haya cargado el anuncio nativo, comprueba el valor que ha devuelto el método isCustomMuteThisAdEnabled()
en el objeto UnifiedNativeAd
. Si es "true", añade el botón o gesto personalizado para ocultar anuncios y configura su interfaz mediante el elemento MuteThisAdReason
del método getMuteThisAdReasons()
que, a su vez, pertenece al objeto UnifiedNativeAd
.
Java
// In OnUnifiedNativeAdLoadedListener @Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { // Show the ad. ... this.nativeAdView.setNativeAd(unifiedNativeAd); if (nativeAd.isCustomMuteThisAdEnabled()) { enableCustomMuteWithReasons(nativeAd.getMuteThisAdReasons()); } else { hideCustomMute(); } } ... private void enableCustomMuteWithReasons(List<MuteThisAdReason> reasons) { //TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes // the ad. } private void hideCustomMute() { //TODO: Remove / hide the custom mute button from your user interface. }
Kotlin
// When constructing the ad loader. builder.forUnifiedNativeAd { unifiedNativeAd -> // Show the ad. ... this.nativeAdView.nativeAd = nativeAd if (nativeAd.isCustomMuteThisAdEnabled) { enableCustomMuteWithReasons(nativeAd.muteThisAdReasons); } else { hideCustomMute(); } } ... private fun enableCustomMuteWithReasons(reasons: List<MuteThisAdReason>) { //TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes // the ad. } private fun hideCustomMute() { //TODO: Remove / hide the custom mute button from your user interface. }
Tú eres quien decide cómo implementar la interfaz personalizada para ocultar anuncios. Puedes colocar un pequeño botón de cierre en el anuncio o permitir que los usuarios lo oculten de otra manera.
Muestra los motivos para ocultar anuncios
Si se puede personalizar la opción Mantener oculto este anuncio, habrá una serie de objetos MuteThisAdReason
disponibles en la propiedad muteThisAdReasons
de UnifiedNativeAd
.
MuteThisAdReason
tiene un método getDescription()
que proporciona una cadena que puede mostrarse.
Te recomendamos mostrar los motivos a los usuarios y permitirles que indiquen por qué quieren ocultar el anuncio. Cuando hagan clic en uno de los motivos, deberías registrar el anuncio que se ha ocultado junto con el motivo seleccionado.
Si lo prefieres, puedes optar por no mostrar los motivos cuando el usuario haga clic en el botón para cerrar el anuncio y registrar la acción de ocultar el anuncio directamente, sin acompañarla de un motivo.
Oculta el anuncio
El proceso para ocultar un anuncio consta de dos pasos:
Registra el motivo por el que se ha ocultado el anuncio nativo mediante el método
muteThisAd
deUnifiedNativeAd
.Oculta el anuncio en tu interfaz de la forma que prefieras:
Java
private void muteAdDialogDidSelectReason(MuteThisAdReason reason) { // Report the mute action and reason to the ad. nativeAd.muteThisAd(reason); muteAd(); } private void muteAd { //TODO: Mute / hide the ad in your preferred manner. }
Kotlin
private fun muteAdDialogDidSelectReason(reason: MuteThisAdReason) { // Report the mute action and reason to the ad. nativeAd.muteThisAd(reason) muteAd() } private fun muteAd() { //TODO: Mute / hide the ad in your preferred manner. }
Recibe confirmación de que el anuncio se ha ocultado (opcional)
Si quieres recibir una notificación cuando el anuncio se oculte correctamente, puedes implementar el objeto MuteThisAdListener
, que se configura mediante el método setMuteThisAdListener
de UnifiedNativeAd
. Solo se llamará al método onAdMuted()
si el anuncio se ha ocultado correctamente.
Java
nativeAd.setMuteThisAdListener(new MuteThisAdListener() { @Override public void onAdMuted() { Toast.makeText(getActivity(), "Ad muted", Toast.LENGTH_SHORT).show(); } });
Kotlin
nativeAd.setMuteThisAdListener { Toast.makeText(activity, "Ad muted", Toast.LENGTH_SHORT).show() }