必要條件
完成自訂事件設定。
索取獎勵廣告
當瀑布式中介鏈中到達自訂事件明細時,系統會根據您在建立自訂事件時提供的類別名稱,呼叫 loadRewardedAd()
方法。在本例中,該方法位於 SampleCustomEvent
中,然後呼叫 SampleRewardedCustomEventLoader
中的 loadRewardedAd()
方法。
如要要求獎勵廣告,請建立或修改可延伸 Adapter
的類別,以便實作 loadRewardedAd()
。此外,請建立新類別來實作 MediationRewardedAd
。
在自訂事件範例中,SampleCustomEvent
會擴充 Adapter
類別,然後委派至 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
介面。接收並回報廣告事件回呼至 Google Mobile Ads SDK
廣告設定中包含 Ad Manager 使用者介面中定義的選用參數。您可以透過 adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
存取此參數。這個參數通常是廣告聯播網 SDK 在例項化廣告物件時所需的廣告單元 ID。
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 回呼中實作。在本例中,Sample 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 上取得完整範例。您可以將其與已支援的廣告聯播網搭配使用,也可以修改該廣告活動,以便顯示自訂事件獎勵廣告。