Mit den WebView-APIs für Anzeigen werden App-Signale für die Tags in Ihrem CustomTabSession
verfügbar gemacht. So können Webpublisher, die die Inhalte bereitgestellt haben, ihren Umsatz steigern und Werbetreibende werden vor Spam geschützt.
Funktionsweise
Die Kommunikation mit dem Google Mobile Ads SDK (Beta) erfolgt nur als Reaktion auf Anzeigenereignisse, die durch Folgendes ausgelöst werden:
Das Google Mobile Ads SDK (Beta) erleichtert die Kommunikation, indem ein postMessage-Channel mit dem CustomTabsSession
des Android-Frameworks geöffnet wird.
Vorbereitung
- Google Mobile Ads SDK (Beta) 0.6.0-alpha01 oder höher
- Eine vorhandene Implementierung von benutzerdefinierten Chrome-Tabs in Ihrer mobilen App. Weitere Informationen finden Sie unter Aufwärmen und Vorabruf: Verwenden des Custom Tabs Service.
- Verknüpfen Sie Ihre mobile App mit einer Website über Digital Asset Links. Eine Anleitung finden Sie unter PostMessage für TWA.
App-ID an das SDK übergeben
Wenn Sie bereits eine Ad Manager-Anwendungs-ID haben, initialisieren Sie das Google Mobile Ads SDK (Beta) mit Ihrer vorhandenen Anwendungs-ID.
Wenn Sie keine Ad Manager-Anwendungs-ID haben, übergeben Sie InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID
als Anwendungs-ID, wenn Sie das Google Mobile Ads SDK (Beta) initialisieren:
Kotlin
MobileAds.initialize(
this@MainActivity,
// Use this application ID to initialize the Google Mobile Ads SDK (beta) if
// you don't have an Ad Manager application ID.
InitializationConfig.Builder(InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID)
.build(),
) {
// Adapter initialization complete.
}
Java
MobileAds.initialize(
this,
// Use this application ID to initialize the Google Mobile Ads SDK (beta) if
// you don't have an Ad Manager application ID.
new InitializationConfig.Builder(InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID)
.build(),
initializationStatus -> {
// Adapter initialization is complete.
});
Implementierung
Ändern Sie in Ihrer Implementierung von benutzerdefinierten Chrome-Tabs den Code im onCustomTabsServiceConnected()
-Callback. Ersetzen Sie newSession()
durch MobileAds.registerCustomTabsSession()
. Dadurch wird eine Verbindung zum postMessage-Channel hergestellt, um die Anzeigen-Tags mit SDK-Signalen anzureichern. Zum Verbinden des postMessage-Channels ist ein Digital Asset Link erforderlich. Optional können Sie den Parameter CustomTabsCallback festlegen, um auf Ereignisse von benutzerdefinierten Chrome-Tabs zu warten.
Sie sollten weiterhin Nachrichten über den CustomTabsSession
senden können, der vom Google Mobile Ads SDK (Beta) empfangen wurde. Der Port kann sich jedoch ändern, wenn das SDK einen neuen Channel anfordert, der den vorhandenen überschreibt.
Das folgende Code-Snippet zeigt, wie Sie ein CustomTabsSession
-Objekt mit dem Google Mobile Ads SDK (Beta) registrieren:
Kotlin
import com.google.android.libraries.ads.mobile.sdk.MobileAds
class MainActivity : ComponentActivity() {
private var customTabsClient: CustomTabsClient? = null
private var customTabsSession: CustomTabsSession? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the default browser package name, this will be null if
// the default browser does not provide a CustomTabsService.
val packageName = CustomTabsClient.getPackageName(applicationContext, null);
if (packageName == null) {
// Do nothing as service connection is not supported.
return
}
CustomTabsClient.bindCustomTabsService(
applicationContext,
packageName,
object : CustomTabsServiceConnection() {
override fun onCustomTabsServiceConnected(
name: ComponentName, client: CustomTabsClient,
) {
customTabsClient = client
// Warm up the browser process.
customTabsClient?.warmup(0L)
// Create a new browser session using the Google Mobile Ads SDK (beta).
customTabsSession = MobileAds.INSTANCE.registerCustomTabsSession(
client,
// Checks the "Digital Asset Link" to connect the postMessage channel.
ORIGIN,
// Optional parameter to receive the delegated callbacks.
customTabsCallback
)
// Create a new browser session if the Google Mobile Ads SDK (beta) is
// unable to create one.
if (customTabsSession == null) {
customTabsSession = client.newSession(customTabsCallback)
}
// Pass the custom tabs session into the intent.
val customTabsIntent = CustomTabsIntent.Builder(customTabsSession).build()
customTabsIntent.launchUrl(this@MainActivity,
Uri.parse("YOUR_URL"))
}
override fun onServiceDisconnected(componentName: ComponentName) {
// Remove the custom tabs client and custom tabs session.
customTabsClient = null
customTabsSession = null
}
})
}
// Listen for events from the CustomTabsSession delegated by the Google Mobile Ads SDK (beta).
private val customTabsCallback: CustomTabsCallback = object : CustomTabsCallback() {
@Synchronized
override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
// Called when a navigation event happens.
}
@Synchronized
override fun onMessageChannelReady(extras: Bundle?) {
// Called when the channel is ready for sending and receiving messages on both
// ends.
// This frequently happens, such as each time the SDK requests a
// new channel.
}
@Synchronized
override fun onPostMessage(message: String, extras: Bundle?) {
// Called when a tab controlled by this CustomTabsSession has sent a postMessage.
}
override fun onRelationshipValidationResult(
relation: Int, requestedOrigin: Uri, result: Boolean, extras: Bundle?
) {
// Called when a relationship validation result is available.
}
override fun onActivityResized(height: Int, width: Int, extras: Bundle) {
// Called when the tab is resized.
}
override fun extraCallback(callbackName: String, args: Bundle?) {
}
override fun extraCallbackWithResult(callbackName: String, args: Bundle?): Bundle? {
return null
}
}
companion object {
// Replace this URL with an associated website.
const val ORIGIN = "https://www.google.com"
}
}
Java
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
class MainActivity extends ComponentActivity {
// Replace this URL with an associated website.
private static final String ORIGIN = "https://www.google.com";
private CustomTabsClient customTabsClient;
private CustomTabsSession customTabsSession;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the default browser package name, this will be null if
// the default browser does not provide a CustomTabsService.
String packageName = CustomTabsClient.getPackageName(getApplicationContext(), null);
if (packageName == null) {
// Do nothing as service connection is not supported.
return;
}
CustomTabsClient.bindCustomTabsService(
getApplicationContext(),
packageName,
new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(@NonNull ComponentName name,
@NonNull CustomTabsClient client) {
customTabsClient = client;
// Warm up the browser process.
customTabsClient.warmup(0);
// Create a new browser session using the Google Mobile Ads SDK (beta).
customTabsSession = MobileAds.INSTANCE.registerCustomTabsSession(
client,
// Checks the "Digital Asset Link" to connect the postMessage channel.
ORIGIN,
// Optional parameter to receive the delegated callbacks.
customTabsCallback);
// Create a new browser session if the Google Mobile Ads SDK (beta) is
// unable to create one.
if (customTabsSession == null) {
customTabsSession = client.newSession(customTabsCallback);
}
// Pass the custom tabs session into the intent.
CustomTabsIntent intent = new CustomTabsIntent.Builder(customTabsSession).build();
intent.launchUrl(MainActivity.this, Uri.parse("YOUR_URL"));
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// Remove the custom tabs client and custom tabs session.
customTabsClient = null;
customTabsSession = null;
}
}
);
}
// Listen for events from the CustomTabsSession delegated by the Google Mobile Ads SDK (beta).
private final CustomTabsCallback customTabsCallback = new CustomTabsCallback() {
@Override
public void onNavigationEvent(int navigationEvent, @Nullable Bundle extras) {
// Called when a navigation event happens.
super.onNavigationEvent(navigationEvent, extras);
}
@Override
public void onMessageChannelReady(@Nullable Bundle extras) {
// Called when the channel is ready for sending and receiving messages on both
// ends.
// This frequently happens, such as each time the SDK requests a
// new channel.
super.onMessageChannelReady(extras);
}
@Override
public void onPostMessage(@NonNull String message, @Nullable Bundle extras) {
// Called when a tab controlled by this CustomTabsSession has sent a postMessage.
super.onPostMessage(message, extras);
}
@Override
public void onRelationshipValidationResult(int relation, @NonNull Uri requestedOrigin,
boolean result, @Nullable Bundle extras) {
// Called when a relationship validation result is available.
super.onRelationshipValidationResult(relation, requestedOrigin, result, extras);
}
@Override
public void onActivityResized(int height, int width, @NonNull Bundle extras) {
// Called when the tab is resized.
super.onActivityResized(height, width, extras);
}
@Override
public void extraCallback(@NonNull String callbackName, @Nullable Bundle args) {
super.extraCallback(callbackName, args);
}
@Nullable
@Override
public Bundle extraCallbackWithResult(@NonNull String callbackName, @Nullable Bundle args) {
return super.extraCallbackWithResult(callbackName, args);
}
};
}