激励广告自定义事件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
前提条件
完成自定义事件设置。
请求激励广告
在达到广告瀑布流中介链内的自定义事件订单项时,系统会对您在创建自定义事件时提供的类名称调用 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
负责执行以下任务:
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 具有一个 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);
}
调用 onSuccess()
后,适配器便可以使用返回的 MediationRewardedAdCallback
对象,将展示事件从第三方 SDK 转发至 Google 移动广告 SDK。SampleRewardedCustomEventLoader
类扩展了 SampleAdListener
接口,可将回调从示例广告联盟转发到 Google 移动广告 SDK。
您的自定义事件必须尽可能多地转发这些回调,以便您的应用从 Google 移动广告 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提供了完整的示例。您可在已获支持的广告联盟上直接使用这些示例代码,也可在修改后用于展示自定义事件激励广告。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eTo request a rewarded ad through a custom event, create a class extending \u003ccode\u003eAdapter\u003c/code\u003e and implement the \u003ccode\u003eloadRewardedAd()\u003c/code\u003e method, delegating the ad loading process to a separate loader class.\u003c/p\u003e\n"],["\u003cp\u003eThe loader class is responsible for loading the ad, implementing the \u003ccode\u003eMediationRewardedAd\u003c/code\u003e interface, and handling ad event callbacks.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful ad loading, call \u003ccode\u003eonSuccess()\u003c/code\u003e on the \u003ccode\u003eMediationAdLoadCallback\u003c/code\u003e, passing in the \u003ccode\u003eMediationRewardedAd\u003c/code\u003e implementation; on failure, call \u003ccode\u003eonFailure()\u003c/code\u003e with the error.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eMediationRewardedAd\u003c/code\u003e implementation must include a \u003ccode\u003eshowAd()\u003c/code\u003e method to display the ad when called.\u003c/p\u003e\n"],["\u003cp\u003eForward ad events from the third-party SDK to the Google Mobile Ads SDK using the \u003ccode\u003eMediationRewardedAdCallback\u003c/code\u003e to ensure consistent ad behavior and reporting.\u003c/p\u003e\n"]]],[],null,["Prerequisites\n\nComplete the [custom events setup](/ad-manager/mobile-ads-sdk/android/custom-events/setup).\n\nRequest a rewarded ad\n\nWhen the custom event line item is reached in the waterfall mediation chain,\nthe `loadRewardedAd()` method is called on the class name you\nprovided when [creating a custom\nevent](/ad-manager/mobile-ads-sdk/android/custom-events/setup#create). In this case,\nthat method is in `SampleCustomEvent`, which then calls the `loadRewardedAd()`\nmethod in `SampleRewardedCustomEventLoader`.\n\nTo request a rewarded ad, create or modify a class that extends `Adapter` to\nimplement `loadRewardedAd()`. Additionally, create a new class to implement\n`MediationRewardedAd`.\n\nIn our [custom event example](//github.com/googleads/googleads-mobile-android-mediation/blob/main/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent/SampleCustomEvent.java),\n`SampleCustomEvent` extends the `Adapter` class and then delegates to\n`SampleRewardedCustomEventLoader`. \n\nJava \n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationRewardedAd;\nimport com.google.android.gms.ads.mediation.MediationRewardedAdCallback;\n...\n\npublic class SampleCustomEvent extends Adapter {\n\n private SampleNativeCustomEventLoader nativeLoader;\n\n @Override\n public void loadRewardedAd(\n @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,\n @NonNull\n MediationAdLoadCallback\u003cMediationRewardedAd, MediationRewardedAdCallback\u003e\n mediationAdLoadCallback) {\n rewardedLoader =\n new SampleRewardedCustomEventLoader(\n mediationRewardedAdConfiguration, mediationAdLoadCallback);\n rewardedLoader.loadAd();\n }\n}\n```\n\n`SampleRewardedCustomEventLoader` is responsible for the following tasks:\n\n- Loading the rewarded ad\n\n- Implementing the `MediationRewardedAd` interface.\n\n- Receiving and reporting ad event callbacks to the Google Mobile Ads SDK\n\nThe optional parameter defined in the Ad Manager UI is\nincluded in the ad configuration. The parameter can be accessed through\n`adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)`.\nThis parameter is typically an ad unit identifier that an ad network SDK\nrequires when instantiating an ad object. \n\nJava \n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationRewardedAd;\nimport com.google.android.gms.ads.mediation.MediationRewardedAdCallback;\n...\n\npublic class SampleRewardedCustomEventLoader extends SampleRewardedAdListener\n implements MediationRewardedAd {\n\n /** Configuration for requesting the rewarded ad from the third-party network. */\n private final MediationRewardedAdConfiguration mediationRewardedAdConfiguration;\n\n /**\n * A {@link MediationAdLoadCallback} that handles any callback when a Sample\n * rewarded ad finishes loading.\n */\n private final MediationAdLoadCallback\u003cMediationRewardedAd, MediationRewardedAdCallback\u003e\n mediationAdLoadCallback;\n\n /** Callback for rewarded ad events. */\n private MediationRewardedAdCallback rewardedAdCallback;\n\n /** Constructor. */\n public SampleRewardedCustomEventLoader(\n @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,\n @NonNull MediationAdLoadCallback\u003cMediationRewardedAd, MediationRewardedAdCallback\u003e\n mediationAdLoadCallback) {\n this.mediationRewardedAdConfiguration = mediationRewardedAdConfiguration;\n this.mediationAdLoadCallback = mediationAdLoadCallback;\n }\n\n /** Loads the rewarded ad from the third-party ad network. */\n public void loadAd() {\n // All custom events have a server parameter named \"parameter\" that returns\n // back the parameter entered into the AdMob UI when defining the custom event.\n Log.i(\"RewardedCustomEvent\", \"Begin loading rewarded ad.\");\n String serverParameter = mediationRewardedAdConfiguration\n .getServerParameters()\n .getString(MediationConfiguration\n .CUSTOM_EVENT_SERVER_PARAMETER_FIELD);\n Log.d(\"RewardedCustomEvent\", \"Received server parameter.\");\n SampleAdRequest request = createSampleRequest(mediationRewardedAdConfiguration);\n sampleRewardedAd = new SampleRewardedAd(serverParameter);\n sampleRewardedAd.setListener(this);\n Log.i(\"RewardedCustomEvent\", \"Start fetching rewarded ad.\");\n sampleRewardedAd.loadAd(request);\n }\n\n public SampleAdRequest createSampleRequest(\n MediationAdConfiguration mediationAdConfiguration) {\n SampleAdRequest request = new SampleAdRequest();\n request.setTestMode(mediationAdConfiguration.isTestRequest());\n request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());\n return request;\n }\n}\n```\n\nDepending on whether the ad is successfully fetched or encounters an error, you\nwould call either\n[`onSuccess()`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onSuccess(MediationAdT))\nor\n[`onFailure()`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onFailure(com.google.android.gms.ads.AdError)).\n`onSuccess()` is called by passing in an instance of the class that implements\n`MediationRewardedAd`.\n\nTypically, these methods are implemented inside callbacks from the\nthird-party SDK your adapter implements. For this example, the Sample SDK\nhas a `SampleAdListener` with relevant callbacks: \n\nJava \n\n```java\n@Override\npublic void onRewardedAdLoaded() {\n rewardedAdCallback = mediationAdLoadCallback.onSuccess(this);\n}\n\n@Override\npublic void onRewardedAdFailedToLoad(SampleErrorCode errorCode) {\n mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));\n}\n```\n\n`MediationRewardedAd` requires implementing a `showAd()` method to display\nthe ad: \n\nJava \n\n```java\n@Override\npublic void showAd(Context context) {\n if (!(context instanceof Activity)) {\n rewardedAdCallback.onAdFailedToShow(\n SampleCustomEventError.createCustomEventNoActivityContextError());\n return;\n }\n Activity activity = (Activity) context;\n\n if (!sampleRewardedAd.isAdAvailable()) {\n rewardedAdCallback.onAdFailedToShow(\n SampleCustomEventError.createCustomEventAdNotAvailableError());\n return;\n }\n sampleRewardedAd.showAd(activity);\n}\n```\n\nForward mediation events to the Google Mobile Ads SDK\n\nOnce `onSuccess()` is called, the returned `MediationRewardedAdCallback`\nobject can then be used by the adapter to forward presentation events from the\nthird-party SDK to the Google Mobile Ads SDK. The\n`SampleRewardedCustomEventLoader` class extends the `SampleAdListener`\ninterface to forward callbacks from the sample ad network to the Google Mobile\nAds SDK.\n\nIt's important that your custom event forwards as many of these callbacks as\npossible, so that your app receives these equivalent events from the Google\nMobile Ads SDK. Here's an example of using callbacks: \n\nJava \n\n```java\n@Override\npublic void onAdRewarded(final String rewardType, final int amount) {\n RewardItem rewardItem =\n new RewardItem() {\n @Override\n public String getType() {\n return rewardType;\n }\n\n @Override\n public int getAmount() {\n return amount;\n }\n };\n rewardedAdCallback.onUserEarnedReward(rewardItem);\n}\n\n@Override\npublic void onAdClicked() {\n rewardedAdCallback.reportAdClicked();\n}\n\n@Override\npublic void onAdFullScreen() {\n rewardedAdCallback.onAdOpened();\n rewardedAdCallback.onVideoStart();\n rewardedAdCallback.reportAdImpression();\n}\n\n@Override\npublic void onAdClosed() {\n rewardedAdCallback.onAdClosed();\n}\n\n@Override\npublic void onAdCompleted() {\n rewardedAdCallback.onVideoComplete();\n}\n```\n\nThis completes the custom events implementation for rewarded ads. The full\nexample is available on\n[GitHub](//github.com/googleads/googleads-mobile-android-mediation/tree/master/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent).\nYou can use it with an ad network that is already supported or modify it to\ndisplay custom event rewarded ads."]]