Quảng cáo có tặng thưởng

Quảng cáo có tặng thưởng cho phép bạn tặng người dùng các vật phẩm trong ứng dụng khi họ tương tác với quảng cáo dạng video, quảng cáo có thể chơi và các bản khảo sát.

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

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

Khi 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 thực tế. Chúng tôi có thể tạm ngưng tài khoản của bạn nếu bạn không làm như vậy.

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 có tặng thưởng trên Android:

ca-app-pub-3940256099942544/5224354917

Mã 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 và bạn có thể sử dụng mã này trong ứng dụng của mình khi lập trình, chạy thử nghiệm và gỡ lỗi. Bạn chỉ cần thay thế mã này bằng mã đơn vị quảng cáo của mình trước khi xuất bản ứng dụng.

Để biết thêm thông tin về cách hoạt động của quảng cáo thử nghiệm của SDK quảng cáo trên thiết bị di động, hãy xem bài viết Quảng cáo thử nghiệm.

加载激励广告对象

RewardedAd 类调用静态 load() 方法并传入 RewardedAdLoadCallback 可加载激励广告。这通常在 ActivityonCreate() 方法中完成。请注意,与其他广告格式加载回调一样,RewardedAdLoadCallback 会利用 LoadAdError 提供较高保真度的错误详情。

Java

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {
  private RewardedAd rewardedAd;
  private final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    AdRequest adRequest = new AdRequest.Builder().build();
    RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
      adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          // Handle the error.
          Log.d(TAG, loadAdError.toString());
          rewardedAd = null;
        }

        @Override
        public void onAdLoaded(@NonNull RewardedAd ad) {
          rewardedAd = ad;
          Log.d(TAG, "Ad was loaded.");
        }
    });
  }
}

Kotlin

class MainActivity : AppCompatActivity() {

  private var rewardedAd: RewardedAd? = null
  private final var TAG = "MainActivity"

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var adRequest = AdRequest.Builder().build()
    RewardedAd.load(this,"ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        Log.d(TAG, adError?.toString())
        rewardedAd = null
      }

      override fun onAdLoaded(ad: RewardedAd) {
        Log.d(TAG, "Ad was loaded.")
        rewardedAd = ad
       }
    })
  }
}

设置 FullScreenContentCallback

FullScreenContentCallback 会处理与显示 RewardedAd 相关的事件。在显示 RewardedAd 之前,请务必按如下方式设置回调:

Java

rewardedAd.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.");
    rewardedAd = null;
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.");
    rewardedAd = 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.");
  }
});

Kotlin

rewardedAd?.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.")
    rewardedAd = null
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    // Called when ad fails to show.
    Log.e(TAG, "Ad failed to show fullscreen content.")
    rewardedAd = 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.")
  }
}

展示广告

展示激励广告时,您将使用 OnUserEarnedRewardListener 对象处理奖励事件。

Java

if (rewardedAd != null) {
  Activity activityContext = MainActivity.this;
  rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

Kotlin

rewardedAd?.let { ad ->
  ad.show(this, OnUserEarnedRewardListener { rewardItem ->
    // Handle the reward.
    val rewardAmount = rewardItem.amount
    val rewardType = rewardItem.type
    Log.d(TAG, "User earned the reward.")
  })
} ?: run {
  Log.d(TAG, "The rewarded ad wasn't ready yet.")
}

[可选] 验证服务器端验证 (SSV) 回调

对于需要服务器端验证回调中额外数据的应用,应使用激励广告的自定义数据功能。在激励广告对象上设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会出现在 SSV 回调中。

以下代码示例演示了如何在请求广告之前对激励广告对象设置自定义数据。

Java

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

Kotlin

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

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

如果您要设置自定义奖励字符串,则必须在展示广告之前设置。

Câu hỏi thường gặp

Lệnh gọi khởi chạy có thời gian chờ không?
Sau 10 giây, SDK Quảng cáo của Google trên thiết bị di động sẽ gọi OnInitializationCompleteListener ngay cả khi mạng dàn xếp chưa hoàn thành quy trình khởi chạy.
Điều gì sẽ xảy ra nếu một số mạng dàn xếp chưa sẵn sàng khi tôi nhận được lệnh gọi lại khởi chạy?

Bạn nên tải quảng cáo bên trong lệnh gọi lại của OnInitializationCompleteListener. Ngay cả khi mạng dàn xếp chưa sẵn sàng hoạt động, SDK Quảng cáo của Google trên thiết bị di động vẫn yêu cầu mạng đó cung cấp một quảng cáo. Vì vậy, nếu quá trình khởi chạy kết thúc sau khi hết thời gian chờ, thì mạng dàn xếp vẫn có thể thực hiện các yêu cầu quảng cáo tiếp theo trong phiên hoạt động đó.

Bạn có thể tiếp tục thăm dò trạng thái khởi chạy của tất cả các bộ chuyển đổi trong suốt phiên hoạt động của ứng dụng bằng cách gọi MobileAds.getInitializationStatus().

Làm cách nào để biết lý do khiến một mạng dàn xếp cụ thể chưa sẵn sàng?

AdapterStatus.getDescription() cho biết lý do khiến một bộ chuyển đổi chưa sẵn sàng đáp ứng các yêu cầu quảng cáo.

Lệnh gọi lại onUserEarnedReward() có luôn được gọi trước lệnh gọi lại onAdDismissedFullScreenContent() không?

Đối với quảng cáo của Google, tất cả lệnh gọi onUserEarnedReward() đều xảy ra trước onAdDismissedFullScreenContent(). Đối với những quảng cáo được phân phát thông qua tính năng dàn xếp, hoạt động triển khai SDK mạng quảng cáo bên thứ ba sẽ xác định thứ tự lệnh gọi lại. Đối với các SDK của mạng quảng cáo cung cấp một lệnh gọi lại đóng có thông tin về phần thưởng, bộ chuyển đổi dàn xếp sẽ gọi onUserEarnedReward() trước onAdDismissedFullScreenContent().

Ví dụ trên GitHub

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

Các bước tiếp theo

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