إعلان بيني

الإعلانات البينية هي إعلانات بملء الشاشة تغطي واجهة التطبيق المضيف. وعادة ما يتم عرضها في نقاط انتقال طبيعية في تدفق التطبيق، مثل بين الأنشطة أو أثناء التوقف المؤقت بين المستويات في لعبة. عندما يعرض أحد التطبيقات إعلانًا بينيًا، يمكن للمستخدم النقر على الإعلان والمتابعة إلى وجهته أو إغلاقه والعودة إلى التطبيق.

يشرح هذا الدليل كيفية دمج الإعلانات البينية في تطبيق 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 طلبات معاودة الاتصال

هذا كل شيء! أصبح تطبيقك جاهزًا الآن لعرض الإعلانات البينية.

الخطوات التالية