Banner ads custom events

先决条件

之前您可以创建自定义事件,您需要的横幅广告格式集成到你的应用程序

您可能还需要第一次读到加载横幅广告,以及如何调解工作。

定义自定义事件

在下面的例子中,您将首先创建中的横幅广告自定义事件Ad Manager 调解。这需要你定义一个自定义事件指向通过您的应用程序特定类Ad Manager UI,然后实现CustomEventBanner服务的视图。这个例子定义了从一个定制事件来展示广告示例广告网络

自定义事件必须在规定的谷歌广告管理系统UI 。你可以找到设置广告管理系统的产量组此调解指导帮助中心文章

请确保你给的类名称的值具有完整路径。参数应该包含发出广告请求你的自定义事件实现网络所需的任何信息。

下面是显示出一些示例自定义事件设置的截图:

请求横幅

要请求横幅广告,定义一个类实现CustomEventBanner并调用它SampleCustomEventBanner 。当从广告中介流选择自定义事件,该requestBannerAd()方法被调用您在设置中提供的类名。

上文所定义的可选参数被传递到自定义事件作为一部分requestBannerAd()方法。通常,此参数是一个标识符,使自定义事件能够确定要加载的广告。

您还应该根据需要实现生命周期方法。Ad Manager 调解转发适配器onPause()onResume()活动事件如果用户调用的AdManagerAdView.pause()AdManagerAdView.resume()方法。示例广告网络不包含暂停或恢复调用,因此它提供了一个空实现。调解使调用它的最佳尝试onDestroy()时,适配器将被摧毁。在那里执行任何必要的清理工作。

下面是一个例子实施SampleCustomEventBanner

爪哇

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 Ad Manager 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 Ad Manager
     * 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 Ad Manager.
        // The callback forwarding is handled by SampleBannerEventForwarder.
        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;
    }
}

科特林

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 Ad Manager 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 Ad Manager
     * 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
        // Ad Manager mediation. The callback forwarding
        // is handled by SampleBannerEventForwarder.
        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
    }
}

所述SampleAdView样品中使用的类是从第三方广告视图的一个简单的例子样品SDK

为自定义事件请求发送广告网络附加信息(可选)

如果你需要额外的参数发送到您的自定义事件,请使用addCustomEventExtrasBundle()的方法AdManagerAdRequest.Builder类。你必须通过你的自定义事件适配器类和自定义事件适配器预计额外的包。

下面是一个代码片段,展示了如何通过一个SampleExtra参数为SampleCustomEventBanner类时定义:

Bundle extras = new Bundle();
extras.putBoolean("SampleExtra", true);

AdManagerAdRequest request = new AdManagerAdRequest.Builder()
        .addCustomEventExtrasBundle(SampleCustomEventBanner.class, extras)
        .build();

如果你不叫addCustomEventExtras()与正确的自定义事件类和自定义事件请求包,该bundle通过适配器接收参数为null

通知 Ad Manager 调解

您的自定义事件必须通过通知中介CustomEventBannerListener当它加载或无法加载广告接口。否则,自定义事件会超时,并且广告中介会转移到下一个网络。

实施网络广告监听器并调用相关的回调CustomEventBannerListener将消息发送回Ad Manager 调解。

Ad Manager 中介支持以下回调:

方法什么时候打电话
onAdLoaded()横幅请求成功。
onAdFailedToLoad()横幅请求失败。
onAdClicked()横幅被点击了。
onAdOpened()横幅正在呈现全屏视图。
onAdClosed()用户单击横幅后返回应用程序。

你就需要通知Ad Manager 调解所有的回调。

下面是一个例子实施SampleCustomBannerEventForwarder

爪哇

public class SampleCustomBannerEventForwarder extends SampleAdListener {
    private CustomEventBannerListener mBannerListener;
    private SampleAdView mAdView;

    /**
     * Creates a new SampleBannerEventForwarder.
     * @param listener A CustomEventBannerListener that should receive
     *                 forwarded events.
     * @param adView   A 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(AdManagerAdRequest.ERROR_CODE_INTERNAL_ERROR);
                break;
            case BAD_REQUEST:
                mBannerListener.onAdFailedToLoad(AdManagerAdRequest.ERROR_CODE_INVALID_REQUEST);
                break;
            case NETWORK_ERROR:
                mBannerListener.onAdFailedToLoad(AdManagerAdRequest.ERROR_CODE_NETWORK_ERROR);
                break;
            case NO_INVENTORY:
                mBannerListener.onAdFailedToLoad(AdManagerAdRequest.ERROR_CODE_NO_FILL);
                break;
        }
    }

    @Override
    public void onAdFullScreen() {
        mBannerListener.onAdClicked();
        mBannerListener.onAdOpened();
    }

    @Override
    public void onAdClosed() {
        mBannerListener.onAdClosed();
    }
}

科特林

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(AdManagerAdRequest.ERROR_CODE_INTERNAL_ERROR)
            BAD_REQUEST -> mBannerListener.onAdFailedToLoad(AdManagerAdRequest.ERROR_CODE_INVALID_REQUEST)
            NETWORK_ERROR -> mBannerListener.onAdFailedToLoad(AdManagerAdRequest.ERROR_CODE_NETWORK_ERROR)
            NO_INVENTORY -> mBannerListener.onAdFailedToLoad(AdManagerAdRequest.ERROR_CODE_NO_FILL)
        }
    }

    override fun onAdFullScreen() {
        mBannerListener.onAdClicked()
        mBannerListener.onAdOpened()
    }

    override fun onAdClosed() {
        mBannerListener.onAdClosed()
    }
}

这样就完成了横幅广告的自定义事件实现。完整的例子可以在GitHub上。您可以将其与已受支持的广告网络一起使用,或对其进行修改以显示自定义横幅广告。