الإعلان البيني بمكافأة (إصدار تجريبي)

الإعلان البيني الذي يضم مكافأة هو نوع من أشكال الإعلانات المحفَّزة التي تتيح لك تقديم مكافآت للإعلانات التي تظهر تلقائيًا أثناء عمليات النقل العادية للتطبيقات. وعلى عكس الإعلانات بمكافأة، لا يُطلب من المستخدمين الموافقة على عرض إعلان بيني بمكافأة.

المتطلبات الأساسية

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

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

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

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

إعداد حزمة تطوير البرامج (SDK) لعرض الإعلانات للأجهزة الجوّالة

قبل تحميل الإعلانات، اطلب من تطبيقك إعداد حزمة تطوير البرامج (SDK) لعرض الإعلانات على الأجهزة الجوّالة من خلال استدعاء MobileAds.Initialize(). يجب إجراء ذلك مرة واحدة فقط، ومن الأفضل أن يتم ذلك عند تشغيل التطبيق.

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

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

التنفيذ

في ما يلي الخطوات الرئيسية لدمج الإعلانات البينية التي تضم مكافآت:

  1. تحميل الإعلان البيني الذي يضمّ مكافأة
  2. [اختياري] التحقّق من صحة عمليات معاودة الاتصال في عملية إثبات الملكية من جهة الخادم
  3. عرض الإعلان البيني الذي يضمّ مكافأة مع طلب استدعاء المكافأة
  4. الاستماع إلى أحداث الإعلانات البينية التي تضم مكافأة
  5. تنظيف الإعلان البيني الذي يضم مكافأة
  6. التحميل المسبق للإعلان البيني التالي الذي يضم مكافأة

تحميل الإعلان البيني الذي يضمّ مكافأة

يتم تحميل إعلان بيني يضمّ مكافأة باستخدام الطريقة الثابتة Load() في فئة RewardedInterstitialAd. تتطلّب طريقة التحميل معرّف وحدة إعلانية وعنصر AdRequest ومعالج اكتمال يتم استدعاؤه عند نجاح تحميل الإعلان أو تعذّره. يتوفّر عنصر RewardedInterstitialAd كمَعلمة في عملية الإكمال. . يوضّح المثال أدناه كيفية تحميل RewardedInterstitialAd.


  // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
  private string _adUnitId = "unused";
#endif

  private RewardedInterstitialAd _rewardedInterstitialAd;

  /// <summary>
  /// Loads the rewarded interstitial ad.
  /// </summary>
  public void LoadRewardedInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_rewardedInterstitialAd != null)
      {
            _rewardedInterstitialAd.Destroy();
            _rewardedInterstitialAd = null;
      }

      Debug.Log("Loading the rewarded interstitial ad.");

      // create our request used to load the ad.
      var adRequest = new AdRequest();
      adRequest.Keywords.Add("unity-admob-sample");

      // send the request to load the ad.
      RewardedInterstitialAd.Load(_adUnitId, adRequest,
          (RewardedInterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("rewarded interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Rewarded interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _rewardedInterstitialAd = ad;
          });
  }

[اختياري] التحقّق من صحة عمليات معاودة الاتصال في عملية إثبات الملكية من جهة الخادم

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

يوضح نموذج التعليمة البرمجية التالي كيفية تعيين خيارات SSV بعد تم تحميل الإعلان البيني الذي يضم مكافأة.

// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
                            adRequest,
                            (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("Rewarded interstitial ad failed to load an ad " +
                           " with error : " + error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("Rewarded interstitial ad loaded with response : " +
                   ad.GetResponseInfo());
        
        // Create and pass the SSV options to the rewarded ad.
        var options = new ServerSideVerificationOptions
                              .Builder()
                              .SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
                              .Build()
        ad.SetServerSideVerificationOptions(options);
        
});

إذا كنت تريد ضبط سلسلة المكافأة المخصّصة، عليك إجراء ذلك قبل عرض الإعلان.

عرض الإعلان البيني الذي يضمّ مكافأة مع طلب استدعاء المكافأة

عند عرض إعلانك، يجب تقديم دالة ردّ اتصال لمعالجة المكافأة التي يحصل عليها المستخدِم. يمكن عرض الإعلانات مرة واحدة فقط لكل تحميل. استخدِم طريقة CanShowAd() من أجل التحقق من أن الإعلان جاهز للعرض.

يقدِّم الرمز التالي أفضل طريقة لعرض إعلان بمكافأة الإعلان البيني.

public void ShowRewardedInterstitialAd()
{
    const string rewardMsg =
        "Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";

    if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
    {
        rewardedInterstitialAd.Show((Reward reward) =>
        {
            // TODO: Reward the user.
            Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
        });
    }
}

الاستماع إلى أحداث الإعلانات البينية التي تضم مكافأة

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

private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Rewarded interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Rewarded interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);
    };
}

تنظيف الإعلان البيني الذي يضمّ مكافأة

عند الانتهاء من "RewardedInterstitialAd"، احرص على الاتصال Destroy() قبل إسقاط الإشارة إليها:

_rewardedInterstitialAd.Destroy();

يُعلم هذا المكوّن الإضافي بأنّه لم يعُد يتم استخدام العنصر وأنّه يمكن استرداد الذاكرة التي يشغلها. يؤدي عدم طلب هذه الطريقة إلى حدوث تسرُّب للذاكرة.

تحميل الإعلان البيني التالي الذي يضمّ مكافأة مسبقًا

RewardedInterstitialAd هو عنصر للاستخدام لمرة واحدة. وهذا يعني أنه بمجرد عرض مكافأة سيتم عرض إعلان بيني، لا يمكن استخدام العنصر مرة أخرى. لطلب مفتاح آخر إعلان بيني يضم مكافأة، ستحتاج إلى تحميل RewardedInterstitialAd جديد الخاص بك.

لإعداد إعلان بيني يضمّ مكافأة لفرصة الظهور التالية، حمِّل الإعلان البيني الذي يضمّ مكافأة مسبقًا بعد بدء حدث الإعلان OnAdFullScreenContentClosed أو OnAdFullScreenContentFailed.

private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
}

مراجع إضافية