您可以利用自訂事件,為不受支援的廣告聯播網新增刊登序列中介服務。方法是為您要整合的廣告聯播網導入自訂事件轉接程式。
您可以在我們的 GitHub 存放區中找到完整的範例自訂事件專案。
必要條件
您必須先將下列其中一種廣告格式整合到應用程式中,才能建立自訂事件:
在 UI 中建立自訂事件
您必須先在 AdMob UI 中建立自訂事件。請參閱「新增自訂事件」一文中的操作說明。
您必須提供以下資訊:
- 類別名稱
實作自訂事件轉接程式的類別完整名稱,例如
com.google.ads.mediation.sample.customevent.SampleCustomEvent
。建議您針對所有自訂事件廣告格式使用單一轉接程式類別。- 標籤
定義廣告來源的專屬名稱。
- 參數
傳送至自訂事件轉接程式的選用字串引數。
初始化轉接器
Google Mobile Ads SDK 初始化時,會在 AdMob UI 中為應用程式設定的所有支援的第三方轉接程式和自訂事件上叫用 initialize()
。請使用這個方法,對自訂事件所需的第三方 SDK 執行所有必要的設定或初始化。
Java
package com.google.ads.mediation.sample.customevent;
import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.InitializationCompleteCallback;
import com.google.android.gms.ads.mediation.MediationConfiguration;
public class SampleAdNetworkCustomEvent extends Adapter {
private static final String SAMPLE_AD_UNIT_KEY = "parameter";
@Override
public void initialize(Context context,
InitializationCompleteCallback initializationCompleteCallback,
List<MediationConfiguration> mediationConfigurations) {
// This is where you will initialize the SDK that this custom
// event is built for. Upon finishing the SDK initialization,
// call the completion handler with success.
initializationCompleteCallback.onInitializationSucceeded();
}
}
Kotlin
package com.google.ads.mediation.sample.customevent
import com.google.android.gms.ads.mediation.Adapter
import com.google.android.gms.ads.mediation.InitializationCompleteCallback
import com.google.android.gms.ads.mediation.MediationConfiguration
class SampleCustomEvent : Adapter() {
private val SAMPLE_AD_UNIT_KEY = "parameter"
override fun initialize(
context: Context,
initializationCompleteCallback: InitializationCompleteCallback,
mediationConfigurations: List<MediationConfiguration>
) {
// This is where you will initialize the SDK that this custom
// event is built for. Upon finishing the SDK initialization,
// call the completion handler with success.
initializationCompleteCallback.onInitializationSucceeded()
}
}
回報版本號碼
所有自訂事件都必須向 Google Mobile Ads SDK 回報自訂事件轉接器本身的版本,以及自訂事件介面所連結的第三方 SDK 版本。版本會以 VersionInfo
物件回報:
Java
package com.google.ads.mediation.sample.customevent;
public class SampleCustomEvent extends Adapter {
@Override
public VersionInfo getVersionInfo() {
String versionString = new VersionInfo(1, 2, 3);
String[] splits = versionString.split("\\.");
if (splits.length >= 4) {
int major = Integer.parseInt(splits[0]);
int minor = Integer.parseInt(splits[1]);
int micro = Integer.parseInt(splits[2]) * 100 + Integer.parseInt(splits[3]);
return new VersionInfo(major, minor, micro);
}
return new VersionInfo(0, 0, 0);
}
@Override
public VersionInfo getSDKVersionInfo() {
String versionString = SampleAdRequest.getSDKVersion();
String[] splits = versionString.split("\\.");
if (splits.length >= 3) {
int major = Integer.parseInt(splits[0]);
int minor = Integer.parseInt(splits[1]);
int micro = Integer.parseInt(splits[2]);
return new VersionInfo(major, minor, micro);
}
return new VersionInfo(0, 0, 0);
}
}
Kotlin
package com.google.ads.mediation.sample.customevent
class SampleCustomEvent : Adapter() {
override fun getVersionInfo(): VersionInfo {
val versionString = VersionInfo(1,2,3).toString()
val splits: List<String> = versionString.split("\\.")
if (splits.count() >= 4) {
val major = splits[0].toInt()
val minor = splits[1].toInt()
val micro = (splits[2].toInt() * 100) + splits[3].toInt()
return VersionInfo(major, minor, micro)
}
return VersionInfo(0, 0, 0)
}
override fun getSDKVersionInfo(): VersionInfo {
val versionString = VersionInfo(1,2,3).toString()
val splits: List<String> = versionString.split("\\.")
if (splits.count() >= 3) {
val major = splits[0].toInt()
val minor = splits[1].toInt()
val micro = splits[2].toInt()
return VersionInfo(major, minor, micro)
}
return VersionInfo(0, 0, 0)
}
}
請求廣告
如要提出廣告要求,請參閱該廣告格式的操作說明: