بار کردن یک آگهی بینابینی پاداش‌دار

آگهی‌های بینابینی پاداش‌دار قالبی است که به شما امکان می‌دهد برای آگهی‌هایی که به‌طور خودکار درطول انتقال‌های طبیعی برنامه نمایش داده می‌شوند پاداش ارائه دهید. برخلاف آگهی‌های دارای پاداش، کاربران ملزم به موافقت برای مشاهده آگهی بینابینی دارای پاداش نیستند. برای اطلاعات بیشتر، به راهنمایی آگهی میانه‌ای پاداش‌دار مراجعه کنید.

این راهنما نحوه ادغام کردن آگهی‌های بینابینی پاداش‌دار در برنامه Android را توضیح می‌دهد.

پیش‌نیازها

‫GMA Next-Gen SDK را راه‌اندازی کنید.

همیشه با آگهی‌های آزمایشی آزمایش کنید

هنگام ساختن و آزمایش کردن برنامه‌هایتان، مطمئن شوید که از آگهی‌های آزمایشی به‌جای آگهی‌های زنده و تولید استفاده می‌کنید. اگر از آگهی‌های آزمایشی استفاده نکنید، Google می‌تواند حسابتان را تعلیق کند.

آسان‌ترین راه برای بار کردن آگهی‌های آزمایشی استفاده از شناسه واحد آگهی آزمایشی اختصاصی برای آگهی‌های بینابینی پاداش‌دار Android است:

ca-app-pub-3940256099942544/5354046379

این شناسه واحد آگهی به‌طور ویژه پیکربندی شده است تا برای هر درخواست آگهی‌های آزمایشی برگرداند. می‌توانید هنگام کدنویسی، آزمایش، و اشکال‌زدایی، آزادانه از آن در برنامه‌های خودتان استفاده کنید. قبل‌از انتشار برنامه‌تان، حتماً آن را با شناسه واحد آگهی خودتان جایگزین کنید.

برای جزئیات مربوط به GMA Next-Gen SDK آگهی آزمایشی، به فعال کردن آگهی‌های آزمایشی مراجعه کنید.

بار کردن آگهی

مثال زیر نحوه بار کردن یک آگهی را نشان می‌دهد:

Kotlin

// Load ads after you initialize MobileAds.
RewardedInterstitialAd.load(
  AdRequest.Builder(adUnitId).build(),
  object : AdLoadCallback<RewardedInterstitialAd> {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      // Rewarded interstitial ad loaded.
      rewardedInterstitialAd = ad
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      // Rewarded interstitial ad failed to load.
      Log.e(TAG, "Rewarded interstitial ad failed to load: ${adError.message}")
      rewardedInterstitialAd = null
    }
  },
)

جاوا

// Load ads after you initialize MobileAds.
RewardedInterstitialAd.load(
    new AdRequest.Builder(adUnitId).build(),
    new AdLoadCallback<RewardedInterstitialAd>() {
      @Override
      public void onAdLoaded(@NonNull RewardedInterstitialAd ad) {
        // Rewarded interstitial ad loaded.
        rewardedInterstitialAd = ad;
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError adError) {
        // Rewarded interstitial ad failed to load.
        Log.e(TAG, "Rewarded interstitial ad failed to load: " + adError.getMessage());
        rewardedInterstitialAd = null;
      }
    });

هنگام آزمایش، از شناسه واحد آگهی آزمایشی استفاده کنید: ca-app-pub-3940256099942544/5354046379.

نمایش آگهی

برای نمایش آگهی بینابینی پاداش‌دار، از روش show استفاده کنید. از شیء OnUserEarnedRewardListener برای مدیریت رویدادهای پاداش استفاده کنید.

Kotlin

private fun showAd(rewardedInterstitialAd: RewardedInterstitialAd, activity: Activity) {
  // Show the ad.
  rewardedInterstitialAd.show(
    activity,
    object : OnUserEarnedRewardListener {
      override fun onUserEarnedReward(rewardItem: RewardItem) {
        // User earned the reward.
        val rewardAmount = rewardItem.amount
        val rewardType = rewardItem.type
      }
    },
  )
}

جاوا

private void showAd(RewardedInterstitialAd rewardedInterstitialAd, Activity activity) {
  // Show the ad.
  rewardedInterstitialAd.show(
      activity,
      new OnUserEarnedRewardListener() {
        @Override
        public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
          // User earned the reward.
          int rewardAmount = rewardItem.getAmount();
          String rewardType = rewardItem.getType();
        }
      });
}

گوش دادن به رویدادهای آگهی

قبل‌از نمایش آگهی، به رویدادهای چرخه حیات آگهی گوش دهید:

Kotlin

private fun listenToAdEvents() {
  // Listen for ad events.
  val ad = rewardedInterstitialAd
  if (ad == null) {
    Log.e(TAG, "Rewarded interstitial ad is not ready yet.")
    return
  }

  ad.adEventCallback =
    object : RewardedInterstitialAdEventCallback {
      override fun onAdShowedFullScreenContent() {
        // Rewarded interstitial ad did show.
      }

      override fun onAdDismissedFullScreenContent() {
        // Rewarded interstitial ad did dismiss.
        rewardedInterstitialAd = null
      }

      override fun onAdFailedToShowFullScreenContent(
        fullScreenContentError: FullScreenContentError
      ) {
        // Rewarded interstitial ad failed to show.
        Log.e(TAG, "Rewarded interstitial ad failed to show: ${fullScreenContentError.message}")
      }

      override fun onAdImpression() {
        // Rewarded interstitial ad did record an impression.
      }

      override fun onAdClicked() {
        // Rewarded interstitial ad did record a click.
      }
    }
}

جاوا

private void listenToAdEvents() {
  // Listen for ad events.
  if (rewardedInterstitialAd == null) {
    Log.e(TAG, "Rewarded interstitial ad is not ready yet.");
    return;
  }

  rewardedInterstitialAd.setAdEventCallback(
      new RewardedInterstitialAdEventCallback() {
        @Override
        public void onAdShowedFullScreenContent() {
          // Rewarded interstitial ad did show.
        }

        @Override
        public void onAdDismissedFullScreenContent() {
          // Rewarded interstitial ad did dismiss.
          rewardedInterstitialAd = null;
        }

        @Override
        public void onAdFailedToShowFullScreenContent(
            @NonNull FullScreenContentError fullScreenContentError) {
          // Rewarded interstitial ad failed to show.
          Log.e(
              TAG,
              "Rewarded interstitial ad failed to show: " + fullScreenContentError.getMessage());
        }

        @Override
        public void onAdImpression() {
          // Rewarded interstitial ad did record an impression.
        }

        @Override
        public void onAdClicked() {
          // Rewarded interstitial ad did record a click.
        }
      });
}

اختیاری: اعتبارسنجی کردن پیام‌های برگشتی درستی‌سنجی سمت سرور (SSV)

اگر برنامه شما در درستی‌سنجی سمت سرور به داده‌های اضافی نیاز دارد، از ویژگی داده سفارشی آگهی‌های بینابینی پاداش‌دار استفاده کنید. هر مقدار رشته‌ای که روی یک شیء آگهی بینابینی پاداش‌دار تنظیم شده باشد به پارامتر پُرسمان custom_data در برگشت تماس SSV منتقل می‌شود. اگر مقدار داده سفارشی تنظیم نشده باشد، مقدار پارامتر پُرسمان custom_data در بازخوان SSV وجود نخواهد داشت.

نمونه کد زیر نشان می‌دهد که چگونه داده‌های سفارشی را روی یک شیء آگهی بینابینی پاداش‌دار قبل‌از درخواست آگهی تنظیم کنید:

Kotlin

RewardedInterstitialAd.load(
  context,
  AD_UNIT_ID,
  AdRequest.Builder().build(),
  object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      rewardedInterstitialAd = ad
      val options =
        ServerSideVerificationOptions.Builder().setCustomData("SAMPLE_CUSTOM_DATA_STRING").build()
      rewardedInterstitialAd?.setServerSideVerificationOptions(options)
    }
  },
)

جاوا

RewardedInterstitialAd.load(
    context,
    AD_UNIT_ID,
    new AdRequest.Builder().build(),
    new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        rewardedInterstitialAd = ad;
        ServerSideVerificationOptions options =
            new ServerSideVerificationOptions.Builder()
                .setCustomData("SAMPLE_CUSTOM_DATA_STRING")
                .build();
        rewardedInterstitialAd.setServerSideVerificationOptions(options);
      }
    });

‫SAMPLE_CUSTOM_DATA_STRING را با داده‌های سفارشی خودتان جایگزین کنید.

اگر می‌خواهید رشته پاداش سفارشی را تنظیم کنید، باید این کار را قبل‌از نمایش آگهی انجام دهید.