创建自定义适配器
如果您想支持未在 AppLovin MAX Mediation Matrix 中列出的广告平台,您可以编写自定义适配器。 以下说明将向您展示如何执行此操作。
Android
Implement a subclass of com.applovin.mediation.adapters.MediationAdapterBase.
This subclass enables the interface between your mediation adapter and the AppLovin MAX SDK.
That interface handles functions of your SDK such as initialization, versioning, and resource cleanup.
- 在您的应用程序的
com.applovin.mediation.adapters包中创建MediationAdapterBase的子类。 此类的名称应包含平台名称 (例如,com.mycompanyname.MyNetworkMediationAdapter)。 - 重写
initialize(final MaxAdapterInitializationParameters parameters, final Activity activity, final OnCompletionListener onCompletionListener)方法。 实现初始化广告平台 SDK 的代码。 如果您需要访问在 UI 中设置的 app ID,可以通过parameters.getServerParameters().getString( "app_id", null );进行访问。 此逻辑与 AppLovin SDK 的初始化并行执行。 在初始化广告平台后调用onCompletionListener.onCompletion(),并向该完成处理程序传递以下值之一:- 当广告平台 SDK 初始化时:
InitializationStatus.INITIALIZING - If the ad network SDK initializes:
InitializationStatus.INITIALIZED_SUCCESS - 如果广告平台 SDK 初始化失败:
InitializationStatus.INITIALIZED_FAILURE(带有相关错误信息) - 如果广告平台 SDK 没有初始化回传状态:
InitializationStatus.DOES_NOT_APPLY - 如果广告平台已明确初始化,但没有状态:
InitializationStatus.INITIALIZED_UNKNOWN
- 当广告平台 SDK 初始化时:
- 重写
getSdkVersion()方法。 实现从该方法返回平台 SDK 版本字符串的代码。 - Override the
getAdapterVersion()method. Implement code that returns the version number of the mediation adapter from that method. All AppLovin’s adapters use a five-number versioning scheme: The leftmost four numbers correspond to the network SDK version. The last number denotes the minor version number, which refers to the adapter release. - 重写
onDestroy()方法。 实现该方法中平台 SDK 对象的清理逻辑。
横幅广告
- 在
com.mycompanyname.MyNetworkMediationAdapter类中实现MaxAdViewAdapter界面。 - 重写请求广告的
loadAdViewAd()方法。 调用parameters.getThirdPartyAdPlacementId()方法以获取广告逻辑所需的 Placement ID。 - 调用适当的
MaxAdViewAdapterListener方法。 它们向 AppLovin MAX SDK 发出关于横幅生命周期事件的通知:- When an ad loads, call
MaxAdViewAdapterListener.onAdViewAdLoaded(). - 当广告加载失败时,使用适当的
MaxAdapterError调用MaxAdViewAdapterListener.onAdViewAdLoadFailed()。 - 还可以酌情调用以下横幅生命周期事件通知:
onAdViewAdClicked()、onAdViewAdCollapsed()、onAdViewAdDisplayed()、onAdViewAdDisplayFailed()、onAdViewAdExpanded()、onAdViewAdHidden()。
- When an ad loads, call
插屏
- 在
com.mycompanyname.MyNetworkMediationAdapter类中实现MaxInterstitialAdapter界面。 - 重写请求广告的
loadInterstitialAd()方法。 调用parameters.getThirdPartyAdPlacementId()方法以获取广告逻辑所需的 Placement ID。 - 重写显示已加载广告的
showInterstitialAd()方法。 调用parameters.getThirdPartyAdPlacementId()方法以获取广告逻辑所需的 Placement ID。如果广告尚未准备就绪,则调用MaxInterstitialAdapterListener.onInterstitialAdDisplayFailed( MaxAdapterError.AD_NOT_READY )。 - 调用适当的
MaxInterstitialAdapterListener方法。 它们向 AppLovin MAX SDK 发出关于插屏生命周期事件的通知:- When an ad loads, call
MaxInterstitialAdapterListener.onInterstitialAdLoaded(). - 当广告加载失败时,使用适当的
MaxAdapterError调用MaxInterstitialAdapterListener.onInterstitialAdLoadFailed()。 - 当广告被隐藏时,调用
MaxInterstitialAdapterListener.onInterstitialAdHidden()。 - 还可以酌情调用以下插屏生命周期事件通知:
onInterstitialAdClicked()、onInterstitialAdDisplayFailed()。
- When an ad loads, call
激励广告
- 在
com.mycompanyname.MyNetworkMediationAdapter类中实现MaxRewardedAdapter界面。 - 重写请求广告的
loadRewardedAd()方法。 调用parameters.getThirdPartyAdPlacementId()方法以获取广告逻辑所需的 Placement ID。 - 重写显示已加载广告的
showRewardedAd()方法。 调用parameters.getThirdPartyAdPlacementId()方法以获取广告逻辑所需的 Placement ID。如果广告尚未准备就绪,则调用MaxRewardedAdapterListener.onRewardedAdDisplayFailed( MaxAdapterError.AD_NOT_READY )。 - 调用适当的
MaxRewardedAdapterListener方法。 它们向 AppLovin MAX SDK 发出关于激励视频生命周期事件的通知:- When an ad loads, call
MaxRewardedAdapterListener.onRewardedAdLoaded(). - 当广告加载失败时,使用适当的
MaxAdapterError调用MaxRewardedAdapterListener.onRewardedAdLoadFailed()。 - 当广告开始播放时,调用
MaxRewardedAdapterListener.onRewardedAdDisplayed()。 - 如果应该向用户提供奖励,使用适当的
MaxReward金额和货币调用MaxRewardedAdapterListener.onUserRewarded()。 如果没有可用金额,则默认为MaxReward.DEFAULT_AMOUNT。 AppLovin 建议您在MaxRewardedAdapterListener.onRewardedAdHidden()之前立即调用此项。 - 当广告被隐藏时,调用
MaxRewardedAdapterListener.onRewardedAdHidden()。 - 还可以酌情调用以下激励视频生命周期事件通知:
onRewardedAdClicked()。
- When an ad loads, call
原生广告
- 在
com.mycompanyname.MyNetworkMediationAdapter类中实现MaxNativeAdAdapter界面。 - 重写请求广告的
loadNativeAd()方法。 - 调用适当的
MaxNativeAdAdapterListener方法。 它们向 AppLovin MAX SDK 发出关于原生广告生命周期事件的通知:- When an ad loads, call
MaxNativeAdAdapterListener.onNativeAdLoaded(). - 当广告加载失败时,使用适当的
MaxAdapterError调用MaxNativeAdAdapterListener.onNativeAdLoadFailed()。 - 当广告展示时,调用
MaxNativeAdAdapterListener.onNativeAdDisplayed()。 - When the user clicks an ad, call
MaxNativeAdAdapterListener.onNativeAdClicked().
- When an ad loads, call
隐私
MAX offers two boolean flags that indicate whether the user provides privacy consent.
They are Boolean objects.
Their value is null if the user has indicated neither consent or no consent.
MAX passes the parameters object into each method.
To get the value of the privacy flags, use the following methods of the parameters object:
MaxAdapterParameters.hasUserConsent()- 用于 GDPR 许可标记。 要确定 GDPR 是否适用于用户,请阅读 SDK 集成指南 > 平台 > 概览 > 隐私页面。
MaxAdapterParameters.isDoNotSell()- 用于多州定向广告许可标记。 AppLovin 没有 API 来指示用户是否属于适用的地区。
iOS
Implement a subclass of ALMediationAdapter.
This subclass enables the interface between your mediation adapter and the AppLovin MAX SDK.
That interface handles functions of your SDK such as initialization, versioning, and resource cleanup.
- 在您的项目中创建一个从
ALMediationAdapter扩展的.m和.h。 此类的名称应包含平台名称 (例如,MyNetworkMediationAdapter)。 - Implement the
- (void)initializeWithParameters:(id<MAAdapterInitializationParameters>)parameters completionHandler:(void (^)(MAAdapterInitializationStatus NSString *_Nullable))completionHandlermethod Implement code that initializes your ad network SDK. You can retrieve the app ID set in the UI fromparameters.serverParameters[@"app_id"]. This logic executes in parallel with the initialization of the AppLovin SDK. CallcompletionHandlerafter you initialize the ad network, and pass that handler one of the following values:- 当广告平台 SDK 初始化时:
MAAdapterInitializationStatusInitializing - If the ad network SDK initializes:
MAAdapterInitializationStatusInitializedSuccess - 如果广告平台 SDK 初始化失败:
MAAdapterInitializationStatusInitializedFailure(带有相关错误信息) - 如果广告平台 SDK 没有初始化回传状态:
MAAdapterInitializationStatusDoesNotApply(错误信息为null) - 如果广告平台已明确初始化,但没有状态:
MAAdapterInitializationStatusInitializedUnknown
- 当广告平台 SDK 初始化时:
- 实现
- (NSString *)SDKVersion方法。 实现从该方法返回平台 SDK 版本字符串的代码。 - Implement the
- (NSString *)adapterVersionmethod. Implement code that returns the version number of the mediation adapter from that method. All AppLovin’s adapters use a five-number versioning scheme: The leftmost four numbers correspond to the network SDK version. The last number denotes the minor version number, which refers to the adapter release. - 实现
- (void)destroy方法。 实现该方法中平台 SDK 对象的清理逻辑。
横幅广告
- 在
MyNetworkMediationAdapter类中实现MAAdViewAdapter协议。 - 实现请求广告的
- (void)loadAdViewAdForParameters:(id<MAAdapterResponseParameters>)parameters adFormat:(MAAdFormat *)adFormat andNotify:(id<MAAdViewAdapterDelegate>)delegate方法。 调用parameters.thirdPartyAdPlacementIdentifier方法以获取广告逻辑所需的 Placement ID。 - 调用适当的
MAAdViewAdapterDelegate方法。 它们向 AppLovin MAX SDK 发出关于横幅生命周期事件的通知:- When an ad loads, call
-[MAAdViewAdapterDelegate didLoadAdForAdView]. - 当广告加载失败时,使用适当的
MAAdapterError调用-[MAAdViewAdapterDelegate didFailToLoadAdViewAdWithError:]。 - 还可以酌情调用以下横幅生命周期事件通知:
didClickAdViewAd、didCollapseAdViewAd、didDisplayAdViewAd、didExpandAdViewAd、didFailToDisplayAdViewAdWithError、didHideAdViewAd。
- When an ad loads, call
插屏
- 在
MyNetworkMediationAdapter类中实现MAInterstitialAdapter协议。 - 声明请求广告的
- (void)loadInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegate方法。 调用parameters.thirdPartyAdPlacementIdentifier方法以获取广告逻辑所需的 Placement ID。 - 声明显示已加载广告的
- (void)showInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegate方法。 调用parameters.thirdPartyAdPlacementIdentifier方法以获取广告逻辑所需的 Placement ID。 如果广告尚未准备就绪,则调用-[MAInterstitialAdapterDelegate didFailToDisplayInterstitialAdWithError: MAAdapterError.adNotReady]。 - 调用适当的
MAInterstitialAdapterDelegate方法。 它们向 AppLovin MAX SDK 发出关于插屏生命周期事件的通知:- When an ad loads, call
-[MAInterstitialAdapterDelegate didLoadInterstitialAd:]. - 当广告加载失败时,使用适当的
MAAdapterError调用-[MAInterstitialAdapterDelegate didFailToLoadInterstitialAdWithError:]。 - 当广告展示时,调用
-[MAInterstitialAdapterDelegate didDisplayInterstitialAd]。 - 当广告被隐藏时,调用
-[MAInterstitialAdapterDelegate didHideInterstitialAd:]。 - 还可以酌情调用以下插屏生命周期事件通知:
didClickInterstitialAd、didFailToDisplayInterstitialAdWithError。
- When an ad loads, call
激励广告
- 在
MyNetworkMediationAdapter类中实现MARewardedAdapter协议。 - 声明请求广告的
- (void)loadRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate;方法。 调用parameters.thirdPartyAdPlacementIdentifier方法以获取广告逻辑所需的 Placement ID。 - 声明显示已加载广告的
- (void)showRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate;方法。 调用parameters.thirdPartyAdPlacementIdentifier方法以获取广告逻辑所需的 Placement ID。 如果广告尚未准备就绪,则调用-[MARewardedAdapterDelegate didFailToDisplayRewardedAdWithError: MAAdapterError.adNotReady]。 - 调用适当的
MARewardedAdapterDelegate方法。 它们向 AppLovin MAX SDK 发出关于激励视频生命周期事件的通知:- When an ad loads, call
-[MARewardedAdapterDelegate didLoadRewardedAd:]. - 当广告加载失败时,使用适当的
MAAdapterError调用-[MARewardedAdapterDelegate didFailToLoadRewardedAdWithError:]。 - 当广告开始播放时,调用
-[MARewardedAdapterDelegate didDisplayRewardedAd:]。 - 如果应该向用户提供奖励,使用适当的
MAReward金额和货币调用-[MARewardedAdapterDelegate didRewardUserWithReward:]。 如果没有可用金额,则默认为MAReward.defaultAmount。 AppLovin 建议您在-[MARewardedAdapterDelegate didHideRewardedAd:]之前立即调用此项。 - 当广告被隐藏时,调用
-[MARewardedAdapterDelegate didHideRewardedAd:]。 - 还可以酌情调用以下激励视频生命周期事件通知:
didClickRewardedAd。
- When an ad loads, call
原生广告
- 在
MyNetworkMediationAdapter类中实现MANativeAdAdapter协议。 - 实现请求广告的
- (void)loadNativeAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MANativeAdAdapterDelegate>)delegate;方法。 - 调用适当的
MANativeAdAdapterDelegate方法。 它们向 AppLovin MAX SDK 发出关于原生广告生命周期事件的通知:- When an ad loads, call
-[MANativeAdAdapterDelegate didLoadAdForNativeAd:]. - 当广告加载失败时,使用适当的
MAAdapterError调用-[MANativeAdAdapterDelegate didFailToLoadNativeAdWithError:]。 - 当广告展示时,调用
-[MANativeAdAdapterDelegate didDisplayNativeAdWithExtraInfo:]。 - When the user clicks an ad, call
-[MANativeAdAdapterDelegate didClickNativeAd:].
- When an ad loads, call
隐私
MAX offers two boolean flags that indicate whether the user provides privacy consent.
They are NSNumber objects.
Their value is nil if the user has indicated neither consent or no consent.
MAX passes the parameters object into each method.
To get the value of the privacy flags, use the following APIs of the parameters object:
-[MAAdapterParameters hasUserConsent]- 用于 GDPR 许可标记。 要确定 GDPR 是否适用于用户,请阅读 SDK 集成指南 > 平台 > 概览 > 隐私页面。
-[MAAdapterParameters isDoNotSell]- 用于多州定向广告许可标记。 AppLovin 没有 API 来指示用户是否属于适用的地区。
测试
您可以使用 MAX Demo App 测试自定义适配器 (请参阅 SDK 集成指南 > 平台 > 测试平台)。 要想获得广告请求,请提高瀑布流中广告位的 CPM。
故障排查
If your custom adapter does not appear in the waterfall as you expect, use the following troubleshooting checklist:
- 禁用 Test Mode (测试模式)(参见 SDK 集成指南 > 平台 > 测试平台 > Test Mode)
- 确保应用包名、广告单元和 SDK Key 均匹配。
- 使用实体设备进行测试。
- Set the CPM of the custom network high enough that it can win in the waterfall.