تم منح المكافأة

"الإعلانات التي تضم مكافآت" هي إعلانات يتوفّر للمستخدمين خيار التفاعل معها مقابل الحصول على مكافآت داخل التطبيق. يوضّح هذا الدليل كيفية دمج الإعلانات التي تضم مكافآت من AdMob في تطبيق Flutter.

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

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

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

Android

ca-app-pub-3940256099942544/5224354917

iOS

ca-app-pub-3940256099942544/1712485313

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

تحميل إعلان

يُحمِّل المثال التالي إعلانًا يضم مكافأة:

class RewardedExampleState extends State<RewardedExample> {
  RewardedAd? _rewardedAd;

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

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

أحداث الإعلانات التي تضم مكافآت

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

class RewardedExampleState extends State<RewardedExample> {
  RewardedAd? _rewardedAd;

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

  /// Loads a rewarded ad.
  void loadAd() {
    RewardedAd.load(
        adUnitId: adUnitId,
        request: const AdRequest(),
        adLoadCallback: RewardedAdLoadCallback(
          // 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.
            _rewardedAd = ad;
          },
          // Called when an ad request failed.
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('RewardedAd failed to load: $error');
          },
        ));
  }
}

إعلان صوري

يتم عرض RewardedAd على شكل طبقة مركّبة على سطح كل محتوى التطبيق ويتم وضعه بشكل ثابت، وبالتالي لا يمكن إضافته إلى شجرة تطبيقات Flutter المصغّرة. يمكنك اختيار وقت عرض الإعلان عن طريق الاتصال على show(). يحصل RewardedAd.show() على OnUserEarnedRewardCallback، والتي يتم استدعاءها عندما يحصل المستخدم على مكافأة. تأكد من تنفيذ هذا الأمر وكافئ المستخدم على مشاهدة هذا الإعلان.

_rewardedAd.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) {
  // Reward the user for watching an ad.
});

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

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

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

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

إعلان بمكافأة