横幅广告自定义事件

前提条件

完成自定义事件设置

请求横幅广告

当到达广告瀑布流中介链中的自定义事件订单项时, 系统将对您提供的类名称调用 loadBannerAd() 方法, 创建自定义 事件。在此示例中 该方法位于 SampleCustomEvent 中,后者随后会调用 loadBannerAd() 方法。SampleBannerCustomEventLoader

如需请求横幅广告,请创建或修改一个扩展 Adapter 以实现 loadBannerAd() 的类。此外,请创建一个新类来实现 MediationBannerAd

在我们的自定义事件示例中,SampleCustomEvent 会扩展 Adapter 类,然后委托给 SampleBannerCustomEventLoader

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.MediationBannerAd;
import com.google.android.gms.ads.mediation.MediationBannerAdCallback;
...

public class SampleCustomEvent extends Adapter {
  private SampleBannerCustomEventLoader bannerLoader;
  @Override
  public void loadBannerAd(
      @NonNull MediationBannerAdConfiguration adConfiguration,
      @NonNull MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback> callback) {
    bannerLoader = new SampleBannerCustomEventLoader(adConfiguration, callback);
    bannerLoader.loadAd();
  }
}

SampleBannerCustomEventLoader 负责执行以下任务:

  • 加载横幅广告,并在加载完成后调用 MediationAdLoadCallback 方法。

  • 实现 MediationBannerAd 接口。

  • 接收广告事件回调并向 Google 移动广告 SDK 报告。

界面中定义的可选参数会包含在广告配置中。可以通过 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.MediationBannerAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationBannerAd;
import com.google.android.gms.ads.mediation.MediationBannerAdCallback;
...

public class SampleBannerCustomEventLoader extends SampleAdListener implements MediationBannerAd {

  /** View to contain the sample banner ad. */
  private SampleAdView sampleAdView;

  /** Configuration for requesting the banner ad from the third-party network. */
  private final MediationBannerAdConfiguration mediationBannerAdConfiguration;

  /** Callback that fires on loading success or failure. */
  private final MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
      mediationAdLoadCallback;

  /** Callback for banner ad events. */
  private MediationBannerAdCallback bannerAdCallback;

  /** Constructor. */
  public SampleBannerCustomEventLoader(
      @NonNull MediationBannerAdConfiguration mediationBannerAdConfiguration,
      @NonNull MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
              mediationAdLoadCallback) {
    this.mediationBannerAdConfiguration = mediationBannerAdConfiguration;
    this.mediationAdLoadCallback = mediationAdLoadCallback;
  }

  /** Loads a banner 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("BannerCustomEvent", "Begin loading banner ad.");
    String serverParameter =
        mediationBannerAdConfiguration.getServerParameters().getString(
        MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);

    Log.d("BannerCustomEvent", "Received server parameter.");

    Context context = mediationBannerAdConfiguration.getContext();
    sampleAdView = new SampleAdView(context);

    // Assumes that the serverParameter is the ad unit of the Sample Network.
    sampleAdView.setAdUnit(serverParameter);
    AdSize size = mediationBannerAdConfiguration.getAdSize();

    // Internally, smart banners use constants to represent their ad size, which
    // means a call to AdSize.getHeight could return a negative value. You can
    // accommodate this by using AdSize.getHeightInPixels and
    // AdSize.getWidthInPixels instead, and then adjusting to match the device's
    // display metrics.
    int widthInPixels = size.getWidthInPixels(context);
    int heightInPixels = size.getHeightInPixels(context);
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    int widthInDp = Math.round(widthInPixels / displayMetrics.density);
    int heightInDp = Math.round(heightInPixels / displayMetrics.density);

    sampleAdView.setSize(new SampleAdSize(widthInDp, heightInDp));
    sampleAdView.setAdListener(this);

    SampleAdRequest request = createSampleRequest(mediationBannerAdConfiguration);
    Log.i("BannerCustomEvent", "Start fetching banner ad.");
    sampleAdView.fetchAd(request);
  }

  public SampleAdRequest createSampleRequest(
      MediationAdConfiguration mediationAdConfiguration) {
    SampleAdRequest request = new SampleAdRequest();
    request.setTestMode(mediationAdConfiguration.isTestRequest());
    request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
    return request;
  }
}

根据广告是成功抓取还是遇到错误,您可以 会调用 onSuccess()onFailure()onSuccess() 的调用方法为:传入实现 MediationBannerAd

通常,这些方法是在 适配器实现的第三方 SDK。在本示例中,示例 SDK 具有带有相关回调的 SampleAdListener

Java

@Override
public void onAdFetchSucceeded() {
  bannerAdCallback = mediationAdLoadCallback.onSuccess(this);
}

@Override
public void onAdFetchFailed(SampleErrorCode errorCode) {
  mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}

MediationBannerAd 需要实现 View Getter 方法:

Java

@Override
@NonNull
public View getView() {
  return sampleAdView;
}

将中介事件转发至 Google 移动广告 SDK

调用 onSuccess() 后,适配器便可以使用返回的 MediationBannerAdCallback 对象,将展示事件从第三方 SDK 转发至 Google 移动广告 SDK。通过 SampleBannerCustomEventLoader 类扩展了 SampleAdListener 接口 将示例广告联盟中的回调转发给 Google 移动广告 SDK。

您的自定义事件必须尽可能多地转发这些回调,以便您的应用从 Google 移动广告 SDK 接收这些等效事件。下面是一个使用回调的示例:

Java

@Override
public void onAdFullScreen() {
  bannerAdCallback.onAdOpened();
  bannerAdCallback.reportAdClicked();
}

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

到这里,我们已经实现针对横幅广告的自定义事件。完整示例 可通过以下平台观看: GitHub。 您可以将其用于已获支持的广告网络,也可以将其修改为 展示自定义事件横幅广告。