APIهای نمای وب برای تبلیغات، سیگنالهای برنامه را در دسترس تگهای موجود در CustomTabSession شما قرار میدهند و به بهبود کسب درآمد برای ناشران وب که محتوا را ارائه دادهاند و محافظت از تبلیغکنندگان در برابر هرزنامه کمک میکنند.
چگونه کار میکند؟
ارتباط با SDK تبلیغات موبایلی گوگل فقط در پاسخ به رویدادهای تبلیغاتی ناشی از هر یک از موارد زیر اتفاق میافتد:
کیت توسعه نرمافزاری تبلیغات موبایل گوگل (SDK) با باز کردن یک کانال postMessage با CustomTabsSession ارائه شده توسط چارچوب اندروید، ارتباط را تسهیل میکند.
پیشنیازها
- کیت توسعه نرمافزاری تبلیغات موبایلی گوگل (SDK) نسخه ۲۳.۰.۰ یا بالاتر.
- پیادهسازی تبهای سفارشی کروم موجود در برنامه تلفن همراه شما. به بخش گرم کردن و پیشواکشی: استفاده از سرویس تبهای سفارشی مراجعه کنید.
- برنامه تلفن همراه خود را با استفاده از پیوندهای دارایی دیجیتال به یک وبسایت مرتبط کنید. برای دستورالعملها، به PostMessage برای TWA مراجعه کنید.
تگ
<meta-data>زیر را در فایلAndroidManifest.xmlخود اضافه کنید تا از بررسیAPPLICATION_IDعبور کنید. اگر این مرحله را از دست بدهید و تگ<meta-data>را ارائه ندهید، SDK تبلیغات موبایل گوگل در هنگام شروع برنامه، خطایIllegalStateExceptionرا صادر میکند.<!-- Bypass APPLICATION_ID check for web view APIs for ads --> <meta-data android:name="com.google.android.gms.ads.INTEGRATION_MANAGER" android:value="webview"/>
پیادهسازی
در پیادهسازی تبهای سفارشی کروم خود، کد موجود در فراخوانی onCustomTabsServiceConnected() را تغییر دهید. newSession() را با MobileAds.registerCustomTabsSession() جایگزین کنید. این کار ارتباطی با کانال postMessage برقرار میکند تا تگهای تبلیغاتی را با سیگنالهای SDK غنیسازی کند. برای اتصال کانال postMessage به یک Digital Asset Link نیاز است. به صورت اختیاری، میتوانید پارامتر CustomTabsCallback را طوری تنظیم کنید که به رویدادهای تب سفارشی کروم گوش دهد.
شما هنوز هم باید بتوانید از CustomTabsSession دریافت شده از SDK تبلیغات موبایل گوگل، پیام ارسال کنید، اما پورت میتواند زمانی که SDK درخواست کانال جدیدی را میدهد که کانال موجود را لغو میکند، تغییر کند.
قطعه کد زیر نحوه ثبت یک CustomTabsSession را با استفاده از SDK تبلیغات موبایلی گوگل نشان میدهد:
کاتلین
import com.google.android.gms.ads.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.
customTabsSession = MobileAds.registerCustomTabsSession(
this@MainActivity.applicationContext,
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 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.
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"
}
}
جاوا
import com.google.android.gms.ads.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.
customTabsSession = MobileAds.registerCustomTabsSession(
MainActivity.this.getApplicationContext(),
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 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.
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);
}
};
}