বিজ্ঞাপনের জন্য ওয়েব ভিউ APIগুলি আপনার CustomTabSession
এ ট্যাগগুলিতে অ্যাপ্লিকেশান সংকেতগুলি উপলব্ধ করে, যা সামগ্রী সরবরাহকারী ওয়েব প্রকাশকদের নগদীকরণ উন্নত করতে এবং বিজ্ঞাপনদাতাদের স্প্যাম থেকে রক্ষা করতে সহায়তা করে৷
এটা কিভাবে কাজ করে
Google মোবাইল বিজ্ঞাপন SDK-এর সাথে যোগাযোগ শুধুমাত্র নিম্নলিখিতগুলির যেকোনো একটি দ্বারা ট্রিগার করা বিজ্ঞাপন ইভেন্টগুলির প্রতিক্রিয়া হিসাবে ঘটে:
Google মোবাইল বিজ্ঞাপন SDK অ্যান্ড্রয়েড ফ্রেমওয়ার্ক দ্বারা প্রদত্ত CustomTabsSession
সহ একটি পোস্টমেসেজ চ্যানেল খোলার মাধ্যমে যোগাযোগের সুবিধা দেয়৷
পূর্বশর্ত
- Google মোবাইল বিজ্ঞাপন SDK 23.0.0 বা উচ্চতর।
- আপনার মোবাইল অ্যাপে বিদ্যমান Chrome কাস্টম ট্যাব বাস্তবায়ন। ওয়ার্ম-আপ এবং প্রি-ফেচ দেখুন: কাস্টম ট্যাব পরিষেবা ব্যবহার করে ।
- ডিজিটাল অ্যাসেট লিংক সহ আপনার মোবাইল অ্যাপকে একটি ওয়েবসাইটে সংযুক্ত করুন।
APPLICATION_ID
এর চেক বাইপাস করতে আপনারAndroidManifest.xml
ফাইলে নিম্নলিখিত<meta-data>
ট্যাগ যোগ করুন। আপনি যদি এই ধাপটি মিস করেন এবং<meta-data>
ট্যাগ প্রদান না করেন, Google Mobile Ads 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"/>
বাস্তবায়ন
আপনার Chrome কাস্টম ট্যাব বাস্তবায়নের মধ্যে, onCustomTabsServiceConnected()
কলব্যাকে কোডটি পরিবর্তন করুন। newSession()
MobileAds.registerCustomTabsSession()
দিয়ে প্রতিস্থাপন করুন। এটি SDK সংকেত সহ বিজ্ঞাপন ট্যাগগুলিকে সমৃদ্ধ করতে পোস্টমেসেজ চ্যানেলের সাথে একটি সংযোগ স্থাপন করে। পোস্টমেসেজ চ্যানেল সংযোগ করার জন্য একটি ডিজিটাল সম্পদ লিঙ্ক প্রয়োজন। ঐচ্ছিকভাবে, আপনি Chrome কাস্টম ট্যাব ইভেন্টগুলি শোনার জন্য CustomTabsCallback প্যারামিটার সেট করতে পারেন৷
আপনি এখনও Google মোবাইল বিজ্ঞাপন SDK থেকে প্রাপ্ত CustomTabsSession
থেকে বার্তা পাঠাতে সক্ষম হবেন কিন্তু SDK একটি নতুন চ্যানেলের অনুরোধ করলে পোর্টটি পরিবর্তন হতে পারে যা বিদ্যমানটিকে ওভাররাইড করে।
নিম্নলিখিত কোড স্নিপেট দেখায় কিভাবে Google মোবাইল বিজ্ঞাপন SDK ব্যবহার করে একটি CustomTabsSession
নিবন্ধন করতে হয়:
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);
}
};
}