פרסומת מרווח ביניים

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

במדריך הזה מוסבר איך לשלב מודעות מעברון באפליקציית Flutter.

ביצוע בדיקות באמצעות מודעות בדיקה תמיד

כשיוצרים ובודקים אפליקציות, חשוב להשתמש במודעות בדיקה במקום במודעות בדיקה של מודעות בשידור חי. אם לא תעשו זאת, ייתכן שהחשבון שלכם יושעה.

הדרך הקלה ביותר לטעון מודעות בדיקה היא להשתמש במזהה הייעודי של יחידת מודעות לבדיקה עבור מודעות מעברון:

  • /21775744923/example/interstitial

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

טעינת מודעה

בדוגמה הבאה נטען מודעת מעברון:

class InterstitialExampleState extends State<InterstitialExample> {
  AdManagerInterstitialAd? _interstitialAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = '/21775744923/example/interstitial';

  /// Loads an interstitial ad.
  void loadAd() {
    AdManagerInterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdManagerAdRequest(),
        adLoadCallback: AdManagerInterstitialAdLoadCallback(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
            debugPrint('$ad loaded.');
            // Keep a reference to the ad so you can show it later.
            _interstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('AdManagerInterstitialAd failed to load: $error');
          },
        ));
  }
}

אירועים של מודעות מעברון

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

class InterstitialExampleState extends State<InterstitialExample> {
  AdManagerInterstitialAd? _interstitialAd;

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = '/21775744923/example/interstitial';

  /// Loads an interstitial ad.
  void loadAd() {
    AdManagerInterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdManagerAdRequest(),
        adLoadCallback: AdManagerInterstitialAdLoadCallback(
          // Called when an ad is successfully received.
          onAdLoaded: (ad) {
             ad.fullScreenContentCallback = FullScreenContentCallback(
                // Called when the ad showed the full screen content.
                onAdShowedFullScreenContent: (ad) {},
                // Called when an impression occurs on the ad.
                onAdImpression: (ad) {},
                // Called when the ad failed to show full screen content.
                onAdFailedToShowFullScreenContent: (ad, err) {
                  // Dispose the ad here to free resources.
                  ad.dispose();
                },
                // Called when the ad dismissed full screen content.
                onAdDismissedFullScreenContent: (ad) {
                  // Dispose the ad here to free resources.
                  ad.dispose();
                },
                // Called when a click is recorded for an ad.
                onAdClicked: (ad) {});

            debugPrint('$ad loaded.');
            // Keep a reference to the ad so you can show it later.
            _interstitialAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('AdManagerInterstitialAd failed to load: $error');
          },
        ));
  }
}

הצגה של מודעת מעברון

AdManagerInterstitialAd מוצג בתור Overlay מעל כל התוכן של האפליקציה וממוקם באופן סטטי; ולכן לא ניתן להוסיף אותו לעץ הווידג'ט של Flutter. ניתן לבחור מתי להציג את המודעה על ידי התקשרות למספר show().

_interstitiaAd.show();

לאחר הקריאה אל show(), לא ניתן לסגור Ad שמוצג כך באופן פרוגרמטי ומחייב קלט של משתמשים. ניתן להציג AdManagerInterstitialAd בלבד פעם אחת. הקריאות הבאות להצגה יפעילו את onAdFailedToShowFullScreenContent.

מודעה חייבת להשליך כאשר אין יותר צורך בגישה אליה. השיטה המומלצת מתי להתקשר אל dispose() FullScreenContentCallback.onAdDismissedFullScreenContent והקבוצה FullScreenContentCallback.onAdFailedToShowFullScreenContent התקשרות חזרה.

זהו! האפליקציה מוכנה עכשיו להצגת מודעות מעברון.

השלבים הבאים