Tải một quảng cáo xen kẽ có tặng thưởng

Next-Gen SDK có API tải một lần.

Quảng cáo xen kẽ có tặng thưởng là một định dạng cho phép bạn tặng thưởng cho những quảng cáo tự động xuất hiện tại các điểm chuyển tiếp tự nhiên của ứng dụng. Không giống như quảng cáo có tặng thưởng, người dùng không bắt buộc phải chọn xem quảng cáo xen kẽ có tặng thưởng.

Tài liệu hướng dẫn này giải thích cách tích hợp quảng cáo xen kẽ có tặng thưởng vào một ứng dụng Android.

Điều kiện tiên quyết

Thiết lập GMA Next-Gen SDK.

Luôn thử nghiệm bằng quảng cáo thử nghiệm

Khi bạn tạo và thử nghiệm ứng dụng, hãy nhớ sử dụng quảng cáo thử nghiệm thay vì quảng cáo đang chạy trong thực tế. Nếu bạn không sử dụng quảng cáo thử nghiệm, Google có thể tạm ngưng tài khoản của bạn.

Cách dễ nhất để tải quảng cáo thử nghiệm là sử dụng mã đơn vị quảng cáo thử nghiệm dành riêng cho quảng cáo xen kẽ có tặng thưởng trên Android:

/21775744923/example/rewarded-interstitial

Mã đơn vị quảng cáo này được định cấu hình đặc biệt để trả về quảng cáo thử nghiệm cho mọi yêu cầu. Bạn có thể sử dụng mã này trong ứng dụng của mình khi viết mã, chạy thử nghiệm và gỡ lỗi. Hãy nhớ thay thế mã này bằng mã đơn vị quảng cáo của riêng bạn trước khi xuất bản ứng dụng.

Để biết thông tin chi tiết về quảng cáo thử nghiệm GMA Next-Gen SDK, hãy xem bài viết Bật quảng cáo thử nghiệm.

Tải một quảng cáo

Ví dụ sau đây cho biết cách tải một quảng cáo:

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
    }
  },
)

Java

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

Khi thử nghiệm, hãy sử dụng mã đơn vị quảng cáo thử nghiệm: /21775744923/example/rewarded-interstitial.

Hiển thị quảng cáo

Để hiển thị quảng cáo xen kẽ có tặng thưởng, hãy sử dụng phương thức show. Sử dụng đối tượng OnUserEarnedRewardListener để xử lý các sự kiện tặng thưởng.

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
      }
    },
  )
}

Java

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

Theo dõi các sự kiện quảng cáo

Trước khi hiển thị quảng cáo, hãy theo dõi các sự kiện trong vòng đời quảng cáo:

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.
      }
    }
}

Java

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

Không bắt buộc: Xác thực lệnh gọi lại của tính năng xác minh phía máy chủ (SSV)

Nếu ứng dụng của bạn cần có thêm dữ liệu trong lệnh gọi lại xác minh phía máy chủ, hãy sử dụng tính năng dữ liệu tuỳ chỉnh của quảng cáo xen kẽ có tặng thưởng. Mọi giá trị chuỗi được đặt cho đối tượng quảng cáo xen kẽ có tặng thưởng đều được truyền đến tham số truy vấn custom_data của lệnh gọi lại SSV. Nếu bạn không đặt giá trị dữ liệu tuỳ chỉnh, thì giá trị tham số truy vấn custom_data sẽ không hiển thị trong lệnh gọi lại SSV.

Mã mẫu sau đây cho biết cách đặt dữ liệu tuỳ chỉnh cho đối tượng quảng cáo xen kẽ có tặng thưởng trước khi yêu cầu quảng cáo:

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)
    }
  },
)

Java

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

Thay thế SAMPLE_CUSTOM_DATA_STRING bằng dữ liệu tuỳ chỉnh của bạn.

Nếu muốn đặt chuỗi phần thưởng tuỳ chỉnh, bạn phải thực hiện việc này trước khi hiển thị quảng cáo.