Interstitial con premio

Gli interstitial con premio sono un tipo di formato dell'annuncio incentivato che consente di offrire premi per gli annunci che vengono visualizzati automaticamente durante le naturali transizioni dell'app. A differenza degli annunci con premio, gli utenti non devono attivare la visualizzazione di questi annunci.

Prerequisiti

Implementazione

I passaggi principali per integrare gli annunci interstitial con premio sono i seguenti:

  • Carica un annuncio
  • Registrati per i callback degli eventi a schermo intero
  • Gestire il callback del premio
  • Visualizza l'annuncio
  • [Facoltativo] Convalidare i callback SSV

Carica un annuncio

Il caricamento di un annuncio viene eseguito utilizzando il metodo load() statico nella classe RewardedInterstitialAd. Il metodo di caricamento richiede un contesto, il tuo ID unità pubblicitaria, un oggetto AdRequest e un RewardedInterstitialAdLoadCallback per ricevere una notifica quando il caricamento dell'annuncio ha esito positivo o negativo. L'oggetto RewardedInterstitialAd caricato viene fornito come parametro nel callback onRewardedInterstitialAdLoaded().

L'esempio seguente mostra come caricare un elemento RewardedInterstitialAd in MainActivity.

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);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
      @Override
      public void onInitializationComplete(InitializationStatus initializationStatus) {
        loadAd();
      }
    });
  }

  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)

    MobileAds.initialize(this) { initializationStatus ->
      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
      }
    })
  }
}

Registrati per i callback

Per ricevere notifiche per gli eventi di presentazione, devi passare un oggetto FullScreenContentCallback all'autore dell'impostazione del tuo annuncio. L'oggetto FullScreenContentCallback gestisce i callback per quando l'annuncio viene presentato correttamente o meno e quando viene ignorato. Il seguente codice mostra come impostare un oggetto FullScreenContentCallback anonimo all'interno di 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
    }
  })
}

Gestire i premi

Per mostrare il tuo annuncio interstitial con premio, implementa l'interfaccia OnUserEarnedRewardListener in MainActivity per ricevere una notifica quando l'utente guadagna un premio.

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!
  }
}

Mostra l'annuncio

Dopo aver implementato l'interfaccia OnUserEarnedRewardListener, puoi presentare l'annuncio utilizzando il relativo metodo show() nel seguente modo:

Java

rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
    OnUserEarnedRewardListener */ MainActivity.this);

Kotlin

rewardedInterstitialAd?.show(/* Activity */ this, /*
    OnUserEarnedRewardListener */ this)

[Facoltativo] Convalidare i callback di verifica lato server (SSV)

Le app che richiedono dati aggiuntivi nei callback della verifica lato server dovrebbero utilizzare la funzionalità dei dati personalizzati degli annunci con premio. Qualsiasi valore di stringa impostato su un oggetto annuncio con premio viene trasmesso al parametro di query custom_data del callback SSV. Se non è impostato alcun valore dei dati personalizzati, il valore del parametro di query custom_data non sarà presente nel callback della SSV.

Il seguente esempio di codice mostra come impostare i dati personalizzati su un oggetto annuncio interstitial con premio prima di richiedere un annuncio.

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
  }
})

Se vuoi impostare la stringa premio personalizzata, devi farlo prima di mostrare l'annuncio.

Esempi su GitHub

  • Esempio di annunci interstitial con premio: Java | Kotlin

Passaggi successivi

Esplora i seguenti argomenti: