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

Quảng cáo xen kẽ có tặng thưởng là một loại của định dạng quảng cáo có trả thưởng cho phép bạn tặng thưởng cho những quảng cáo xuất hiện tự động trong quá trình chuyển đổi 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 người dùng phải chọn tham gia để 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 của Google trên thiết bị di động phiên bản 19.2.0 trở lên. * Xem hết Hướng dẫn bắt đầu sử dụng.

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 phần thưởng
  • Hiển thị quảng cáo
  • [Không bắt buộc] Xác thực lệnh gọi lại của SSO

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 trên Lớp RewardedInterstitialAd. Phương thức tải yêu cầu Ngữ cảnh, quảng cáo của bạn mã đơn vị, một đối tượng AdRequest và một RewardedInterstitialAdLoadCallback để được thông báo khi tải quảng cáo 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 tham số trong lệnh gọi lại onRewardedInterstitialAdLoaded().

Ví dụ sau đây trình bày cách tải một RewardedInterstitialAd trong MainActivity.

Java

public class MainActivity extends AppCompatActivity {
  private RewardedInterstitialAd rewardedInterstitialAd;
  private String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              MobileAds.initialize(this, initializationStatus -> {});
              // Load an ad on the main thread.
              runOnUiThread(
                  () -> {
                    loadAd();
                  });
            })
        .start();
  }

  public void loadAd() {
    // Use the test ad unit ID to load an ad.
    RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
        new AdRequest.Builder().build(),  new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        Log.d(TAG, "Ad was loaded.");
        rewardedInterstitialAd = ad;
      }
      @Override
      public void onAdFailedToLoad(LoadAdError loadAdError) {
        Log.d(TAG, loadAdError.toString());
        rewardedInterstitialAd = null;
      }
    });
  }
}

Kotlin

import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback

class MainActivity : AppCompactActivity() {

  private var rewardedInterstitialAd? = null
  private final var TAG = "MainActivity"

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      // Initialize the Google Mobile Ads SDK on a background thread.
      MobileAds.initialize(this@MainActivity) {}
      // Load an ad on the main thread.
      runOnUiThread {
        loadAd()
      }
    }
  }

  private fun loadAd() {
    RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
      AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
      override fun onAdLoaded(ad: RewardedInterstitialAd) {
        Log.d(TAG, "Ad was loaded.")
        rewardedInterstitialAd = ad
      }

      override fun onAdFailedToLoad(adError: LoadAdError) {
        Log.d(TAG, adError?.toString())
        rewardedInterstitialAd = null
      }
    })
  }
}

Đăng ký các 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 truyền một FullScreenContentCallback đối với phương thức setter trên quảng cáo của bạn. Chiến lược phát hành đĩa đơn Đối tượng FullScreenContentCallback xử lý các lệnh gọi lại khi quảng cáo hiển thị thành công hay không thành công và khi bỏ qua. Mã sau đây cho biết cách đặt một đối tượng FullScreenContentCallback ẩn danh trong RewardedInterstitialAdLoadCallback:

Java

public void loadAd(){
  RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
      new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
    @Override
    public void onAdLoaded(RewardedInterstitialAd ad) {
      rewardedInterstitialAd = ad;
      rewardedInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
        @Override
        public void onAdClicked() {
          // Called when a click is recorded for an ad.
          Log.d(TAG, "Ad was clicked.");
        }

        @Override
        public void onAdDismissedFullScreenContent() {
          // Called when ad is dismissed.
          // Set the ad reference to null so you don't show the ad a second time.
          Log.d(TAG, "Ad dismissed fullscreen content.");
          rewardedInterstitialAd = null;
        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
          // Called when ad fails to show.
          Log.e(TAG, "Ad failed to show fullscreen content.");
          rewardedInterstitialAd = null;
        }

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

        @Override
        public void onAdShowedFullScreenContent() {
          // Called when ad is shown.
          Log.d(TAG, "Ad showed fullscreen content.");
        }
      });
    }
    @Override
    public void onAdFailedToLoad(LoadAdError loadAdError) {
      Log.d(TAG, loadAdError.toString());
      rewardedInterstitialAd = null;
    }
  });
}

Kotlin

private fun loadAd() {
  RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
    AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      rewardedInterstitialAd = ad
      rewardedInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
        override fun onAdClicked() {
          // Called when a click is recorded for an ad.
          Log.d(TAG, "Ad was clicked.")
        }

        override fun onAdDismissedFullScreenContent() {
          // Called when ad is dismissed.
          // Set the ad reference to null so you don't show the ad a second time.
          Log.d(TAG, "Ad dismissed fullscreen content.")
          rewardedInterstitialAd = null
        }

        override fun onAdFailedToShowFullScreenContent(adError: AdError) {
          // Called when ad fails to show.
          Log.e(TAG, "Ad failed to show fullscreen content.")
          rewardedInterstitialAd = null
        }

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

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

    override fun onAdFailedToLoad(adError: LoadAdError) {
      Log.d(TAG, adError?.toString())
      rewardedInterstitialAd = null
    }
  })
}

Xử lý phần thưởng

Để hiển thị quảng cáo xen kẽ có tặng thưởng của bạn, hãy triển khai Giao diện OnUserEarnedRewardListener trong MainActivity của bạn. Bạn sẽ nhận được thông báo khi người dùng nhận được một phần thưởng.

Java

public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
  ...
  @Override
  public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
    Log.i(TAG, "User earned reward.");
    // TODO: Reward the user!
  }
}

Kotlin

class MainActivity : AppCompatActivity(), OnUserEarnedRewardListener {
  ...
  override fun onUserEarnedReward(rewardItem: RewardItem) {
    Log.d(TAG, "User earned reward.")
    // TODO: Reward the user!
  }
}

Hiển thị quảng cáo

Sau khi triển khai giao diện OnUserEarnedRewardListener, bạn có thể trình bày quảng cáo sử dụng phương thức show() của quảng cáo như sau:

Java

rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
    OnUserEarnedRewardListener */ MainActivity.this);

Kotlin

rewardedInterstitialAd?.show(/* Activity */ this, /*
    OnUserEarnedRewardListener */ this)

[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)

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

Mã mẫu sau đây minh hoạ cách đặt dữ liệu tuỳ chỉnh trên quảng cáo có tặng thưởng quảng cáo xen kẽ trước khi yêu cầu một quảng cáo.

Java

RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
    new AdRequest.Builder().build(),  new RewardedInterstitialAdLoadCallback() {
  @Override
  public void onAdLoaded(RewardedInterstitialAd ad) {
    Log.d(TAG, "Ad was loaded.");
    rewardedInterstitialAd = ad;
    ServerSideVerificationOptions options = new ServerSideVerificationOptions
        .Builder()
        .setCustomData("SAMPLE_CUSTOM_DATA_STRING")
        .build();
    rewardedInterstitialAd.setServerSideVerificationOptions(options);
  }
  @Override
  public void onAdFailedToLoad(LoadAdError loadAdError) {
    Log.d(TAG, loadAdError.toString());
    rewardedInterstitialAd = null;
  }
});

Kotlin

RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
    AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
  override fun onAdLoaded(ad: RewardedInterstitialAd) {
    Log.d(TAG, "Ad was loaded.")
    rewardedInterstitialAd = ad
    val options = ServerSideVerificationOptions.Builder()
        .setCustomData("SAMPLE_CUSTOM_DATA_STRING")
        .build()
    rewardedInterstitialAd.setServerSideVerificationOptions(options)
  }

  override fun onAdFailedToLoad(adError: LoadAdError) {
    Log.d(TAG, adError?.toString())
    rewardedInterstitialAd = null
  }
})

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

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: