Peristiwa kustom iklan banner

Prasyarat

Selesaikan penyiapan peristiwa kustom.

Meminta iklan banner

Saat item baris peristiwa kustom tercapai dalam rantai mediasi waterfall, the loadBannerAd() method akan dipanggil pada nama class yang Anda berikan saat membuat peristiwa kustom. Dalam hal ini, metode tersebut ada di SampleCustomEvent, yang kemudian memanggil the loadBannerAd() method dalam SampleBannerCustomEventLoader.

Untuk meminta iklan banner, buat atau ubah class yang memperluas Adapter untuk menerapkan loadBannerAd(). Selain itu, buat class baru untuk mengimplementasikan MediationBannerAd.

Dalam contoh peristiwa kustom kami, SampleCustomEvent mengimplementasikanthe Adapter interface , lalu mendelegasikan keSampleBannerCustomEventLoader.

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 bertanggung jawab atas tugas-tugas berikut:

  • Memuat iklan banner dan memanggil MediationAdLoadCallback method setelah pemuatan selesai

  • Mengimplementasikan MediationBannerAd interface

  • Menerima dan melaporkan callback peristiwa iklan ke Google Mobile Ads SDK

Parameter opsional yang ditentukan dalam UI AdMob disertakan dalam konfigurasi iklan. Parameter ini dapat diakses melalui adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD). Parameter ini biasanya merupakan ID unit iklan yang diperlukan SDK jaringan iklan saat membuat instance objek iklan.

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;
  }
}

Bergantung pada apakah iklan berhasil diambil atau mengalami error, Anda akan memanggil onSuccess() atau onFailure(). onSuccess() dipanggil dengan meneruskan instance class yang mengimplementasikan MediationBannerAd.

Biasanya, metode ini diimplementasikan di dalam callback dari SDK pihak ketiga yang diterapkan oleh adaptor Anda. Untuk contoh ini, SDK Sampel memiliki SampleAdListener dengan callback yang relevan:

Java

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

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

MediationBannerAd memerlukan penerapan metode pengambil View:

Java

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

Meneruskan peristiwa mediasi ke Google Mobile Ads SDK

Setelah onSuccess() dipanggil, objek MediationBannerAdCallback yang ditampilkan kemudian dapat digunakan oleh adaptor untuk meneruskan peristiwa presentasi dari SDK pihak ketiga ke Google Mobile Ads SDK. Class SampleBannerCustomEventLoader memperluas antarmuka SampleAdListener untuk meneruskan callback dari jaringan iklan contoh ke Google Mobile Ads SDK.

Peristiwa kustom Anda harus meneruskan callback ini sebanyak mungkin, agar aplikasi Anda menerima peristiwa yang setara dari Google Mobile Ads SDK. Berikut adalah contoh penggunaan callback:

Java

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

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

Ini menyelesaikan penerapan peristiwa kustom untuk iklan banner. Contoh lengkapnya tersedia di GitHub. Anda dapat menggunakannya dengan jaringan iklan yang sudah didukung atau mengubahnya untuk menampilkan iklan banner peristiwa kustom.