API ของ WebView สำหรับโฆษณาจะทำให้สัญญาณของแอปพร้อมใช้งานสำหรับแท็กใน CustomTabSession ซึ่งจะช่วยปรับปรุงการสร้างรายได้สำหรับผู้เผยแพร่โฆษณาบนเว็บที่ให้เนื้อหาและปกป้องผู้ลงโฆษณาจากสแปม
วิธีการทำงาน
การสื่อสารกับ Google Mobile Ads SDK (Legacy) จะเกิดขึ้นเมื่อมีการตอบสนองต่อเหตุการณ์โฆษณาที่ทริกเกอร์โดยรายการต่อไปนี้เท่านั้น
Google Mobile Ads SDK (Legacy) ช่วยอำนวยความสะดวกในการสื่อสารโดยเปิดช่องทาง postMessage ด้วย CustomTabsSession ที่เฟรมเวิร์ก Android มีให้
ข้อกำหนดเบื้องต้น
- Google Mobile Ads SDK (Legacy) 23.0.0 ขึ้นไป
- การติดตั้งใช้งาน Chrome Custom Tabs ที่มีอยู่ในแอปบนอุปกรณ์เคลื่อนที่ ดูการเตรียมพร้อมและการดึงข้อมูลล่วงหน้า: การใช้บริการ Custom Tabs
- เชื่อมโยงแอปบนอุปกรณ์เคลื่อนที่กับเว็บไซต์โดยใช้ลิงก์เนื้อหาดิจิทัล โปรดดูวิธีการที่หัวข้อ PostMessage สำหรับ TWA
เพิ่มแท็ก
<meta-data>ต่อไปนี้ในไฟล์AndroidManifest.xmlเพื่อข้ามการตรวจสอบAPPLICATION_IDหากคุณข้ามขั้นตอนนี้และไม่ได้ระบุแท็ก<meta-data>Google Mobile Ads SDK (Legacy) จะแสดง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"/>
การใช้งาน
ในการติดตั้งใช้งานแท็บที่กำหนดเองของ Chrome ให้แก้ไขโค้ดใน Callback onCustomTabsServiceConnected() แทนที่ newSession() ด้วย MobileAds.registerCustomTabsSession() ซึ่งจะสร้างการเชื่อมต่อกับช่องทาง postMessage เพื่อเพิ่มสัญญาณ SDK ลงในแท็กโฆษณา ต้องใช้ Digital Asset Link เพื่อเชื่อมต่อช่องทาง postMessage คุณสามารถเลือกตั้งค่าพารามิเตอร์ CustomTabsCallback เพื่อฟังเหตุการณ์แท็บที่กำหนดเองของ Chrome ได้
คุณควรจะยังส่งข้อความจาก CustomTabsSession ที่ได้รับจาก Google Mobile Ads SDK (Legacy) ได้ แต่พอร์ตอาจเปลี่ยนแปลงเมื่อ SDK ขอช่องทางใหม่ที่ลบล้างช่องทางที่มีอยู่
ข้อมูลโค้ดต่อไปนี้แสดงวิธีลงทะเบียน CustomTabsSession โดยใช้ Google Mobile Ads SDK (Legacy)
Kotlin
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 (Legacy).
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 (Legacy) 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 (Legacy).
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.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 (Legacy).
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 (Legacy) 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 (Legacy).
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);
}
};
}