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

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
  • [Không bắt buộc] Xác thực lệnh gọi lại của SSO
  • Đăng ký các lệnh gọi lại
  • Hiển thị quảng cáo và xử lý sự kiện tặng thưởng

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 tĩnh Phương thức loadWithAdUnitID:request:completionHandler: trên Lớp GADRewardedInterstitialAd. Phương thức tải này yêu cầu bạn phải có mã đơn vị quảng cáo, Đối tượng GAMRequest và một trình xử lý hoàn thành được gọi khi tải quảng cáo thành công hoặc không thành công. Các URL đã tải Đối tượng GADRewardedInterstitialAd được cung cấp dưới dạng tham số trong phần hoàn thành trình xử lý. Ví dụ sau đây trình bày cách tải một GADRewardedInterstitialAd trong lớp ViewController của bạn.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
    } catch {
      print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

Objective-C

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"<var label='the ad unit ID'>/21775744923/example/rewarded_interstitial</var>"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

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

Swift

do {
  rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
    withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
  let options = GADServerSideVerificationOptions()
  options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
  rewardedInterstitialAd.serverSideVerificationOptions = options
} catch {
  print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}

Objective-C

[GADRewardedInterstitialAd
    loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
              request:GAMRequest
    completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
      if (error) {
        // Handle Error
        return;
      }
      self.rewardedInterstitialAd = ad;
      GADServerSideVerificationOptions *options =
          [[GADServerSideVerificationOptions alloc] init];
      options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
      ad.serverSideVerificationOptions = options;
    }];

Đăng ký các lệnh gọi lại

Để nhận thông báo về các sự kiện trình bày, bạn phải triển khai giao thức GADFullScreenContentDelegate và chỉ định giao thức đó cho thuộc tính fullScreenContentDelegate của quảng cáo được trả về. Chiến lược phát hành đĩa đơn Giao thức GADFullScreenContentDelegate 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 bị loại bỏ. Nội dung sau đây mã cho biết cách triển khai giao thức và chỉ định giao thức đó cho quảng cáo:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
      self.rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }

  /// Tells the delegate that the ad failed to present full screen content.
  func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    print("Ad did fail to present full screen content.")
  }

  /// Tells the delegate that the ad will present full screen content.
  func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad will present full screen content.")
  }

  /// Tells the delegate that the ad dismissed full screen content.
  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did dismiss full screen content.")
  }
}

Objective-C

@interface ViewController () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADRewardedInterstitialAd *rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd,
          NSError *_Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
          self.rewardedInterstitialAd.fullScreenContentDelegate = self;
        }
      }];
}

/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
    NSLog(@"Ad did fail to present full screen content.");
}

/// Tells the delegate that the ad will present full screen content.
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {

    NSLog(@"Ad will present full screen content.");
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  NSLog(@"Ad did dismiss full screen content.");
}

Hiển thị quảng cáo và xử lý sự kiện tặng thưởng

Khi hiển thị quảng cáo, bạn phải cung cấp đối tượng GADUserDidEarnRewardHandler để xử lý phần thưởng cho người dùng.

Đoạn mã sau đây trình bày phương pháp tốt nhất để hiển thị quảng cáo có tặng thưởng quảng cáo xen kẽ.

Swift

func show() {
  guard let rewardedInterstitialAd = rewardedInterstitialAd else {
    return print("Ad wasn't ready.")
  }

  // The UIViewController parameter is an optional.
  rewardedInterstitialAd.present(fromRootViewController: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    // TODO: Reward the user.
  }
}

Objective-C

- (void)show {
  // The UIViewController parameter is nullable.
  [_rewardedInterstitialAd presentFromRootViewController:nil
                                userDidEarnRewardHandler:^{

                                  GADAdReward *reward =
                                      self.rewardedInterstitialAd.adReward;
                                  // TODO: Reward the user.
                                }];
}

Các bước tiếp theo

Tìm hiểu thêm về quyền riêng tư của người dùng.