إعلان بيني

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

يوضّح هذا الدليل كيفية دمج الإعلانات البينية في تطبيق Flutter.

الاختبار دائمًا من خلال الإعلانات الاختبارية

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

أسهل طريقة لتحميل الإعلانات الاختبارية هي استخدام رقم تعريف الوحدة الإعلانية الاختبارية المخصّصة للإعلانات البينية:

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

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

تحميل إعلان

يُحمِّل المثال التالي إعلانًا بينيًا:

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

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/1033173712'
    : 'ca-app-pub-3940256099942544/4411468910';

  /// Loads an interstitial ad.
  void loadAd() {
    InterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          // 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('InterstitialAd failed to load: $error');
          },
        ));
  }
}

أحداث الإعلانات البينية

من خلال استخدام FullScreenContentCallback، يمكنك الاطّلاع على أحداث مراحل النشاط، مثل وقت عرض الإعلان أو رفضه. اضبط InterstitialAd.fullScreenContentCallback قبل عرض الإعلان لتلقّي إشعارات لهذه الأحداث. ينفّذ هذا المثال كل طريقة:

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

  // TODO: replace this test ad unit with your own ad unit.
  final adUnitId = Platform.isAndroid
    ? 'ca-app-pub-3940256099942544/1033173712'
    : 'ca-app-pub-3940256099942544/4411468910';

  /// Loads an interstitial ad.
  void loadAd() {
    InterstitialAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          // 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('InterstitialAd failed to load: $error');
          },
        ));
  }
}

عرض إعلان بيني

يتم عرض InterstitialAd على شكل Overlay أعلى محتوى التطبيق ويتم وضعه بشكل ثابت، وبالتالي لا يمكن إضافته إلى شجرة تطبيقات Flutter المصغّرة. يمكنك اختيار وقت عرض الإعلان عن طريق الاتصال على show().

_interstitiaAd.show();

بعد استدعاء show()، لن يكون بالإمكان إغلاق Ad المعروض بهذه الطريقة آليًا، وسيتطلّب ذلك إدخال المستخدم. لا يمكن عرض InterstitialAd مرة واحدة فقط. ستؤدي المكالمات اللاحقة التي يتم عرضها إلى تشغيل onAdFailedToShowFullScreenContent.

يجب التخلص من الإعلان في حالة عدم الحاجة إليه. تتمثل أفضل ممارسة لوقت طلب الرقم dispose() في معاودة الاتصال بـ FullScreenContentCallback.onAdDismissedFullScreenContent وFullScreenContentCallback.onAdFailedToShowFullScreenContent.

أكملت هذه الخطوة. تطبيقك جاهز الآن لعرض الإعلانات البينية.

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

مثال كامل على GitHub

إعلان بيني