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. Để biết thêm thông tin, hãy xem Hướng dẫn về 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 ứng dụng Android.
Trước khi bắt đầu
Trước khi tiếp tục, hãy làm như sau:
- Thiết lập GMA Next-Gen SDK.
- Sử dụng mã đơn vị quảng cáo xen kẽ có tặng thưởng thử nghiệm
ca-app-pub-3940256099942544/5354046379.- 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 sản phẩm đang chạy trong thực tế. Nếu bạn không sử dụng mã đơn vị quảng cáo thử nghiệm, Google có thể tạm ngưng tài khoản của bạn.
- Trước khi xuất bản ứng dụng, hãy thay thế mã này bằng mã đơn vị quảng cáo của bạn.
- Để 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 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:
ca-app-pub-3940256099942544/5354046379.
Hiển thị quảng cáo
Để hiển thị quảng cáo xen kẽ có tặng thưởng, hãy gọi phương thứcshow. Để xử lý các sự kiện có tặng thưởng, hãy sử dụng đối tượng OnUserEarnedRewardListener.
Ví dụ sau đây cho thấy quảng cáo:
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 của quảng cáo. Ví dụ sau đây đăng ký lệnh gọi lại cho các sự kiện 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)
Để truyền bối cảnh người dùng và ứng dụng trong lệnh gọi lại của tính năng xác minh phía máy chủ (SSV), hãy đặt các tuỳ chọn xác minh trên đối tượng quảng cáo trước khi hiển thị quảng cáo. Vì lệnh gọi lại SSV đến máy chủ của bạn từ Google mà không có trạng thái phiên, hãy đính kèm các chuỗi không bắt buộc sau đây. Google sẽ chuyển tiếp các chuỗi này mà không thay đổi dưới dạng tham số truy vấn đã ký trong lệnh gọi lại SSV:
- Tham số truy vấn
user_id: Xác định người dùng được ghi nhận, chẳng hạn như mã tài khoản nội bộ. - Tham số truy vấn
custom_data: Chứa dữ liệu ứng dụng, chẳng hạn như vị trí đặt phần thưởng, cấp độ trò chơi hoặc số chỉ dùng một lần do máy chủ tạo.
Nếu bạn không đặt giá trị, Google sẽ bỏ qua tham số truy vấn đó trong lệnh gọi lại SSV.
Ví dụ sau đây đặt dữ liệu tuỳ chỉnh cho một đối tượng quảng cáo trước khi hiển thị quảng cáo:
Kotlin
Java
Thay thế SAMPLE_CUSTOM_DATA_STRING bằng dữ liệu tuỳ chỉnh của bạn.
Lưu ý quan trọng: Tham số truy vấn custom_data được mã hoá theo phần trăm. Trước khi phân tích cú pháp giá trị, hãy giải mã URL của chuỗi trên máy chủ và xác minh chữ ký gọi lại trước khi tin tưởng các tham số truy vấn user_id hoặc custom_data.