Tài liệu hướng dẫn này dành cho các nhà xuất bản muốn sử dụng tính năng dàn xếp AdMob để thực hiện những hoạt động sau:
- Hiển thị quảng cáo từ một mạng không được hỗ trợ trực tiếp trong giao diện người dùng AdMob.
- Hiển thị chế độ xem tùy chỉnh thay vì quảng cáo.
Sự kiện tùy chỉnh cho phép bạn viết trình chuyển đổi dàn xếp tùy chỉnh để đặt bất kỳ chế độ xem nào vào không gian quảng cáo. Trong hướng dẫn này, chúng tôi sẽ hướng dẫn bạn cách viết một sự kiện tùy chỉnh để yêu cầu quảng cáo từ một SDK mẫu mà chúng tôi đã phát triển. Bạn có thể tìm nguồn đầy đủ cho sự kiện tùy chỉnh và SDK mẫu tương ứng trên GitHub.
Điều kiện tiên quyết
Trước khi có thể tích hợp các sự kiện tùy chỉnh cho quảng cáo biểu ngữ hoặc quảng cáo xen kẽ, bạn cần tích hợp định dạng quảng cáo đó vào ứng dụng của mình. Dưới đây là các hướng dẫn liên quan:
Sự kiện tùy chỉnh cho quảng cáo biểu ngữ
Trong ví dụ sau, trước tiên bạn sẽ tạo một sự kiện tùy chỉnh cho quảng cáo biểu ngữ trong
tính năng dàn xếp AdMob. Việc này yêu cầu bạn phải xác định một
sự kiện tùy chỉnh trỏ đến lớp cụ thể đó trong ứng dụng của bạn thông qua
giao diện người dùng AdMob, sau đó triển khai
CustomEventBanner
để phân phát lượt xem.
Ví dụ này xác định một sự kiện tùy chỉnh để hiển thị quảng cáo từ
mạng quảng cáo mẫu.
Xác định sự kiện tùy chỉnh
Sự kiện tùy chỉnh phải được xác định trong giao diện người dùng AdMob. Hãy thực hiện theo các hướng dẫn này để tạo một sự kiện tùy chỉnh.Bạn có thể thấy một số tùy chọn cài đặt của sự kiện tùy chỉnh mẫu trong ảnh chụp màn hình dưới đây:
Yêu cầu quảng cáo biểu ngữ
Để yêu cầu quảng cáo biểu ngữ, hãy xác định một lớp triển khai CustomEventBanner
và
gọi lớp này là SampleCustomEventBanner
. Khi sự kiện tùy chỉnh được chọn từ luồng dàn xếp
quảng cáo, phương thức requestBannerAd()
được gọi theo tên lớp mà bạn
đã cung cấp trong các tùy chọn cài đặt.
Thông số không bắt buộc đã xác định ở trên được chuyển vào sự kiện tùy chỉnh của bạn như
một phần của phương thức requestBannerAd()
. Thông thường, thông số này là
giá trị nhận dạng cho phép sự kiện tùy chỉnh xác định quảng cáo nào cần tải.
Bạn cũng nên triển khai các phương thức vòng đời (nếu phù hợp).
Tính năng dàn xếp AdMob chuyển tiếp bộ chuyển đổi
onPause()
và sự kiện hoạt động
onResume()
nếu người dùng gọi phương thức
AdView.pause()
và
AdView.resume()
. Mạng quảng cáo mẫu không bao gồm một lệnh gọi tạm dừng hoặc tiếp tục, do đó,
mạng cung cấp hoạt động triển khai trống. Tính năng dàn xếp cố gắng trong khả năng cho phép để gọi
onDestroy()
khi bộ chuyển đổi sắp bị hủy. Khi đó, bạn nên xóa những gì không cần thiết.
Dưới đây là quy trình triển khai mẫu của SampleCustomEventBanner
:
Java
public class SampleCustomEventBanner implements CustomEventBanner { /** The SampleAdView representing a banner ad. */ private SampleAdView sampleAdView; /** The event is being destroyed. Perform any necessary cleanup here. */ @Override public void onDestroy() { if (sampleAdView != null) { sampleAdView.destroy(); } } /** * The app is being paused. This call is only forwarded to the adapter if * the developer notifies AdMob mediation that * the app is being paused. */ @Override public void onPause() { // The sample ad network doesn't have an onPause method, so does nothing. } /** * The app is being resumed. This call is only forwarded to the * adapter if the developer notifies AdMob * mediation that the app is being resumed. */ @Override public void onResume() { // The sample ad network doesn't have an onResume method, so does nothing. } @Override public void requestBannerAd(Context context, CustomEventBannerListener listener, String serverParameter, AdSize size, MediationAdRequest mediationAdRequest, Bundle customEventExtras) { sampleAdView = new SampleAdView(context); // Assumes that the serverParameter is the AdUnit for the Sample Network. sampleAdView.setAdUnit(serverParameter); sampleAdView.setSize(new SampleAdSize(size.getWidth(), size.getHeight())); // Implement a SampleAdListener and forward callbacks to AdMob. // The callback forwarding is handled by SampleBannerEventFowarder. sampleAdView.setAdListener(new SampleCustomBannerEventForwarder(listener, sampleAdView)); // Make an ad request. sampleAdView.fetchAd(createSampleRequest(mediationAdRequest)); } private SampleAdRequest createSampleRequest(MediationAdRequest mediationAdRequest) { SampleAdRequest request = new SampleAdRequest(); request.setTestMode(mediationAdRequest.isTesting()); request.setKeywords(mediationAdRequest.getKeywords()); return request; } }
Kotlin
class SampleCustomEventBanner : CustomEventBanner { /** The SampleAdView representing a banner ad. */ private lateinit var mSampleAdView: SampleAdView /** The event is being destroyed. Perform any necessary cleanup here. */ override fun onDestroy() { mSampleAdView.destroy() } /** * The app is being paused. This call is only forwarded to the adapter if * the developer notifies AdMob mediation that * the app is being paused. */ override fun onPause() { // The sample ad network doesn't have an onPause method, so does nothing. } /** * The app is being resumed. This call is only forwarded to the * adapter if the developer notifies AdMob * mediation that the app is being resumed. */ override fun onResume() { // The sample ad network doesn't have an onResume method, so does nothing. } override fun requestBannerAd(context: Context, listener: CustomEventBannerListener, serverParameter: String, size: AdSize, mediationAdRequest: MediationAdRequest, customEventExtras: Bundle?) { mSampleAdView = SampleAdView(context) // Assumes that the serverParameter is the AdUnit for the Sample Network. mSampleAdView.adUnit = serverParameter mSampleAdView.size = SampleAdSize(size.width, size.height) // Implement a SampleAdListener and forward callbacks to // AdMob mediation. The callback forwarding // is handled by SampleBannerEventFowarder. mSampleAdView.adListener = SampleCustomBannerEventForwarder(listener, mSampleAdView) // Make an ad request. mSampleAdView.fetchAd(createSampleRequest(mediationAdRequest)) } private fun createSampleRequest(mediationAdRequest: MediationAdRequest): SampleAdRequest { val request = SampleAdRequest() request.testMode = mediationAdRequest.isTesting request.keywords = mediationAdRequest.keywords return request } }
Lớp SampleAdView
được sử dụng trong mẫu là ví dụ đơn giản về chế độ xem quảng cáo của
bên thứ ba từ SDK mẫu.
Gửi thông số bổ sung của mạng quảng cáo cho các yêu cầu sự kiện tùy chỉnh (không bắt buộc)
Nếu bạn cần gửi thêm thông số cho sự kiện tùy chỉnh, hãy sử dụng
phương thức
addCustomEventExtrasBundle()
của lớp AdRequest.Builder
.
Phương thức
Bạn phải nhập lớp bộ chuyển đổi sự kiện tùy chỉnh của bạn và một gói
các thông số bổ sung cần thiết cho bộ chuyển đổi của sự kiện tùy chỉnh đó.
Dưới đây là đoạn mã cho biết cách chuyển thông số SampleExtra
cho lớp
SampleCustomEventBanner
được xác định ở trên:
Bundle extras = new Bundle();
extras.putBoolean("SampleExtra", true);
AdRequest request = new AdRequest.Builder()
.addCustomEventExtrasBundle(SampleCustomEventBanner.class, extras)
.build();
Nếu bạn không gọi addCustomEventExtras()
bằng lớp chính xác của sự kiện tùy chỉnh
và một gói thông số cho yêu cầu sự kiện tùy chỉnh, thì thông số bundle
mà bộ chuyển đổi nhận được sẽ là null
.
Thông báo cho tính năng dàn xếp AdMob
Sự kiện tùy chỉnh của bạn phải thông báo cho tính năng dàn xếp thông qua
giao diện người dùng CustomEventBannerListener
khi quảng cáo tải thành công hoặc không tải được. Nếu không, sự kiện tùy chỉnh
sẽ hết thời gian chờ và tính năng dàn xếp quảng cáo sẽ chuyển sang mạng tiếp theo.
Triển khai trình xử lý quảng cáo cho mạng của bạn và gọi các lệnh gọi lại có liên quan trên
CustomEventBannerListener
để gửi thông báo về lại cho tính năng dàn xếp
AdMob.
SampleCustomBannerEventForwarder
, triển khai giao diện SampleAdListener
, để chuyển tiếp lệnh gọi lại từ
mạng quảng cáo mẫu.
Tính năng dàn xếp AdMob hỗ trợ các lệnh gọi lại sau đây:
Quảng cáo biểu ngữ
Phương thức | Thời điểm gọi |
---|---|
onAdLoaded() |
Yêu cầu quảng cáo biểu ngữ thành công. |
onAdFailedToLoad() |
Yêu cầu quảng cáo biểu ngữ không thành công. |
onAdClicked() |
Quảng cáo biểu ngữ đã được nhấp. |
onAdOpened() |
Quảng cáo biểu ngữ hiển thị chế độ xem toàn màn hình. |
onAdClosed() |
Người dùng quay lại ứng dụng sau khi nhấp vào quảng cáo biểu ngữ. |
onAdLeftApplication() |
Quảng cáo biểu ngữ khiến người dùng rời khỏi ứng dụng. |
Quảng cáo xen kẽ
Phương thức | Thời điểm gọi |
---|---|
onAdLoaded() |
Yêu cầu quảng cáo xen kẽ thành công. |
onAdFailedToLoad() |
Yêu cầu quảng cáo xen kẽ không thành công. |
onAdClicked() |
Đã có người nhấp vào quảng cáo xen kẽ. |
onAdOpened() |
Quảng cáo xen kẽ hiển thị ở chế độ xem toàn màn hình. |
onAdClosed() |
Quảng cáo kẽ đã đóng. |
onAdLeftApplication() |
Quảng cáo xen kẽ khiến người dùng rời khỏi ứng dụng. |
Sau đây là ví dụ về cách triển khai SampleCustomBannerEventForwarder
:
Java
public class SampleCustomBannerEventForwarder extends SampleAdListener { private CustomEventBannerListener mBannerListener; private SampleAdView mAdView; /** * Creates a new {@code SampleBannerEventForwarder}. * @param listener A {@link CustomEventBannerListener} that should receive * forwarded events. * @param adView A {@link SampleAdView}. */ public SampleCustomBannerEventForwarder( CustomEventBannerListener listener, SampleAdView adView) { this.mBannerListener = listener; this.mAdView = adView; } @Override public void onAdFetchSucceeded() { mBannerListener.onAdLoaded(mAdView); } @Override public void onAdFetchFailed(SampleErrorCode errorCode) { switch (errorCode) { case UNKNOWN: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); break; case BAD_REQUEST: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST); break; case NETWORK_ERROR: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR); break; case NO_INVENTORY: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL); break; } } @Override public void onAdFullScreen() { mBannerListener.onAdClicked(); mBannerListener.onAdOpened(); // Only call onAdLeftApplication if your ad network actually exits the developer's app. mBannerListener.onAdLeftApplication(); } @Override public void onAdClosed() { mBannerListener.onAdClosed(); } }
Kotlin
class SampleCustomBannerEventForwarder(private val mBannerListener: CustomEventBannerListener, private val mAdView: SampleAdView) : SampleAdListener() { override fun onAdFetchSucceeded() { mBannerListener.onAdLoaded(mAdView) } override fun onAdFetchFailed(errorCode: SampleErrorCode) { when (errorCode) { UNKNOWN -> mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR) BAD_REQUEST -> mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST) NETWORK_ERROR -> mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR) NO_INVENTORY -> mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL) } } override fun onAdFullScreen() { mBannerListener.onAdClicked() mBannerListener.onAdOpened() // Only call onAdLeftApplication if your ad network actually exits the developer's app. mBannerListener.onAdLeftApplication() } override fun onAdClosed() { mBannerListener.onAdClosed() } }
Sự kiện tùy chỉnh cho quảng cáo xen kẽ
Để triển khai một sự kiện tùy chỉnh cho quảng cáo xen kẽ, trước tiên bạn cần tạo một sự kiện tùy chỉnh cho quảng cáo xen kẽ
trong tính năng dàn xếp AdMob, giống như một sự kiện tùy chỉnh
cho quảng cáo biểu ngữ. Sau đó, hãy triển khai một
CustomEventInterstitial
để cung cấp thông báo. Ví dụ này sử dụng mạng quảng cáo mẫu như trên.
Xác định sự kiện tùy chỉnh
Bạn có thể xác định một sự kiện tùy chỉnh cho quảng cáo xen kẽ thông qua giao diện người dùng AdMob.
Hãy đảm bảo là giá trị mà bạn cung cấp cho Tên lớp có đường dẫn đầy đủ. Thông số phải chứa mọi thông tin cần thiết để tạo yêu cầu quảng cáo cho mạng mà bạn đang triển khai trong sự kiện tùy chỉnh.
Yêu cầu quảng cáo xen kẽ
Để yêu cầu quảng cáo xen kẽ, hãy xác định một lớp triển khai
CustomEventInterstitial
. Lớp này được gọi là SampleCustomEventInterstitial
. Khi sự kiện tùy chỉnh được chọn từ luồng dàn xếp, tính năng dàn xếp sẽ gọi
phương thức requestInterstitialAd()
qua tên lớp mà bạn đã cung cấp trong các tùy chọn cài đặt. Bạn có thể sử dụng
thông số được cung cấp trong phương thức này để gửi yêu cầu quảng cáo xen kẽ đến
mạng mong muốn của bạn. Ví dụ sau cho thấy cách yêu cầu một quảng cáo xen kẽ từ
mạng quảng cáo mẫu thông qua một sự kiện tùy chỉnh:
Sau đây là ví dụ về cách triển khai SampleCustomEventInterstitial
:
Java
public class SampleCustomEventInterstitial implements CustomEventInterstitial { /** Represents a SampleInterstitial. */ private SampleInterstitial sampleInterstitial; @Override public void requestInterstitialAd(Context context, CustomEventInterstitialListener listener, String serverParameter, MediationAdRequest mediationAdRequest, Bundle customEventExtras) { /** * In this method, you should: * 1. Create your interstitial ad. * 2. Set your ad network's listener. * 3. Make an ad request. */ sampleInterstitial = new SampleInterstitial(context); // Here we're assuming the serverParameter is the ad unit for the sample ad network. sampleInterstitial.setAdUnit(serverParameter); // Implement a SampleAdListener and forward callbacks to // AdMob. sampleInterstitial.setAdListener(new SampleCustomInterstitialEventForwarder(listener)); // Make an ad request. sampleInterstitial.fetchAd(createSampleRequest(mediationAdRequest)); } /** * Helper method to create a SampleAdRequest. * @param mediationAdRequest The mediation request with targeting information. * @return The created SampleAdRequest. */ private SampleAdRequest createSampleRequest(MediationAdRequest mediationAdRequest) { SampleAdRequest request = new SampleAdRequest(); request.setTestMode(mediationAdRequest.isTesting()); request.setKeywords(mediationAdRequest.getKeywords()); return request; } @Override public void showInterstitial() { // Show your interstitial ad. sampleInterstitial.show(); } /** The event is being destroyed. Perform any necessary cleanup here. */ @Override public void onDestroy() { if (sampleInterstitial != null) { sampleInterstitial.destroy(); } } /** * The app is being paused. This call is only forwarded to the adapter if * the developer notifies AdMob mediation * that the app is being paused. */ @Override public void onPause() { // The sample ad network doesn't have an onPause method, so does nothing. } /** * The app is being resumed. This call is only forwarded to the * adapter if the developer notifies AdMob * mediation that the app is being resumed. */ @Override public void onResume() { // The sample ad network doesn't have an onResume method, so does nothing. } }
Kotlin
class SampleCustomEventInterstitial : CustomEventInterstitial { /** Represents a SampleInterstitial. */ private lateinit var mSampleInterstitial: SampleInterstitial override fun requestInterstitialAd(context: Context, listener: CustomEventInterstitialListener, serverParameter: String, mediationAdRequest: MediationAdRequest, customEventExtras: Bundle?) { /** * In this method, you should: * 1. Create your interstitial ad. * 2. Set your ad network's listener. * 3. Make an ad request. */ mSampleInterstitial = SampleInterstitial(context) // Here we're assuming the serverParameter is the ad unit for the sample ad network. mSampleInterstitial.adUnit = serverParameter // Implement a SampleAdListener and forward callbacks to // AdMob mediation. mSampleInterstitial.adListener = SampleCustomInterstitialEventForwarder(listener) // Make an ad request. mSampleInterstitial.fetchAd(createSampleRequest(mediationAdRequest)) } /** * Helper method to create a [SampleAdRequest]. * @param mediationAdRequest The mediation request with targeting information. * * * @return The created [SampleAdRequest]. */ private fun createSampleRequest(mediationAdRequest: MediationAdRequest): SampleAdRequest { val request = SampleAdRequest() request.testMode = mediationAdRequest.isTesting request.keywords = mediationAdRequest.keywords return request } override fun showInterstitial() { // Show your interstitial ad. mSampleInterstitial.show() } /** The event is being destroyed. Perform any necessary cleanup here. */ override fun onDestroy() { mSampleInterstitial.destroy() } /** * The app is being paused. This call is only forwarded to the adapter if * the developer notifies AdMob mediation that * the app is being paused. */ override fun onPause() { // The sample ad network doesn't have an onPause method, so does nothing. } /** * The app is being resumed. This call is only forwarded to the adapter if * the developer notifies AdMob mediation that * the app is being resumed. */ override fun onResume() { // The sample ad network doesn't have an onResume method, so does nothing. } }
Giao diện sự kiện tùy chỉnh cho quảng cáo xen kẽ yêu cầu bạn triển khai
phương thức showInterstitial()
. Tính năng dàn xếp sẽ gọi phương thức này khi bạn yêu cầu
SDK quảng cáo trên thiết bị di động của Google hiển thị quảng cáo xen kẽ.
Gửi thông số bổ sung của mạng quảng cáo cho các yêu cầu sự kiện tùy chỉnh (không bắt buộc)
Nếu bạn cần gửi thêm thông số cho sự kiện tùy chỉnh, hãy sử dụng
phương thức
addCustomEventExtrasBundle()
của lớp AdRequest.Builder
.
Phương thức
Bạn phải nhập lớp bộ chuyển đổi sự kiện tùy chỉnh của mình và một gói các thông số bổ sung
cần thiết cho bộ chuyển đổi sự kiện tùy chỉnh đó.
Dưới đây là một đoạn mã cho thấy cách chuyển một thông số "SampleExtra" cho
lớp SampleCustomEventInterstitial
đã được xác định ở trên:
Bundle extras = new Bundle();
extras.putBoolean("SampleExtra", true);
AdRequest request = new AdRequest.Builder()
.addCustomEventExtrasBundle(SampleCustomEventInterstitial.class, extras)
.build();
Nếu bạn không gọi addCustomEventExtrasBundle()
bằng lớp chính xác
của sự kiện tùy chỉnh và một gói thông số cho yêu cầu sự kiện tùy chỉnh, thì thông số bundle
mà bộ chuyển đổi nhận được sẽ là null
.
Thông báo cho tính năng dàn xếp AdMob
Cũng giống như ví dụ về sự kiện tùy chỉnh cho quảng cáo biểu ngữ, hãy triển khai trình xử lý quảng cáo cho mạng của bạn để gửi thông báo về lại cho tính năng dàn xếp AdMob.
Trong ví dụ sau, lớp SampleCustomInterstitialEventForwarder
triển khai giao diện
SampleAdListener
để chuyển tiếp lệnh gọi lại từ mạng quảng cáo
mẫu:
Java
public class SampleCustomInterstitialEventForwarder extends SampleAdListener { private CustomEventInterstitialListener mInterstitialListener; /** * Creates a new SampleInterstitialEventForwarder. * @param listener An AdMob CustomEventInterstitialListener that should * receive forwarded events. */ public SampleCustomInterstitialEventForwarder(CustomEventInterstitialListener listener) { this.mInterstitialListener = listener; } @Override public void onAdFetchSucceeded() { mInterstitialListener.onAdLoaded(); } @Override public void onAdFetchFailed(SampleErrorCode errorCode) { switch (errorCode) { case UNKNOWN: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); break; case BAD_REQUEST: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST); break; case NETWORK_ERROR: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR); break; case NO_INVENTORY: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL); break; } } @Override public void onAdFullScreen() { mInterstitialListener.onAdOpened(); // Only call onAdLeftApplication if your ad network actually exits the developer's app. mInterstitialListener.onAdLeftApplication(); } @Override public void onAdClosed() { mInterstitialListener.onAdClosed(); } }
Kotlin
class SampleCustomInterstitialEventForwarder(private val mInterstitialListener: CustomEventInterstitialListener) : SampleAdListener() { override fun onAdFetchSucceeded() { mInterstitialListener.onAdLoaded() } override fun onAdFetchFailed(errorCode: SampleErrorCode) { when (errorCode) { UNKNOWN -> mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR) BAD_REQUEST -> mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST) NETWORK_ERROR -> mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR) NO_INVENTORY -> mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL) } } override fun onAdFullScreen() { mInterstitialListener.onAdOpened() // Only call onAdLeftApplication if your ad network actually exits the developer's app. mInterstitialListener.onAdLeftApplication() } override fun onAdClosed() { mInterstitialListener.onAdClosed() } }
Đến đây, bạn đã hoàn thành việc triển khai sự kiện tùy chỉnh cho quảng cáo xen kẽ. Bạn có thể xem toàn bộ ví dụ trên GitHub. Bạn có thể sử dụng ví dụ đó với một mạng quảng cáo đã được hỗ trợ hoặc sửa đổi ví dụ để hiển thị các quảng cáo xen kẽ tùy chỉnh.