Interstisial reward adalah jenis format iklan berinsentif yang memungkinkan Anda menawarkan reward untuk iklan yang muncul secara otomatis selama transisi aplikasi yang alami. Berbeda dengan iklan reward, pengguna tidak diwajibkan untuk memilih melihat interstisial reward.
Prasyarat
* Google Mobile Ads SDK 19.2.0 atau yang lebih tinggi.- Selesaikan Panduan memulai.
Penerapan
Langkah-langkah utama untuk mengintegrasikan iklan interstisial reward adalah sebagai berikut:
- Memuat iklan
- Mendaftar untuk callback peristiwa layar penuh
- Menangani callback reward
- Menampilkan iklan
- [Opsional] Memvalidasi callback SSV
Memuat iklan
Pemuatan iklan dilakukan menggunakan metode load()
statis pada
class RewardedInterstitialAd
. Metode pemuatan memerlukan Context, ID unit iklan, objek AdRequest
, dan RewardedInterstitialAdLoadCallback
untuk diberi tahu saat pemuatan iklan berhasil atau gagal. Objek RewardedInterstitialAd
yang dimuat disediakan sebagai parameter dalam
callback onRewardedInterstitialAdLoaded()
.
Contoh berikut menunjukkan cara memuat RewardedInterstitialAd
di
MainActivity
Anda.
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
}
})
}
}
Mendaftar untuk callback
Untuk menerima notifikasi peristiwa presentasi, Anda harus meneruskan
objek FullScreenContentCallback
ke penyetel di iklan. Objek
FullScreenContentCallback
menangani callback saat iklan ditampilkan
dengan berhasil atau gagal, dan saat iklan ditutup. Kode berikut
menunjukkan cara menetapkan objek FullScreenContentCallback
anonim dalam
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
}
})
}
Menangani reward
Untuk menampilkan iklan interstisial reward, terapkan
antarmuka OnUserEarnedRewardListener
di MainActivity
, agar diberi tahu
saat pengguna memperoleh reward.
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!
}
}
Menampilkan iklan
Setelah menerapkan antarmuka OnUserEarnedRewardListener
, Anda dapat menampilkan
iklan menggunakan metode show()
iklan seperti ini:
Java
rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
OnUserEarnedRewardListener */ MainActivity.this);
Kotlin
rewardedInterstitialAd?.show(/* Activity */ this, /*
OnUserEarnedRewardListener */ this)
[Opsional] Memvalidasi callback verifikasi sisi server (SSV)
Aplikasi yang memerlukan data tambahan dalam callback verifikasi
sisi server harus menggunakan
fitur data kustom iklan reward. Setiap nilai string yang ditetapkan pada objek iklan reward
diteruskan ke parameter kueri custom_data
dari callback SSV. Jika tidak ada nilai data kustom yang ditetapkan, nilai parameter kueri custom_data
tidak akan ada dalam callback SSV.
Contoh kode berikut menunjukkan cara menetapkan data kustom pada objek iklan interstisial reward sebelum meminta iklan.
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
}
})
Jika ingin menetapkan string reward kustom, Anda harus melakukannya sebelum menampilkan iklan.
Contoh di GitHub
Langkah berikutnya
Pelajari topik berikut: