इनाम वाले विज्ञापनों के कस्टम इवेंट

ज़रूरी शर्तें

कस्टम इवेंट का सेटअप पूरा करें.

इनाम वाले विज्ञापन का अनुरोध करना

वॉटरफ़ॉल मीडिएशन चेन में कस्टम इवेंट लाइन आइटम के पहुंचने पर,the loadRewardedAd() method को उस क्लास के नाम पर कॉल किया जाता है जो आपने कस्टम इवेंट बनाते समय दिया था. इस मामले में, वह तरीका SampleCustomEvent में है, जिसके बादthe loadRewardedAd() method में SampleRewardedCustomEventLoaderकॉल करेगा.

इनाम वाले विज्ञापन का अनुरोध करने के लिए, Adapter को बढ़ाने वाली क्लास बनाएं या उसमें बदलाव करके loadRewardedAd() को लागू करें. इसके अलावा, MediationRewardedAd को लागू करने के लिए एक नई क्लास बनाएं.

कस्टम इवेंट के हमारे उदाहरण में, SampleCustomEvent इसे लागू करता है the Adapter interface और फिर इसे SampleRewardedCustomEventLoaderमें सौंपता है.

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 इन टास्क के लिए ज़िम्मेदार हैं:

  • इनाम वाला विज्ञापन लोड हो रहा है

  • MediationRewardedAd interfaceको लागू करना

  • Google Mobile Ads SDK पर विज्ञापन इवेंट कॉलबैक पाना और उन्हें रिपोर्ट करना

Ad Manager यूज़र इंटरफ़ेस (यूआई) में तय किया गया वैकल्पिक पैरामीटर, विज्ञापन कॉन्फ़िगरेशन में शामिल है. इस पैरामीटर को 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.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;
  }
}

विज्ञापन फ़ेच हो गया है या उसमें कोई गड़बड़ी मिली है या नहीं, इसके आधार पर onSuccess() या onFailure() में से किसी एक को कॉल करें. MediationRewardedAd को लागू करने वाली क्लास के इंस्टेंस में पास होने पर onSuccess() कॉल किया जाता है.

आम तौर पर, इन तरीकों को तीसरे पक्ष के SDK टूल से कॉलबैक में लागू किया जाता है. यह SDK टूल, आपका अडैप्टर इस्तेमाल करता है. इस उदाहरण के लिए, सैंपल SDK टूल में काम के कॉलबैक के साथ एक SampleAdListener है:

Java

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

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

MediationRewardedAd को विज्ञापन दिखाने के लिए showAd() तरीका लागू करना ज़रूरी है:

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

Google Mobile Ads SDK पर मीडिएशन इवेंट फ़ॉरवर्ड करना

onSuccess() को कॉल करने के बाद, दिखाए गए MediationRewardedAdCallback ऑब्जेक्ट का इस्तेमाल करके अडैप्टर, प्रज़ेंटेशन के इवेंट को तीसरे पक्ष के SDK टूल से Google Mobile Ads SDK पर फ़ॉरवर्ड कर सकता है. SampleRewardedCustomEventLoader क्लास, SampleAdListener इंटरफ़ेस को बढ़ाती है. ऐसा करके, सैंपल विज्ञापन नेटवर्क से कॉलबैक को, Google Mobile Ads SDK पर फ़ॉरवर्ड किया जाता है.

आपके कस्टम इवेंट से ज़्यादा से ज़्यादा कॉलबैक फ़ॉरवर्ड करने चाहिए, ताकि आपके ऐप्लिकेशन को Google Mobile Ads SDK से मिलते-जुलते इवेंट मिल सकें. यहां कॉलबैक इस्तेमाल करने का एक उदाहरण दिया गया है:

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

इससे, इनाम वाले विज्ञापनों के लिए कस्टम इवेंट लागू हो जाते हैं. इसका पूरा उदाहरण GitHub पर उपलब्ध है. इसे पहले से काम करने वाली किसी विज्ञापन नेटवर्क कंपनी के साथ इस्तेमाल किया जा सकता है. इसके अलावा, कस्टम इवेंट के इनाम वाले विज्ञापनों को दिखाने के लिए, इस नेटवर्क में बदलाव किया जा सकता है.