Custom Mute This Ad for Native Ads

The "Mute This Ad" feature provides users with the ability to close or to stop seeing ads and signal which ads aren't interesting to them. Here's what the default, non-custom version looks like:

non-customized mute this ad

With UnifiedNativeAd, you have the option to implement your own UI to enable a user to mute the native ad. Here's how it's done.

Request custom Mute This Ad

The first step is to enable the custom Mute This Ad feature using setRequestCustomMuteThisAd on the NativeAdOptions.Builder class when making an ad request:

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())

Check if custom Mute This Ad is available

Once the native ad is loaded, check the value returned from the isCustomMuteThisAdEnabled() method on the UnifiedNativeAd object. If it's true, add your custom mute button / gesture and configure your custom mute interface with the MuteThisAdReasons available from the getMuteThisAdReasons() method on the UnifiedNativeAd object.

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.
}

The implementation of the custom mute interface is entirely up to you. You can place a small close button on the ad, or you can provide some other interface for the user to opt to mute the ad.

Show the Mute This Ad reasons

If custom Mute This Ad is available, there is an array of MuteThisAdReason objects available in the muteThisAdReasons property on the UnifiedNativeAd. MuteThisAdReason has a getDescription() method which provides a displayable string.

As a best practice, we recommend displaying these reasons to the user and allowing them to select their reason for muting the ad. When the user clicks on one of the reasons, you should report the ad mute with the selected reason.

You may also choose not to display these reasons when the user clicks the close button, and report the mute action directly with no reason.

Mute the ad

Muting the ad should involve two actions:

  1. Report the reason for the mute to the native ad using the muteThisAd method on UnifiedNativeAd.

  2. On your UI, mute / hide the ad yourself in your preferred manner:

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.
}

Receive confirmation of ad mute (optional)

If you want to be notified that reporting the ad mute was successful, you can implement the MuteThisAdListener, which is set using the setMuteThisAdListener method on the UnifiedNativeAd. The onAdMuted() method will be called only if the ad was muted successfully.

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()
}

傳送您對下列選項的寶貴意見...

這個網頁
需要協助嗎?請前往我們的支援網頁