前提条件
カスタム イベントの設定を完了します。
リワード広告をリクエストする
ウォーターフォール メディエーション チェーンでカスタム イベント広告申込情報に達すると、カスタム イベントの作成時に指定したクラス名でthe loadRewardedAd()
method が呼び出されます。この場合、このメソッドは SampleCustomEvent
にあり、 SampleRewardedCustomEventLoader
のthe loadRewardedAd()
method を呼び出します。
リワード広告をリクエストするには、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 に報告する
UI で定義されたオプション パラメータは、 AdMob 広告設定に含まれます。このパラメータには 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()
を呼び出します。onSuccess()
は、MediationRewardedAd
を実装するクラスのインスタンスを渡すことで呼び出されます。
通常、これらのメソッドは、アダプタが実装するサードパーティ SDK のコールバック内に実装されます。この例では、関連するコールバックを含む SampleAdListener
がサンプル SDK に含まれています。
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 で入手できます。すでにサポートされている広告ネットワークで使用できます。または、カスタム イベントのリワード広告を表示するように変更することもできます。