前提条件
カスタム イベントのセットアップを完了しておきます。
リワード広告をリクエストする
ウォーターフォール メディエーションの順序がカスタム イベントの広告申込情報に回ってくると、
カスタム イベントの作成時に指定したクラス名で
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 (Legacy) に報告する。
アド マネージャーの管理画面で定義された省略可能なパラメータは、広告構成に含まれます。このパラメータには、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 が「Sample 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 (Legacy) に転送する
onSuccess() が呼び出されると、返された MediationRewardedAdCallback
オブジェクトをアダプタが使用することにより、プレゼンテーション イベントを
サードパーティ SDK から Google Mobile Ads SDK (Legacy) に転送できます。SampleRewardedCustomEventLoader クラスは SampleAdListener
インターフェースを拡張して、サンプル広告ネットワークからのコールバックを Google Mobile Ads SDK に転送します。
カスタム イベントには、こうしたコールバックを可能な限り漏れなく転送させましょう。これにより、アプリは同等イベントを から受け取ることができます。 Google Mobile Ads SDK (Legacy)コールバックの使用例を以下に示します。
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で入手できます。 このサンプルは、すでにサポートされている広告ネットワークの実装例として利用できるほか、必要に応じてコードをカスタマイズし、独自のリワード広告用カスタム イベントを表示するために利用することも可能です。