Tính năng "Ẩn quảng cáo này" cho phép người dùng đóng hoặc ngừng xem quảng cáo để cho biết những quảng cáo nào không thu hút họ. Dưới đây là giao diện của phiên bản mặc định, không tùy chỉnh:
Thông qua UnifiedNativeAd
, bạn có thể triển khai giao diện người dùng của riêng mình để cho phép
người dùng ẩn quảng cáo gốc. Dưới đây là cách thực hiện.
Yêu cầu tính năng tùy chỉnh Ẩn quảng cáo này
Bước đầu tiên là bật tính năng tùy chỉnh Ẩn quảng cáo này bằng cách sử dụng
setRequestCustomMuteThisAd
trên lớp NativeAdOptions.Builder
khi tạo
yêu cầu quảng cáo:
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())
Kiểm tra xem liệu bạn có thể sử dụng tính năng tùy chỉnh Ẩn quảng cáo này hay không
Sau khi tải quảng cáo gốc, hãy kiểm tra giá trị được trả về từ phương thức
isCustomMuteThisAdEnabled()
trên đối tượng UnifiedNativeAd
. Nếu giá trị là true, hãy thêm nút/cử chỉ ẩn
tùy chỉnh và định cấu hình giao diện ẩn tùy chỉnh bằng
MuteThisAdReason
có trong phương thức
getMuteThisAdReasons()
trên đối tượng 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. }
Việc triển khai giao diện ẩn tùy chỉnh hoàn toàn tùy thuộc vào bạn. Bạn có thể đặt một nút đóng nhỏ trên quảng cáo hoặc có thể cung cấp một số giao diện khác để người dùng chọn ẩn quảng cáo.
Hiển thị các lý do Ẩn quảng cáo này
Nếu bạn đã bật tính năng tùy chỉnh Ẩn quảng cáo này, thì Google sẽ hiển thị một loạt các đối tượng MuteThisAdReason
có trong thuộc tính muteThisAdReasons
trên UnifiedNativeAd
.
MuteThisAdReason
có
phương thức getDescription()
. Phương thức này cung cấp một chuỗi
có thể hiển thị.
Cách hay nhất là bạn nên hiển thị các lý do này cho người dùng và cho phép họ chọn lý do ẩn quảng cáo. Khi người dùng nhấp vào một trong các lý do này, bạn nên báo cáo việc ẩn quảng cáo cùng với lý do được chọn.
Bạn cũng có thể chọn không hiển thị các lý do này khi người dùng nhấp vào nút đóng và trực tiếp báo cáo hành động ẩn mà không có lý do.
Ẩn quảng cáo
Để bật tính năng ẩn quảng cáo, bạn cần thực hiện hai hành động sau:
Báo cáo lý do ẩn quảng cáo gốc bằng phương thức
muteThisAd
trênUnifiedNativeAd
.Trên giao diện người dùng, hãy tự ẩn quảng cáo theo cách bạn muốn:
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. }
Nhận thông báo xác nhận đã ẩn quảng cáo (không bắt buộc)
Nếu muốn nhận thông báo xác nhận đã ẩn quảng cáo thành công, bạn có thể
triển khai MuteThisAdListener
bằng phương thức setMuteThisAdListener
trên UnifiedNativeAd
. Phương thức onAdMuted()
sẽ chỉ được gọi nếu
ẩn quảng cáo thành công.
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() }