借助“不再显示该广告”功能,用户可以关闭广告或停止显示广告,还可以标示出他们不感兴趣的广告。默认的非自定义版本如下所示:
借助 UnifiedNativeAd
,您可以设置自己的界面,让用户能够选择不再显示原生广告。具体操作步骤如下。
请求自定义版“不再显示该广告”
首先,在发出广告请求时,使用 setRequestCustomMuteThisAd
(位于 NativeAdOptions.Builder
类上)启用自定义版“不再显示该广告”功能:
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())
检查自定义版“不再显示该广告”是否可用
加载原生广告后,检查 UnifiedNativeAd
对象上的 isCustomMuteThisAdEnabled()
方法返回的值。如果该值为 true,则添加自定义版“不再显示”按钮/手势,并使用 UnifiedNativeAd
对象上的 getMuteThisAdReasons()
方法提供的 MuteThisAdReason
配置自定义版“不再显示”界面。
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. }
如何设定自定义版不再显示界面完全由您决定。您可以在广告上放置一个小小的关闭按钮,也可以提供其他界面供用户选择不再显示该广告。
显示“不再显示该广告”的原因
如果自定义版“不再显示该广告”可供使用,则 UnifiedNativeAd
的 muteThisAdReasons
属性中将提供一组 MuteThisAdReason
对象。MuteThisAdReason
具有一个提供可显示字符串的 getDescription()
方法。
我们建议您最好向用户显示这些原因,并让他们选择不再显示该广告的原因。当用户点击其中一个原因时,您应该报告该广告不再显示并附上用户所选原因。
当用户点击关闭按钮时,您也可以选择不显示这些原因,而是直接报告用户执行了不再显示操作,但不提供具体原因。
不再显示该广告
要启用“不再显示该广告”,应执行以下两项操作:
使用
UnifiedNativeAd
中的muteThisAd
方法报告不再显示原生广告的原因。在您的界面上,按照自己喜欢的方式不再显示/隐藏相应广告:
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. }
接收不再显示广告的确认通知(可选)
如果您希望在成功报告广告不再显示后收到确认通知,则可实现 MuteThisAdListener
,该对象是通过 UnifiedNativeAd
中的 setMuteThisAdListener
方法设置的。只有在成功实现了不再显示相应广告后,才会调用 onAdMuted()
方法。
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() }