L'interstitial con premio è un tipo di formato dell'annuncio con incentivi che ti consente di offrire premi per gli annunci che vengono visualizzati automaticamente durante le normali transizioni dell'app. A differenza degli annunci con premio, gli utenti non devono attivare la visualizzazione di un annuncio interstitial con premio.
Prerequisiti
- SDK Google Mobile Ads 19.2.0 o versioni successive.
- Completa la Guida introduttiva.
Implementazione
I passaggi principali per integrare gli annunci interstitial con premio sono i seguenti:
- Caricare un annuncio
- Registrarsi ai callback degli eventi a schermo intero
- Gestire il callback del premio
- Visualizzare l'annuncio
Caricare un annuncio
Il caricamento di un annuncio viene eseguito utilizzando il metodo statico load() nella classe RewardedInterstitialAd. Il metodo di caricamento richiede un contesto, l'ID unità pubblicitaria, un oggetto AdManagerAdRequest e un RewardedInterstitialAdLoadCallback da notificare quando il caricamento dell'annuncio ha esito positivo o negativo. L'oggetto RewardedInterstitialAd caricato viene fornito come parametro nel
callback onRewardedInterstitialAdLoaded().
Il seguente esempio mostra come caricare un RewardedInterstitialAd nel tuo
MainActivity.
Java
Kotlin
Sostituisci AD_UNIT_ID con l'ID unità pubblicitaria.
Registrarsi per i callback
Per ricevere notifiche per gli eventi di presentazione, devi passare un
oggetto FullScreenContentCallback al setter dell'annuncio. L'oggetto
FullScreenContentCallback gestisce i callback per quando l'annuncio viene visualizzato
correttamente o meno e quando viene chiuso. Il seguente codice
mostra come impostare un oggetto FullScreenContentCallback anonimo all'interno del
RewardedInterstitialAdLoadCallback:
Java
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();
}
}
Kotlin
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.")
}
}
Mostra l'annuncio
Quando mostri un annuncio interstitial con premio, utilizzi un
oggetto OnUserEarnedRewardListener per gestire gli eventi premio.
Java
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();
}
});
Kotlin
rewardedInterstitialAd?.show(this) { rewardItem ->
Log.d(TAG, "User earned the reward.")
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
}
Esempi su GitHub
Passaggi successivi
Esplora i seguenti argomenti: