מודעת מעברון מתגמלת (בטא)

מודעת מעברון מתגמלת היא סוג של פורמט מודעה שמבוסס על תמריצים. הוא מאפשר להציע תגמולים על מודעות שמופיעות באופן אוטומטי בנקודות מעבר טבעיות באפליקציה. בניגוד למודעות מתגמלות, המשתמשים לא צריכים להביע הסכמה לצפייה במודעת מעברון מתגמלת.

דרישות מוקדמות

הטמעה

אלה השלבים העיקריים לשילוב מודעות מעברון מתגמלות:

  • טעינת מודעה
  • הרשמה לקריאה חוזרת (callback) של אירוע במסך מלא
  • טיפול בקריאה החוזרת (callback) של הפרס
  • מציגים את המודעה

טעינת מודעה

טעינת מודעה מתבצעת באמצעות השיטה load() הסטטית במחלקה RewardedInterstitialAd. לשיטת הטעינה נדרשים הקשר, מזהה יחידת המודעות, אובייקט AdManagerAdRequest ו-RewardedInterstitialAdLoadCallback, כדי לקבל הודעה כשטעינת המודעה מצליחה או נכשלת. האובייקט RewardedInterstitialAd שנטען מסופק כפרמטר בקריאה החוזרת (callback) של onRewardedInterstitialAdLoaded().

הדוגמה הבאה מראה איך לטעון RewardedInterstitialAd ב-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);

    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              MobileAds.initialize(this, initializationStatus -> {});
              // Load an ad on the main thread.
              runOnUiThread(
                  () -> {
                    loadAd();
                  });
            })
        .start();
  }

  public void loadAd() {
    // Use the test ad unit ID to load an ad.
    RewardedInterstitialAd.load(MainActivity.this, "/21775744923/example/rewarded_interstitial",
        new AdManagerAdRequest.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)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      // Initialize the Google Mobile Ads SDK on a background thread.
      MobileAds.initialize(this@MainActivity) {}
      // Load an ad on the main thread.
      runOnUiThread {
        loadAd()
      }
    }
  }

  private fun loadAd() {
    RewardedInterstitialAd.load(this, "/21775744923/example/rewarded_interstitial",
      AdManagerAdRequest.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
      }
    })
  }
}

הרשמה להתקשרות חזרה

כדי לקבל התראות על אירועי מצגת, עליכם להעביר אובייקט FullScreenContentCallback לרכיב המגדיר במודעה. האובייקט FullScreenContentCallback מטפל בקריאות חוזרות (callback) במקרים שבהם המודעה מוצגת בהצלחה או נכשלת, וכשהיא נסגרת. הקוד הבא מראה איך להגדיר אובייקט FullScreenContentCallback אנונימי ב-RewardedInterstitialAdLoadCallback:

Java

public void loadAd(){
  RewardedInterstitialAd.load(MainActivity.this, "/21775744923/example/rewarded_interstitial",
      new AdManagerAdRequest.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, "/21775744923/example/rewarded_interstitial",
    AdManagerAdRequest.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
    }
  })
}

מתמודדים עם פרסים

כדי להציג את מודעת המעברון המתגמלת, צריך להטמיע את הממשק OnUserEarnedRewardListener ב-MainActivity כדי לקבל הודעה כשהמשתמש זוכה בפרס.

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

הצגת המודעה

אחרי שמטמיעים את הממשק של OnUserEarnedRewardListener, אפשר להציג את המודעה באמצעות השיטה show() של המודעה, למשל:

Java

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

Kotlin

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

דוגמאות ב-GitHub

  • דוגמה למודעות מעברון מתגמלות: Java | Kotlin

השלבים הבאים

נסו את הנושאים הבאים: