Sự kiện tuỳ chỉnh cho quảng cáo có tặng thưởng

Điều kiện tiên quyết

Hoàn tất quy trình thiết lập sự kiện tuỳ chỉnh.

Yêu cầu quảng cáo có tặng thưởng

Khi đạt đến mục hàng sự kiện tuỳ chỉnh trong chuỗi dàn xếp kiểu thác nước, the loadRewardedAd() method sẽ được gọi dựa trên tên lớp mà bạn đã cung cấp khi tạo sự kiện tuỳ chỉnh. Trong trường hợp này, phương thức đó nằm trong SampleCustomEvent, sau đó sẽ gọithe loadRewardedAd() method trong SampleRewardedCustomEventLoader.

Để yêu cầu hiển thị quảng cáo có tặng thưởng, hãy tạo hoặc sửa đổi một lớp mở rộng Adapter để triển khai loadRewardedAd(). Ngoài ra, hãy tạo một lớp mới để triển khai MediationRewardedAd.

Trong ví dụ về sự kiện tuỳ chỉnh của chúng tôi, SampleCustomEvent sẽ triển khaithe Adapter interface , sau đó uỷ quyền choSampleRewardedCustomEventLoader.

Java

package com.google.ads.mediation.sample.customevent;

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...

public class SampleCustomEvent extends Adapter {

  private SampleNativeCustomEventLoader nativeLoader;

  @Override
  public void loadRewardedAd(
      @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
      @NonNull
          MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
              mediationAdLoadCallback) {
    rewardedLoader =
        new SampleRewardedCustomEventLoader(
            mediationRewardedAdConfiguration, mediationAdLoadCallback);
    rewardedLoader.loadAd();
  }
}

SampleRewardedCustomEventLoader phụ trách những việc sau:

  • Đang tải quảng cáo có tặng thưởng

  • Triển khai MediationRewardedAd interface

  • Nhận và báo cáo lệnh gọi lại sự kiện quảng cáo cho SDK quảng cáo trên thiết bị di động của Google

Thông số không bắt buộc được xác định trong giao diện người dùng Ad Manager được đưa vào cấu hình quảng cáo. Bạn có thể truy cập tham số này thông qua adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD). Thông số này thường là giá trị nhận dạng đơn vị quảng cáo mà SDK mạng quảng cáo cần có khi tạo thực thể cho đối tượng quảng cáo.

Java

package com.google.ads.mediation.sample.customevent;

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...

public class SampleRewardedCustomEventLoader extends SampleRewardedAdListener
    implements MediationRewardedAd {

  /** Configuration for requesting the rewarded ad from the third-party network. */
  private final MediationRewardedAdConfiguration mediationRewardedAdConfiguration;

  /**
   * A {@link MediationAdLoadCallback} that handles any callback when a Sample
   * rewarded ad finishes loading.
   */
  private final MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
      mediationAdLoadCallback;

  /** Callback for rewarded ad events. */
  private MediationRewardedAdCallback rewardedAdCallback;

  /** Constructor. */
  public SampleRewardedCustomEventLoader(
      @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
      @NonNull MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
              mediationAdLoadCallback) {
    this.mediationRewardedAdConfiguration = mediationRewardedAdConfiguration;
    this.mediationAdLoadCallback = mediationAdLoadCallback;
  }

  /** Loads the rewarded 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 AdMob UI when defining the custom event.
    Log.i("RewardedCustomEvent", "Begin loading rewarded ad.");
    String serverParameter = mediationRewardedAdConfiguration
        .getServerParameters()
        .getString(MediationConfiguration
        .CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
    Log.d("RewardedCustomEvent", "Received server parameter.");
    SampleAdRequest request = createSampleRequest(mediationRewardedAdConfiguration);
    sampleRewardedAd = new SampleRewardedAd(serverParameter);
    sampleRewardedAd.setListener(this);
    Log.i("RewardedCustomEvent", "Start fetching rewarded ad.");
    sampleRewardedAd.loadAd(request);
  }

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

Tuỳ thuộc vào việc quảng cáo được tìm nạp thành công hay gặp lỗi, bạn sẽ gọi onSuccess() hoặc onFailure(). onSuccess() được gọi bằng cách truyền vào một thực thể của lớp triển khai MediationRewardedAd.

Thông thường, các phương thức này được triển khai bên trong lệnh gọi lại từ SDK bên thứ ba mà bộ chuyển đổi của bạn triển khai. Trong ví dụ này, SDK mẫu có một SampleAdListener với các lệnh gọi lại có liên quan:

Java

@Override
public void onRewardedAdLoaded() {
  rewardedAdCallback = mediationAdLoadCallback.onSuccess(this);
}

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

MediationRewardedAd yêu cầu triển khai một phương thức showAd() để hiển thị quảng cáo:

Java

@Override
public void showAd(Context context) {
  if (!(context instanceof Activity)) {
    rewardedAdCallback.onAdFailedToShow(
        SampleCustomEventError.createCustomEventNoActivityContextError());
    return;
  }
  Activity activity = (Activity) context;

  if (!sampleRewardedAd.isAdAvailable()) {
    rewardedAdCallback.onAdFailedToShow(
        SampleCustomEventError.createCustomEventAdNotAvailableError());
    return;
  }
  sampleRewardedAd.showAd(activity);
}

Chuyển tiếp các sự kiện dàn xếp đến SDK quảng cáo trên thiết bị di động của Google

Sau khi onSuccess() được gọi, bộ chuyển đổi có thể sử dụng đối tượng MediationRewardedAdCallback được trả về để chuyển tiếp các sự kiện trình bày từ SDK bên thứ ba đến SDK quảng cáo trên thiết bị di động của Google. Lớp SampleRewardedCustomEventLoader mở rộng giao diện SampleAdListener để chuyển tiếp lệnh gọi lại từ mạng quảng cáo mẫu đến SDK quảng cáo trên thiết bị di động của Google.

Điều quan trọng là sự kiện tuỳ chỉnh của bạn phải chuyển tiếp nhiều lệnh gọi lại này nhất có thể để ứng dụng của bạn nhận được các sự kiện tương đương này từ SDK quảng cáo trên thiết bị di động của Google. Dưới đây là ví dụ về cách sử dụng lệnh gọi lại:

Java

@Override
public void onAdRewarded(final String rewardType, final int amount) {
  RewardItem rewardItem =
      new RewardItem() {
        @Override
        public String getType() {
          return rewardType;
        }

        @Override
        public int getAmount() {
          return amount;
        }
      };
  rewardedAdCallback.onUserEarnedReward(rewardItem);
}

@Override
public void onAdClicked() {
  rewardedAdCallback.reportAdClicked();
}

@Override
public void onAdFullScreen() {
  rewardedAdCallback.onAdOpened();
  rewardedAdCallback.onVideoStart();
  rewardedAdCallback.reportAdImpression();
}

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

@Override
public void onAdCompleted() {
  rewardedAdCallback.onVideoComplete();
}

Đến đây, bạn đã hoàn thành việc triển khai sự kiện tuỳ chỉnh cho quảng cáo có tặng thưởng. Bạn có thể xem toàn bộ ví dụ trên GitHub. Bạn có thể sử dụng đơn vị quảng cáo này với một mạng quảng cáo đã được hỗ trợ hoặc sửa đổi quảng cáo đó để hiển thị quảng cáo có tặng thưởng của sự kiện tuỳ chỉnh.