Quảng cáo xen kẽ có tặng thưởng (thử nghiệm)

Chọn nền tảng: Android iOS Unity Flutter

Quảng cáo xen kẽ có tặng thưởng là một loại định dạng quảng cáo có tặng thưởng, cho phép bạn thêm phần thưởng vào 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.

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

  • SDK quảng cáo trên thiết bị di động của Google phiên bản 19.2.0 trở lên.

Triển khai

Sau đây là các bước chính để tích hợp quảng cáo xen kẽ có tặng thưởng:

  • Tải một quảng cáo
  • Đăng ký sử dụng lệnh gọi lại sự kiện toàn màn hình
  • Xử lý lệnh gọi lại dành cho quảng cáo có tặng thưởng
  • Hiển thị quảng cáo

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

Bạn có thể tải một quảng cáo bằng cách sử dụng phương thức load() tĩnh cho lớp RewardedInterstitialAd. Phương thức tải này yêu cầu một Ngữ cảnh, mã đơn vị quảng cáo, một đối tượng AdManagerAdRequest và một RewardedInterstitialAdLoadCallback để nhận thông báo khi quảng cáo tải thành công hoặc không thành công. Đối tượng RewardedInterstitialAd đã tải được cung cấp dưới dạng một thông số trong lệnh gọi lại onRewardedInterstitialAdLoaded().

Ví dụ sau đây cho biết cách tải một RewardedInterstitialAd trong MainActivity.

Java

RewardedInterstitialAd.load(
    MainActivity.this,
    "AD_UNIT_ID",
    new AdManagerAdRequest.Builder().build(),
    new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        Log.d(TAG, "onAdLoaded");
        rewardedInterstitialAd = ad;
      }

      @Override
      public void onAdFailedToLoad(LoadAdError loadAdError) {
        Log.d(TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
        rewardedInterstitialAd = null;
      }
    });

Kotlin

RewardedInterstitialAd.load(
  this,
  "AD_UNIT_ID",
  AdManagerAdRequest.Builder().build(),
  object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(rewardedAd: RewardedInterstitialAd) {
      Log.d(TAG, "Ad was loaded.")
      rewardedInterstitialAd = rewardedAd
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      Log.d(TAG, "onAdFailedToLoad: ${adError.message}")
      rewardedInterstitialAd = null
    }
  },
)

Thay thế AD_UNIT_ID bằng mã đơn vị quảng cáo của bạn.

Đăng ký sử dụng lệnh gọi lại

Để nhận thông báo cho các sự kiện trình bày, bạn phải chuyển một đối tượng FullScreenContentCallback đến phương thức setter trên quảng cáo của mình. Đối tượng FullScreenContentCallback xử lý lệnh gọi lại khi quảng cáo hiển thị thành công hoặc không thành công và khi quảng cáo bị loại bỏ. Đoạn mã sau đây cho biết cách đặt đối tượng FullScreenContentCallback ẩn danh trong RewardedInterstitialAdLoadCallback:

Java

    rewardedInterstitialAd.setFullScreenContentCallback(
        new FullScreenContentCallback() {
          @Override
          public void onAdDismissedFullScreenContent() {
            // Called when fullscreen content is dismissed.
            Log.d(TAG, "The ad was dismissed.");
            // Make sure to set your reference to null so you don't
            // show it a second time.
            rewardedInterstitialAd = null;
            if (googleMobileAdsConsentManager.canRequestAds()) {
              loadRewardedInterstitialAd();
            }
          }

          @Override
          public void onAdFailedToShowFullScreenContent(AdError adError) {
            // Called when fullscreen content failed to show.
            Log.d(TAG, "The ad failed to show.");
            // Make sure to set your reference to null so you don't
            // show it a second time.
            rewardedInterstitialAd = null;
          }

          @Override
          public void onAdShowedFullScreenContent() {
            // Called when fullscreen content is shown.
            Log.d(TAG, "The ad was shown.");
          }

          @Override
          public void onAdImpression() {
            // Called when an impression is recorded for an ad.
            Log.d(TAG, "The ad recorded an impression.");
          }

          @Override
          public void onAdClicked() {
            // Called when ad is clicked.
            Log.d(TAG, "The ad was clicked.");
          }
        });

    rewardedInterstitialAd.show(
        MainActivity.this,
        new OnUserEarnedRewardListener() {
          @Override
          public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
            Log.d(TAG, "The user earned the reward.");
            // Handle the reward.
            int rewardAmount = rewardItem.getAmount();
            String rewardType = rewardItem.getType();
          }
        });
  }

  private void initializeMobileAdsSdk() {
    if (isMobileAdsInitializeCalled.getAndSet(true)) {
      return;
    }

    // Set your test devices.
    MobileAds.setRequestConfiguration(
        new RequestConfiguration.Builder()
            .setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
            .build());

    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              MobileAds.initialize(this, initializationStatus -> {});

              // Load an ad on the main thread.
              runOnUiThread(() -> loadRewardedInterstitialAd());
            })
        .start();
  }
}

Kotlin

rewardedInterstitialAd?.fullScreenContentCallback =
  object : FullScreenContentCallback() {
    override fun onAdDismissedFullScreenContent() {
      // Called when fullscreen content is dismissed.
      Log.d(TAG, "Ad was dismissed.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      rewardedInterstitialAd = null
    }

    override fun onAdFailedToShowFullScreenContent(adError: AdError) {
      // Called when fullscreen content failed to show.
      Log.d(TAG, "Ad failed to show.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      rewardedInterstitialAd = null
    }

    override fun onAdShowedFullScreenContent() {
      // Called when fullscreen content is shown.
      Log.d(TAG, "Ad showed fullscreen content.")
    }

    override fun onAdImpression() {
      // Called when an impression is recorded for an ad.
      Log.d(TAG, "Ad recorded an impression.")
    }

    override fun onAdClicked() {
      // Called when an ad is clicked.
      Log.d(TAG, "Ad was clicked.")
    }
  }

Hiển thị quảng cáo

Khi hiển thị quảng cáo xen kẽ có tặng thưởng, bạn sẽ sử dụng đối tượng OnUserEarnedRewardListener để xử lý các sự kiện tặng thưởng.

Java

rewardedInterstitialAd.show(
    MainActivity.this,
    new OnUserEarnedRewardListener() {
      @Override
      public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
        Log.d(TAG, "The user earned the reward.");
        // Handle the reward.
        int rewardAmount = rewardItem.getAmount();
        String rewardType = rewardItem.getType();
      }
    });

Kotlin

rewardedInterstitialAd?.show(this) { rewardItem ->
  Log.d(TAG, "User earned the reward.")
  // Handle the reward.
  val rewardAmount = rewardItem.amount
  val rewardType = rewardItem.type
}

Ví dụ trên GitHub

  • Ví dụ về Quảng cáo xen kẽ có tặng thưởng: Java | Kotlin

Các bước tiếp theo

Khám phá các chủ đề sau: