リワード インタースティシャルは、アプリの画面が変わる自然なタイミングで自動的に表示される広告に対して報酬を提供できる、インセンティブ広告フォーマットの一種です。リワード広告とは異なり、ユーザーはリワード インタースティシャルを表示するためにオプトインする必要はありません。
前提条件
- スタートガイドの手順を完了していること
必ずテスト広告でテストする
アプリの開発とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。
下記の Android リワード インタースティシャル広告向けのテスト専用広告ユニット ID を使うと、テスト広告を簡単に読み込むことができます。
ca-app-pub-3940256099942544/5354046379
この ID は、すべてのリクエストに対してテスト広告を返すように設定されており、アプリのコーディング、テスト、デバッグで自由に使用することができます。なお、アプリを公開する前に、必ずテスト用 ID をご自身の広告ユニット ID に置き換えてください。
GMA 次世代 SDK のテスト広告の仕組みについて詳しくは、テスト広告をご覧ください。
広告を読み込む
広告を読み込むために、GMA 次世代 SDK では次の機能が提供されています。
単一広告読み込み API で読み込みます。
広告プリロード API を使用して読み込みます。これにより、広告の手動読み込みとキャッシュ保存が不要になります。
単一の広告読み込み API で読み込む
次の例は、単一の広告を読み込む方法を示しています。
Kotlin
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAdEventCallback
import com.google.android.libraries.ads.mobile.sdk.MobileAds
class RewardedInterstitialActivity : Activity() {
private var rewardedInterstitialAd: RewardedInterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Load ads after you initialize GMA Next Gen SDK.
RewardedInterstitialAd.load(
AdRequest.Builder(AD_UNIT_ID).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.
rewardedInterstitialAd = null
}
},
)
}
companion object {
// Sample rewarded interstitial ad unit ID.
const val AD_UNIT_ID = "ca-app-pub-3940256099942544/5354046379"
}
}
Java
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback;
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest;
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError;
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError;
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAdEventCallback;
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
class RewardedActivity extends Activity {
// Sample rewarded interstitial ad unit ID.
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5354046379";
private RewardedInterstitialAd rewardedInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load ads after you initialize GMA Next Gen SDK.
RewardedInterstitialAd.load(
new AdRequest.Builder(AD_UNIT_ID).build(),
new AdLoadCallback<RewardedInterstitialAd>() {
@Override
public void onAdLoaded(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
// Rewarded interstitial ad loaded.
AdLoadCallback.super.onAdLoaded(rewardedInterstitialAd);
RewardedActivity.this.rewardedInterstitialAd = rewardedInterstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError adError) {
// Rewarded interstitial ad failed to load.
AdLoadCallback.super.onAdFailedToLoad(adError);
rewardedInterstitialAd = null;
}
}
);
}
}
広告プリロード API で読み込む
プリロードを開始する手順は次のとおりです。
広告リクエストでプリロード構成を初期化します。
広告ユニット ID とプリロード構成を使用して、リワード インタースティシャル広告のプリローダーを開始します。
Kotlin
private fun startPreloading(adUnitId: String) {
val adRequest = AdRequest.Builder(adUnitId).build()
val preloadConfig = PreloadConfiguration(adRequest)
RewardedInterstitialAdPreloader.start(adUnitId, preloadConfig)
}
Java
private void startPreloading(String adUnitId) {
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
RewardedInterstitialAdPreloader.start(adUnitId, preloadConfig);
}
広告を表示する準備ができたら、プリローダーから広告をポーリングします。
Kotlin
// Polling returns the next available ad and loads another ad in the background.
val ad = RewardedInterstitialAdPreloader.pollAd(adUnitId)
Java
// Polling returns the next available ad and loads another ad in the background.
final RewardedInterstitialAd ad = RewardedInterstitialAdPreloader.pollAd(adUnitId);
RewardedInterstitialAdEventCallback を設定する
RewardedInterstitialAdEventCallback は、RewardedInterstitialAd の表示に関連するイベントを処理します。リワード インタースティシャル広告を表示する前に、コールバックを設定してください。
Kotlin
// Listen for ad events.
rewardedInterstitialAd?.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.
rewardedInterstitialAd = null
}
override fun onAdImpression() {
// Rewarded interstitial ad did record an impression.
}
override fun onAdClicked() {
// Rewarded interstitial ad did record a click.
}
}
Java
// Listen for ad events.
rewardedInterstitialAd.setAdEventCallback(
new RewardedInterstitialAdEventCallback() {
@Override
public void onAdShowedFullScreenContent() {
// Rewarded interstitial ad did show.
RewardedInterstitialAdEventCallback.super.onAdShowedFullScreenContent();
}
@Override
public void onAdDismissedFullScreenContent() {
// Rewarded interstitial ad did dismiss.
RewardedInterstitialAdEventCallback.super.onAdDismissedFullScreenContent();
rewardedInterstitialAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(
@NonNull FullScreenContentError fullScreenContentError) {
// Rewarded interstitial ad failed to show.
RewardedInterstitialAdEventCallback.super.onAdFailedToShowFullScreenContent(
fullScreenContentError);
rewardedInterstitialAd = null;
}
@Override
public void onAdImpression() {
// Rewarded interstitial ad did record an impression.
RewardedInterstitialAdEventCallback.super.onAdImpression();
}
@Override
public void onAdClicked() {
// Rewarded interstitial ad did record a click.
RewardedInterstitialAdEventCallback.super.onAdClicked();
}
}
);
広告を表示する
リワード インタースティシャル広告を表示するには、show() メソッドを使用します。OnUserEarnedRewardListener オブジェクトを使って、リワード イベントを処理します。
Kotlin
// Show the ad.
rewardedInterstitialAd?.show(
this@RewardedActivity,
object : OnUserEarnedRewardListener {
override fun onUserEarnedReward(rewardItem: RewardItem) {
// User earned the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
}
},
)
Java
// Show the ad.
rewardedInterstitialAd.show(
RewardedActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// User earned the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
例
GMA Next Gen SDK の使用方法を示したサンプルアプリをダウンロードし、実行してください。