L'interstitiel avec récompense est un type de format d'annonce incitatif qui vous permet de proposer des récompenses pour les annonces diffusées automatiquement lors des transitions naturelles dans les applications. Contrairement aux annonces avec récompense, les utilisateurs ne sont pas obligés de choisir de regarder un interstitiel avec récompense.
Prérequis
- SDK Google Mobile Ads 19.2.0 ou version ultérieure.
- Suivez le guide de démarrage.
Implémentation
Voici les principales étapes à suivre pour intégrer des annonces interstitielles avec récompense :
- Charger une annonce
- Enregistrer des rappels d'événements en plein écran
- Gérer le rappel de récompense
- Afficher l'annonce
Charger une annonce
Pour charger une annonce, la méthode statique load()
doit être utilisée sur la classe RewardedInterstitialAd
. La méthode de chargement nécessite un contexte, votre ID de bloc d'annonces, un objet AdManagerAdRequest
et un RewardedInterstitialAdLoadCallback
pour être averti lorsque le chargement de l'annonce réussit ou échoue. L'objet RewardedInterstitialAd
chargé est fourni en tant que paramètre dans le rappel onRewardedInterstitialAdLoaded()
.
L'exemple suivant montre comment charger un RewardedInterstitialAd
dans votre MainActivity
.
Java
Kotlin
Remplacez AD_UNIT_ID par l'ID de votre bloc d'annonces.
S'inscrire aux rappels
Pour recevoir des notifications d'événements de présentation, vous devez transmettre un objet FullScreenContentCallback
au setter de votre annonce. L'objet FullScreenContentCallback
gère les rappels lorsque l'annonce est présentée avec succès ou non, et lorsqu'elle est fermée. Le code suivant montre comment définir un objet FullScreenContentCallback
anonyme dans votre 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.")
}
}
Diffuser l'annonce
Lorsque vous diffusez une annonce interstitielle avec récompense, vous utilisez un objet OnUserEarnedRewardListener
pour gérer les événements de récompense.
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
}
Exemples sur GitHub
Étapes suivantes
Consultez les articles suivants :
- Confidentialité des données des utilisateurs
- Initialisation optimisée du SDK et chargement des annonces (bêta)