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.
Neste guia, explicamos como integrar anúncios intersticiais premiados a um app Android.
Antes de começar
Antes de continuar, faça o seguinte:
- Configure o GMA Next-Gen SDK.
- Use o ID do bloco de anúncios intersticiais premiados de teste
/21775744923/example/rewarded-interstitial.- Ao criar e testar seu app, use anúncios de teste em vez de anúncios de produção ativos. Se você não usar o ID do bloco de anúncios de teste, o Google poderá suspender sua conta.
- Antes de publicar o app, substitua esse ID pelo ID do seu bloco de anúncios.
- Para detalhes sobre os anúncios de teste do GMA Next-Gen SDK, consulte Ativar anúncios de teste.
Carregar um anúncio
O exemplo a seguir carrega 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, chame o métodoshow. Para processar eventos premiados, use um objeto OnUserEarnedRewardListener.
O exemplo a seguir mostra o anúncio:
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(); } }); }
Detectar eventos de anúncio
Antes de veicular o anúncio, acompanhe os eventos de ciclo de vida dele. O exemplo a seguir registra callbacks para eventos de 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)
Para transmitir o contexto do usuário e do aplicativo em callbacks de verificação do lado do servidor (SSV), defina as opções de verificação no objeto de anúncio antes de mostrá-lo. Como os callbacks de SSV chegam ao seu servidor do Google sem estado de sessão, anexe as seguintes strings opcionais. O Google encaminha essas strings sem alterações como parâmetros de consulta assinados no callback de SSV:
- Parâmetro de consulta
user_id: identifica o usuário que vai receber o crédito, como um ID da conta interna. - Parâmetro de consulta
custom_data: transporta dados do app, como o posicionamento da recompensa, o nível do jogo ou um valor de uso único gerado pelo servidor.
Se você não definir um valor, o Google vai omitir esse parâmetro de consulta do callback de SSV.
O exemplo a seguir define dados personalizados em um objeto de anúncio antes de mostrá-lo:
Kotlin
Java
Substitua SAMPLE_CUSTOM_DATA_STRING pelos seus dados personalizados.
Importante: o parâmetro de consulta custom_data é
codificado por porcentagem. Antes de analisar o
valor, decodifique o URL da string no servidor e verifique a assinatura de callback
antes de confiar nos parâmetros de consulta user_id ou custom_data.