بینابینی پاداشدار نوعی قالب تبلیغاتی است که به شما امکان میدهد برای تبلیغاتی که بهطور خودکار در طول انتقال طبیعی برنامه ظاهر میشوند، پاداش ارائه دهید. برخلاف آگهیهای دارای پاداش، کاربران مجبور نیستند برای مشاهده یک بینابینی پاداشدار شرکت کنند.
پیش نیازها
- Google Mobile Ads SDK 19.2.0 یا بالاتر.
- راهنمای شروع را کامل کنید.
پیاده سازی
مراحل اولیه برای ادغام تبلیغات بینابینی دارای پاداش به شرح زیر است:
- یک تبلیغ را بارگیری کنید
- برای تماس های رویداد تمام صفحه ثبت نام کنید
- پاسخ به تماس پاداش را مدیریت کنید
- نمایش آگهی
یک تبلیغ را بارگیری کنید
بارگذاری یک تبلیغ با استفاده از روش load()
در کلاس RewardedInterstitialAd
انجام می شود. روش بارگیری به یک Context، شناسه واحد تبلیغات شما، یک شی AdManagerAdRequest
و یک RewardedInterstitialAdLoadCallback
نیاز دارد تا در صورت موفقیت یا ناموفق بودن بارگیری آگهی مطلع شوند. شیء بارگذاری شده RewardedInterstitialAd
به عنوان یک پارامتر در پاسخ به تماس onRewardedInterstitialAdLoaded()
ارائه می شود.
مثال زیر نحوه بارگیری RewardedInterstitialAd
را در MainActivity
خود نشان می دهد.
جاوا
کاتلین
شناسه واحد تبلیغات خود را جایگزین AD_UNIT_ID کنید.
برای پاسخ به تماس ها ثبت نام کنید
برای دریافت اعلانها برای رویدادهای ارائه، باید یک شی FullScreenContentCallback
را به تنظیمکننده آگهی خود ارسال کنید. شیء FullScreenContentCallback
برای زمانی که تبلیغ با موفقیت یا ناموفق نمایش داده می شود و زمانی که رد می شود، پاسخ تماس ها را کنترل می کند. کد زیر نحوه تنظیم یک شی FullScreenContentCallback
ناشناس را در RewardedInterstitialAdLoadCallback
خود نشان می دهد:
جاوا
rewardedInterstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d(TAG, "The ad was dismissed.");
// Make sure to set your reference to null so you don't
// show it a second time.
rewardedInterstitialAd = null;
if (googleMobileAdsConsentManager.canRequestAds()) {
loadRewardedInterstitialAd();
}
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "The ad failed to show.");
// Make sure to set your reference to null so you don't
// show it a second time.
rewardedInterstitialAd = null;
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "The ad was shown.");
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "The ad recorded an impression.");
}
@Override
public void onAdClicked() {
// Called when ad is clicked.
Log.d(TAG, "The ad was clicked.");
}
});
rewardedInterstitialAd.show(
MainActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.d(TAG, "The user earned the reward.");
// Handle the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
}
private void initializeMobileAdsSdk() {
if (isMobileAdsInitializeCalled.getAndSet(true)) {
return;
}
// Set your test devices.
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder()
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
.build());
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
// Load an ad on the main thread.
runOnUiThread(() -> loadRewardedInterstitialAd());
})
.start();
}
}
کاتلین
rewardedInterstitialAd?.fullScreenContentCallback =
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d(TAG, "Ad was dismissed.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "Ad failed to show.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedInterstitialAd = null
}
override fun onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdClicked() {
// Called when an ad is clicked.
Log.d(TAG, "Ad was clicked.")
}
}
آگهی را نمایش دهید
هنگامی که یک تبلیغ بینابینی پاداش نشان میدهید، از یک شی OnUserEarnedRewardListener
برای مدیریت رویدادهای پاداش استفاده میکنید.
جاوا
rewardedInterstitialAd.show(
MainActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.d(TAG, "The user earned the reward.");
// Handle the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
کاتلین
rewardedInterstitialAd?.show(this) { rewardItem ->
Log.d(TAG, "User earned the reward.")
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
}
نمونه هایی در GitHub
مراحل بعدی
موضوعات زیر را بررسی کنید: