Os anúncios intersticiais premiados são um formato que permite oferecer recompensas em anúncios que aparecem automaticamente durante transições naturais do app. Diferente dos anúncios premiados, os usuários não precisam ativar a visualização dos intersticiais premiados.
Este guia explica como integrar anúncios intersticiais premiados a um app Android.
Pré-requisitos
Sempre usar anúncios de teste
Ao criar e testar seus apps, use anúncios de teste em vez de anúncios de produção ativos. Se você não usar anúncios de teste, o Google poderá suspender sua conta.
A maneira mais fácil de carregar anúncios de teste é usar o ID de bloco de anúncios de teste dedicado para anúncios intersticiais premiados do Android:
/21775744923/example/rewarded-interstitial
Esse ID de bloco de anúncios foi configurado especialmente para retornar anúncios de teste em todas as solicitações. Você pode usá-lo nos seus próprios apps durante a programação, os testes e a depuração. Não se esqueça de substituí-lo pelo ID do seu bloco de anúncios antes de publicar o app.
Para detalhes sobre os anúncios de teste GMA Next-Gen SDK, consulte Ativar anúncios de teste.
Carregar um anúncio
O exemplo a seguir mostra como carregar um único anúncio:
Kotlin
// Load ads after you initialize MobileAds. RewardedInterstitialAd.load( AdRequest.Builder(adUnitId).build(), object : AdLoadCallback<RewardedInterstitialAd> { override fun onAdLoaded(ad: RewardedInterstitialAd) { // Rewarded interstitial ad loaded. rewardedInterstitialAd = ad } override fun onAdFailedToLoad(adError: LoadAdError) { // Rewarded interstitial ad failed to load. Log.e(TAG, "Rewarded interstitial ad failed to load: ${adError.message}") rewardedInterstitialAd = null } }, )
Java
// Load ads after you initialize MobileAds. RewardedInterstitialAd.load( new AdRequest.Builder(adUnitId).build(), new AdLoadCallback<RewardedInterstitialAd>() { @Override public void onAdLoaded(@NonNull RewardedInterstitialAd ad) { // Rewarded interstitial ad loaded. rewardedInterstitialAd = ad; } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Rewarded interstitial ad failed to load. Log.e(TAG, "Rewarded interstitial ad failed to load: " + adError.getMessage()); rewardedInterstitialAd = null; } });
Ao testar, use o ID do bloco de anúncios de teste: /21775744923/example/rewarded-interstitial.
Mostrar o anúncio
Para mostrar um anúncio intersticial premiado, use o método show. Use um objeto OnUserEarnedRewardListener para processar eventos de recompensa.
Kotlin
private fun showAd(rewardedInterstitialAd: RewardedInterstitialAd, activity: Activity) { // Show the ad. rewardedInterstitialAd.show( activity, object : OnUserEarnedRewardListener { override fun onUserEarnedReward(rewardItem: RewardItem) { // User earned the reward. val rewardAmount = rewardItem.amount val rewardType = rewardItem.type } }, ) }
Java
private void showAd(RewardedInterstitialAd rewardedInterstitialAd, Activity activity) { // Show the ad. rewardedInterstitialAd.show( activity, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { // User earned the reward. int rewardAmount = rewardItem.getAmount(); String rewardType = rewardItem.getType(); } }); }
Acompanhar eventos de anúncios
Antes de mostrar o anúncio, acompanhe os eventos do ciclo de vida do anúncio:
Kotlin
private fun listenToAdEvents() { // Listen for ad events. val ad = rewardedInterstitialAd if (ad == null) { Log.e(TAG, "Rewarded interstitial ad is not ready yet.") return } ad.adEventCallback = object : RewardedInterstitialAdEventCallback { override fun onAdShowedFullScreenContent() { // Rewarded interstitial ad did show. } override fun onAdDismissedFullScreenContent() { // Rewarded interstitial ad did dismiss. rewardedInterstitialAd = null } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { // Rewarded interstitial ad failed to show. Log.e(TAG, "Rewarded interstitial ad failed to show: ${fullScreenContentError.message}") } override fun onAdImpression() { // Rewarded interstitial ad did record an impression. } override fun onAdClicked() { // Rewarded interstitial ad did record a click. } } }
Java
private void listenToAdEvents() { // Listen for ad events. if (rewardedInterstitialAd == null) { Log.e(TAG, "Rewarded interstitial ad is not ready yet."); return; } rewardedInterstitialAd.setAdEventCallback( new RewardedInterstitialAdEventCallback() { @Override public void onAdShowedFullScreenContent() { // Rewarded interstitial ad did show. } @Override public void onAdDismissedFullScreenContent() { // Rewarded interstitial ad did dismiss. rewardedInterstitialAd = null; } @Override public void onAdFailedToShowFullScreenContent( @NonNull FullScreenContentError fullScreenContentError) { // Rewarded interstitial ad failed to show. Log.e( TAG, "Rewarded interstitial ad failed to show: " + fullScreenContentError.getMessage()); } @Override public void onAdImpression() { // Rewarded interstitial ad did record an impression. } @Override public void onAdClicked() { // Rewarded interstitial ad did record a click. } }); }
Opcional: validar callbacks de verificação do lado do servidor (SSV)
Se o app exigir dados extras em callbacks de verificação do lado do servidor, use o recurso de dados personalizados dos anúncios intersticiais premiados. Qualquer valor de string definido em um objeto de anúncio intersticial premiado é transmitido ao parâmetro de consulta custom_data do callback de SSV. Se nenhum valor de dados personalizado for definido, o valor do parâmetro de consulta custom_data não estará presente no callback de SSV.
O exemplo de código a seguir mostra como definir dados personalizados em um objeto de anúncio intersticial premiado antes de solicitar um anúncio:
Kotlin
Java
Substitua SAMPLE_CUSTOM_DATA_STRING pelos seus dados personalizados.
Se você quiser definir a string de recompensa personalizada, faça isso antes de mostrar o anúncio.