前提条件
完成自定义事件设置。
请求插页式广告
当到达广告瀑布流中介链中的自定义事件订单项时,
系统将对您提供的类名称调用 loadInterstitialAd()
方法,
创建自定义
事件。在此示例中
该方法位于 SampleCustomEvent
中,后者随后会调用
SampleInterstitialCustomEventLoader
中的 loadInterstitialAd()
方法。
要请求插页式广告,请创建或修改一个扩展 Adapter
的类
来实现 loadInterstitialAd()
。此外,创建一个新课程以
实现 MediationInterstitialAd
。
在我们的自定义事件示例中,
SampleCustomEvent
会扩展 Adapter
类,然后委托给
SampleInterstitialCustomEventLoader
。
Java
package com.google.ads.mediation.sample.customevent; import com.google.android.gms.ads.mediation.Adapter; import com.google.android.gms.ads.mediation.MediationAdConfiguration; import com.google.android.gms.ads.mediation.MediationAdLoadCallback; import com.google.android.gms.ads.mediation.MediationInterstitialAd; import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback; ... public class SampleCustomEvent extends Adapter { private SampleInterstitialCustomEventLoader interstitialLoader; @Override public void loadInterstitialAd( @NonNull MediationInterstitialAdConfiguration adConfiguration, @NonNull MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback> callback) { interstitialLoader = new SampleInterstitialCustomEventLoader(adConfiguration, callback); interstitialLoader.loadAd(); } }
SampleInterstitialCustomEventLoader
负责以下任务:
加载插页式广告并调用
MediationAdLoadCallback
方法。实现
MediationInterstitialAd
接口。接收广告事件回调并报告给 Google 移动广告 SDK
AdMob 界面中定义的可选参数是
包含的广告配置中可以通过
adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
。
此参数通常是广告联盟 SDK
所需的资源。
Java
package com.google.ads.mediation.sample.customevent; import com.google.android.gms.ads.mediation.Adapter; import com.google.android.gms.ads.mediation.MediationInterstitialAdConfiguration; import com.google.android.gms.ads.mediation.MediationAdLoadCallback; import com.google.android.gms.ads.mediation.MediationInterstitialAd; import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback; ... public class SampleInterstitialCustomEventLoader extends SampleAdListener implements MediationInterstitialAd { /** A sample third-party SDK interstitial ad. */ private SampleInterstitial sampleInterstitialAd; /** Configuration for requesting the interstitial ad from the third-party network. */ private final MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration; /** Callback for interstitial ad events. */ private MediationInterstitialAdCallback interstitialAdCallback; /** Callback that fires on loading success or failure. */ private final MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback> mediationAdLoadCallback; /** Constructor. */ public SampleInterstitialCustomEventLoader( @NonNull MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration, @NonNull MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback> mediationAdLoadCallback) { this.mediationInterstitialAdConfiguration = mediationInterstitialAdConfiguration; this.mediationAdLoadCallback = mediationAdLoadCallback; } /** Loads the interstitial ad from the third-party ad network. */ public void loadAd() { // All custom events have a server parameter named "parameter" that returns // back the parameter entered into the UI when defining the custom event. Log.i("InterstitialCustomEvent", "Begin loading interstitial ad."); String serverParameter = mediationInterstitialAdConfiguration.getServerParameters().getString( MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD); Log.d("InterstitialCustomEvent", "Received server parameter."); sampleInterstitialAd = new SampleInterstitial(mediationInterstitialAdConfiguration.getContext()); sampleInterstitialAd.setAdUnit(serverParameter); // Implement a SampleAdListener and forward callbacks to mediation. sampleInterstitialAd.setAdListener(this); // Make an ad request. Log.i("InterstitialCustomEvent", "start fetching interstitial ad."); sampleInterstitialAd.fetchAd( SampleCustomEvent.createSampleRequest(mediationInterstitialAdConfiguration)); } public SampleAdRequest createSampleRequest( MediationAdConfiguration mediationAdConfiguration) { SampleAdRequest request = new SampleAdRequest(); request.setTestMode(mediationAdConfiguration.isTestRequest()); request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet()); return request; } }
根据广告是成功抓取还是遇到错误,您可以
会调用
onSuccess()
或
onFailure()
。
onSuccess()
的调用方法为:传入实现
MediationInterstitialAd
。
通常,这些方法是在
适配器实现的第三方 SDK。在本示例中,示例 SDK
具有带有相关回调的 SampleAdListener
:
Java
@Override public void onAdFetchSucceeded() { interstitialAdCallback = mediationAdLoadCallback.onSuccess(this); } @Override public void onAdFetchFailed(SampleErrorCode errorCode) { mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode)); }
MediationInterstitialAd
需要实现 showAd()
方法才能显示
广告:
Java
@Override public void showAd(@NonNull Context context) { sampleInterstitialAd.show(); }
将中介事件转发至 Google 移动广告 SDK
调用 onSuccess()
后,返回的 MediationInterstitialAdCallback
然后,适配器可以使用该对象从
添加到 Google 移动广告 SDK。通过
SampleInterstitialCustomEventLoader
类扩展了 SampleAdListener
用于将示例广告联盟中的回调转发给 Google 移动的界面
广告 SDK。
请务必确保您的自定义事件转发的回调数量与 这样您的应用才能从 Google 移动广告 SDK。下面是一个使用回调的示例:
Java
@Override public void onAdFullScreen() { interstitialAdCallback.reportAdImpression(); interstitialAdCallback.onAdOpened(); } @Override public void onAdClosed() { interstitialAdCallback.onAdClosed(); }
到这里,我们已经实现针对插页式广告的自定义事件。完整的 示例可在 GitHub 您可以将其用于已获支持的广告网络,也可以将其修改为 展示自定义事件插页式广告。