跳转到内容

创建自定义适配器

如果您想支持未在 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.

  1. 在您的应用程序的 com.applovin.mediation.adapters 包中创建 MediationAdapterBase 的子类。 此类的名称应包含平台名称 (例如,com.mycompanyname.MyNetworkMediationAdapter)。
  2. 重写 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
  3. 重写 getSdkVersion() 方法。 实现从该方法返回平台 SDK 版本字符串的代码。
  4. 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.
  5. 重写 onDestroy() 方法。 实现该方法中平台 SDK 对象的清理逻辑。

横幅广告

  1. com.mycompanyname.MyNetworkMediationAdapter 类中实现 MaxAdViewAdapter 界面。
  2. 重写请求广告的 loadAdViewAd() 方法。 调用 parameters.getThirdPartyAdPlacementId() 方法以获取广告逻辑所需的 Placement ID。
  3. 调用适当的 MaxAdViewAdapterListener 方法。 它们向 AppLovin MAX SDK 发出关于横幅生命周期事件的通知:
    • When an ad loads, call MaxAdViewAdapterListener.onAdViewAdLoaded().
    • 当广告加载失败时,使用适当的 MaxAdapterError 调用 MaxAdViewAdapterListener.onAdViewAdLoadFailed()
    • 还可以酌情调用以下横幅生命周期事件通知:onAdViewAdClicked()onAdViewAdCollapsed()onAdViewAdDisplayed()onAdViewAdDisplayFailed()onAdViewAdExpanded()onAdViewAdHidden()

插屏

  1. com.mycompanyname.MyNetworkMediationAdapter 类中实现 MaxInterstitialAdapter 界面。
  2. 重写请求广告的 loadInterstitialAd() 方法。 调用 parameters.getThirdPartyAdPlacementId() 方法以获取广告逻辑所需的 Placement ID。
  3. 重写显示已加载广告的 showInterstitialAd() 方法。 调用 parameters.getThirdPartyAdPlacementId() 方法以获取广告逻辑所需的 Placement ID。如果广告尚未准备就绪,则调用 MaxInterstitialAdapterListener.onInterstitialAdDisplayFailed( MaxAdapterError.AD_NOT_READY )
  4. 调用适当的 MaxInterstitialAdapterListener 方法。 它们向 AppLovin MAX SDK 发出关于插屏生命周期事件的通知:
    • When an ad loads, call MaxInterstitialAdapterListener.onInterstitialAdLoaded().
    • 当广告加载失败时,使用适当的 MaxAdapterError 调用 MaxInterstitialAdapterListener.onInterstitialAdLoadFailed()
    • 当广告被隐藏时,调用 MaxInterstitialAdapterListener.onInterstitialAdHidden()
    • 还可以酌情调用以下插屏生命周期事件通知:onInterstitialAdClicked()onInterstitialAdDisplayFailed()

激励广告

  1. com.mycompanyname.MyNetworkMediationAdapter 类中实现 MaxRewardedAdapter 界面。
  2. 重写请求广告的 loadRewardedAd() 方法。 调用 parameters.getThirdPartyAdPlacementId() 方法以获取广告逻辑所需的 Placement ID。
  3. 重写显示已加载广告的 showRewardedAd() 方法。 调用 parameters.getThirdPartyAdPlacementId() 方法以获取广告逻辑所需的 Placement ID。如果广告尚未准备就绪,则调用 MaxRewardedAdapterListener.onRewardedAdDisplayFailed( MaxAdapterError.AD_NOT_READY )
  4. 调用适当的 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()

原生广告

  1. com.mycompanyname.MyNetworkMediationAdapter 类中实现 MaxNativeAdAdapter 界面。
  2. 重写请求广告的 loadNativeAd() 方法。
  3. 调用适当的 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().

隐私

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.

  1. 在您的项目中创建一个从 ALMediationAdapter 扩展的 .m.h。 此类的名称应包含平台名称 (例如,MyNetworkMediationAdapter)。
  2. Implement the - (void)initializeWithParameters:(id<MAAdapterInitializationParameters>)parameters completionHandler:(void (^)(MAAdapterInitializationStatus NSString *_Nullable))completionHandler method Implement code that initializes your ad network SDK. You can retrieve the app ID set in the UI from parameters.serverParameters[@"app_id"]. This logic executes in parallel with the initialization of the AppLovin SDK. Call completionHandler after 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
  3. 实现 - (NSString *)SDKVersion 方法。 实现从该方法返回平台 SDK 版本字符串的代码。
  4. Implement the - (NSString *)adapterVersion 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.
  5. 实现 - (void)destroy 方法。 实现该方法中平台 SDK 对象的清理逻辑。

横幅广告

  1. MyNetworkMediationAdapter 类中实现 MAAdViewAdapter 协议。
  2. 实现请求广告的 - (void)loadAdViewAdForParameters:(id<MAAdapterResponseParameters>)parameters adFormat:(MAAdFormat *)adFormat andNotify:(id<MAAdViewAdapterDelegate>)delegate 方法。 调用 parameters.thirdPartyAdPlacementIdentifier 方法以获取广告逻辑所需的 Placement ID。
  3. 调用适当的 MAAdViewAdapterDelegate 方法。 它们向 AppLovin MAX SDK 发出关于横幅生命周期事件的通知:
    • When an ad loads, call -[MAAdViewAdapterDelegate didLoadAdForAdView].
    • 当广告加载失败时,使用适当的 MAAdapterError 调用 -[MAAdViewAdapterDelegate didFailToLoadAdViewAdWithError:]
    • 还可以酌情调用以下横幅生命周期事件通知:didClickAdViewAddidCollapseAdViewAddidDisplayAdViewAddidExpandAdViewAddidFailToDisplayAdViewAdWithErrordidHideAdViewAd

插屏

  1. MyNetworkMediationAdapter 类中实现 MAInterstitialAdapter 协议。
  2. 声明请求广告的 - (void)loadInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegate 方法。 调用 parameters.thirdPartyAdPlacementIdentifier 方法以获取广告逻辑所需的 Placement ID。
  3. 声明显示已加载广告的 - (void)showInterstitialAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MAInterstitialAdapterDelegate>)delegate 方法。 调用 parameters.thirdPartyAdPlacementIdentifier 方法以获取广告逻辑所需的 Placement ID。 如果广告尚未准备就绪,则调用 -[MAInterstitialAdapterDelegate didFailToDisplayInterstitialAdWithError: MAAdapterError.adNotReady]
  4. 调用适当的 MAInterstitialAdapterDelegate 方法。 它们向 AppLovin MAX SDK 发出关于插屏生命周期事件的通知:
    • When an ad loads, call -[MAInterstitialAdapterDelegate didLoadInterstitialAd:].
    • 当广告加载失败时,使用适当的 MAAdapterError 调用 -[MAInterstitialAdapterDelegate didFailToLoadInterstitialAdWithError:]
    • 当广告展示时,调用 -[MAInterstitialAdapterDelegate didDisplayInterstitialAd]
    • 当广告被隐藏时,调用 -[MAInterstitialAdapterDelegate didHideInterstitialAd:]
    • 还可以酌情调用以下插屏生命周期事件通知:didClickInterstitialAddidFailToDisplayInterstitialAdWithError

激励广告

  1. MyNetworkMediationAdapter 类中实现 MARewardedAdapter 协议。
  2. 声明请求广告的 - (void)loadRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate; 方法。 调用 parameters.thirdPartyAdPlacementIdentifier 方法以获取广告逻辑所需的 Placement ID。
  3. 声明显示已加载广告的 - (void)showRewardedAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MARewardedAdapterDelegate>)delegate; 方法。 调用 parameters.thirdPartyAdPlacementIdentifier 方法以获取广告逻辑所需的 Placement ID。 如果广告尚未准备就绪,则调用 -[MARewardedAdapterDelegate didFailToDisplayRewardedAdWithError: MAAdapterError.adNotReady]
  4. 调用适当的 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

原生广告

  1. MyNetworkMediationAdapter 类中实现 MANativeAdAdapter 协议。
  2. 实现请求广告的 - (void)loadNativeAdForParameters:(id<MAAdapterResponseParameters>)parameters andNotify:(id<MANativeAdAdapterDelegate>)delegate; 方法。
  3. 调用适当的 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:].

隐私

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:

  1. 禁用 Test Mode (测试模式)(参见 SDK 集成指南 > 平台 > 测试平台 > Test Mode)
  2. 确保应用包名、广告单元和 SDK Key 均匹配。
  3. 使用实体设备进行测试。
  4. Set the CPM of the custom network high enough that it can win in the waterfall.