یک تبلیغ بینابینی پاداش‌دار بارگذاری کنید

SDK نسل بعدی با API تک بارگذاری.

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

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

پیش‌نیازها

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

همیشه با تبلیغات آزمایشی تست کنید

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

ساده‌ترین راه برای بارگذاری تبلیغات آزمایشی، استفاده از شناسه واحد تبلیغات آزمایشی اختصاصی برای تبلیغات بینابینی پاداش‌دار اندروید است:

ca-app-pub-3940256099942544/5354046379

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

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

بارگذاری یک تبلیغ

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

کاتلین

// 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 برای مدیریت رویدادهای جایزه استفاده کنید.

کاتلین

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();
        }
      });
}

به رویدادهای تبلیغاتی گوش دهید

قبل از نمایش تبلیغ، به رویدادهای چرخه عمر تبلیغ گوش دهید:

کاتلین

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 وجود نخواهد داشت.

نمونه کد زیر نحوه تنظیم داده‌های سفارشی روی یک شیء تبلیغ بینابینی پاداش‌دار، قبل از درخواست تبلیغ را نشان می‌دهد:

کاتلین

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 را با داده‌های سفارشی خود جایگزین کنید.

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