Rewarded interstitial is a type of incentivized ad format that lets you offer rewards for ads that appear automatically during natural app transitions. Unlike rewarded ads, users aren't required to opt in to view a rewarded interstitial.
Prerequisites
- Google Mobile Ads SDK 19.2.0 or higher.
- Complete the Get started guide.
Implementation
The primary steps to integrate rewarded interstitial ads are as follows:
- Load an ad
- Register for full screen event callbacks
- Handle the reward callback
- Display the ad
[Optional] Validate SSV callbacks
โหลดโฆษณา
การโหลดโฆษณาจะดำเนินการได้โดยใช้เมธอด load()
แบบคงที่ในคลาส RewardedInterstitialAd
วิธีโหลดต้องมีบริบท รหัสหน่วยโฆษณา ออบเจ็กต์ AdRequest
และ RewardedInterstitialAdLoadCallback
เพื่อรับการแจ้งเตือนเมื่อการโหลดโฆษณาสำเร็จหรือล้มเหลว ออบเจ็กต์ RewardedInterstitialAd
ที่โหลดจะระบุเป็นพารามิเตอร์ใน Callback onRewardedInterstitialAdLoaded()
ตัวอย่างต่อไปนี้แสดงวิธีโหลด RewardedInterstitialAd
ใน MainActivity
Java
public class MainActivity extends AppCompatActivity {
private RewardedInterstitialAd rewardedInterstitialAd;
private String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
// Load an ad on the main thread.
runOnUiThread(
() -> {
loadAd();
});
})
.start();
}
public void loadAd() {
// Use the test ad unit ID to load an ad.
RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedInterstitialAd = ad;
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
}
}
Kotlin
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback
class MainActivity : AppCompactActivity() {
private var rewardedInterstitialAd? = null
private final var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MainActivity) {}
// Load an ad on the main thread.
runOnUiThread {
loadAd()
}
}
}
private fun loadAd() {
RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
Log.d(TAG, "Ad was loaded.")
rewardedInterstitialAd = ad
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
}
}
ลงทะเบียนสำหรับการติดต่อกลับ
คุณต้องส่งออบเจ็กต์ FullScreenContentCallback
ไปยังตัวตั้งค่าในโฆษณาเพื่อรับการแจ้งเตือนสำหรับเหตุการณ์การนำเสนอ ออบเจ็กต์ FullScreenContentCallback
จะจัดการ Callback เมื่อมีการนำเสนอสำเร็จหรือไม่สำเร็จ และเมื่อมีการปิด โค้ดต่อไปนี้แสดงวิธีตั้งค่าออบเจ็กต์ FullScreenContentCallback
ที่ไม่ระบุตัวตนภายใน RewardedInterstitialAdLoadCallback
ของคุณ
Java
public void loadAd(){
RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
rewardedInterstitialAd = ad;
rewardedInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedInterstitialAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedInterstitialAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
}
Kotlin
private fun loadAd() {
RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
rewardedInterstitialAd = ad
rewardedInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
override fun onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.")
rewardedInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
rewardedInterstitialAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
}
จัดการรางวัล
หากต้องการแสดงโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล ให้ใช้อินเทอร์เฟซ OnUserEarnedRewardListener
ใน MainActivity
เพื่อรับการแจ้งเตือนเมื่อผู้ใช้ได้รับรางวัล
Java
public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
...
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "User earned reward.");
// TODO: Reward the user!
}
}
Kotlin
class MainActivity : AppCompatActivity(), OnUserEarnedRewardListener {
...
override fun onUserEarnedReward(rewardItem: RewardItem) {
Log.d(TAG, "User earned reward.")
// TODO: Reward the user!
}
}
แสดงโฆษณา
หลังจากใช้อินเทอร์เฟซ OnUserEarnedRewardListener
แล้ว คุณจะนำเสนอโฆษณาได้โดยใช้เมธอด show()
ของโฆษณา ดังนี้
Java
rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
OnUserEarnedRewardListener */ MainActivity.this);
Kotlin
rewardedInterstitialAd?.show(/* Activity */ this, /*
OnUserEarnedRewardListener */ this)
[ไม่บังคับ] ตรวจสอบ Callback ของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)
แอปที่ต้องใช้ข้อมูลเพิ่มเติมในการเรียกกลับการยืนยันฝั่งเซิร์ฟเวอร์ควรใช้ฟีเจอร์ข้อมูลที่กำหนดเองของโฆษณาที่มีการให้รางวัล ระบบจะส่งค่าสตริงที่ตั้งค่าไว้ในออบเจ็กต์โฆษณาที่มีการให้รางวัลไปยังพารามิเตอร์การค้นหา custom_data
ของ Callback SSV หากไม่ได้กําหนดค่าข้อมูลที่กําหนดเอง ค่าพารามิเตอร์การค้นหา custom_data
จะไม่แสดงใน Callback SSV
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าข้อมูลที่กำหนดเองในออบเจ็กต์โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลก่อนส่งคำขอโฆษณา
Java
RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedInterstitialAd = ad;
ServerSideVerificationOptions options = new ServerSideVerificationOptions
.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build();
rewardedInterstitialAd.setServerSideVerificationOptions(options);
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
Kotlin
RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
Log.d(TAG, "Ad was loaded.")
rewardedInterstitialAd = ad
val options = ServerSideVerificationOptions.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build()
rewardedInterstitialAd.setServerSideVerificationOptions(options)
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
หากต้องการตั้งค่าสตริงรางวัลที่กำหนดเอง คุณต้องดำเนินการก่อนที่จะแสดงโฆษณา
ตัวอย่างใน GitHub
ขั้นตอนถัดไป
สำรวจหัวข้อต่อไปนี้