Sự kiện tuỳ chỉnh cho quảng cáo có tặng thưởng

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

Hoàn tất quy trình thiết lập sự kiện tuỳ chỉnh.

Yêu cầu quảng cáo có tặng thưởng

Khi đạt đến mục hàng sự kiện tuỳ chỉnh trong chuỗi dàn xếp kiểu thác nước, the loadRewarded:adConfiguration:completionHandler: method sẽ được gọi dựa trên tên lớp mà bạn đã cung cấp khi tạo sự kiện tuỳ chỉnh. Trong trường hợp này, phương thức đó nằm trong SampleCustomEvent, sau đó sẽ gọithe loadRewarded:adConfiguration:completionHandler: method trong SampleCustomEventRewarded.

Để yêu cầu hiển thị quảng cáo có tặng thưởng, hãy tạo hoặc sửa đổi một lớp triển khai GADMediationAdapterloadRewarded:adConfiguration:completionHandler:. Nếu đã có một lớp mở rộng GADMediationAdapter, hãy triển khai loadRewarded:adConfiguration:completionHandler: ở đó. Ngoài ra, hãy tạo một lớp mới để triển khai GADMediationRewardedAd.

Trong ví dụ về sự kiện tuỳ chỉnh của chúng tôi, SampleCustomEvent sẽ triển khaithe GADMediationAdapter interface , sau đó uỷ quyền choSampleCustomEventRewarded.

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, GADMediationAdapter {

  fileprivate var rewardedAd: SampleCustomEventRewarded?
  ...

  func loadRewarded(
    for adConfiguration: GADMediationRewardedAdConfiguration,
    completionHandler: @escaping GADMediationRewardedLoadCompletionHandler
  ) {
    self.rewardedAd = SampleCustomEventRewarded()
    self.rewardedAd?.loadRewarded(
      for: adConfiguration, completionHandler: completionHandler)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent
...

SampleCustomEventRewarded *sampleRewarded;

- (void)loadRewardedForAdConfiguration:
            (GADMediationRewardedAdConfiguration *)adConfiguration
                     completionHandler:
                         (GADMediationRewardedLoadCompletionHandler)
                             completionHandler {
  sampleRewarded = [[SampleCustomEventRewarded alloc] init];
  [sampleRewarded loadRewardedForAdConfiguration:adConfiguration
                               completionHandler:completionHandler];
}

SampleCustomEventRewarded phụ trách những việc sau:

  • Đang tải quảng cáo có tặng thưởng

  • Triển khai GADMediationRewardedAd protocol

  • Nhận và báo cáo lệnh gọi lại sự kiện quảng cáo cho SDK quảng cáo trên thiết bị di động của Google

Thông số không bắt buộc được xác định trong giao diện người dùng AdMob được đưa vào cấu hình quảng cáo. Bạn có thể truy cập tham số này thông qua adConfiguration.credentials.settings[@"parameter"]. Thông số này thường là giá trị nhận dạng đơn vị quảng cáo mà SDK mạng quảng cáo cần đến khi tạo thực thể cho một đối tượng quảng cáo.

Swift

class SampleCustomEventRewarded: NSObject, GADMediationRewardedAd {
  /// The Sample Ad Network rewarded ad.
  var nativeAd: SampleRewarded?

  /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK.
  var delegate: GADMediationRewardedAdEventDelegate?

  /// Completion handler called after ad load.
  var completionHandler: GADMediationRewardedLoadCompletionHandler?

  func loadRewarded(
    for adConfiguration: GADMediationRewardedAdConfiguration,
    completionHandler: @escaping GADMediationRewardedLoadCompletionHandler
  ) {
    rewarded = SampleRewarded.init(
      adUnitID: adConfiguration.credentials.settings["parameter"] as? String)
    rewarded?.delegate = self
    let adRequest = SampleAdRequest()
    adRequest.testMode = adConfiguration.isTestRequest
    self.completionHandler = completionHandler
    rewarded?.fetchAd(adRequest)
  }
}

Objective-C

#import "SampleCustomEventRewarded.h"

@interface SampleCustomEventRewarded () <SampleRewardedAdDelegate,
                                         GADMediationRewardedAd> {
  /// The sample rewarded ad.
  SampleRewarded *_rewardedAd;

  /// The completion handler to call when the ad loading succeeds or fails.
  GADMediationRewardedLoadCompletionHandler _loadCompletionHandler;

  /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK.
  id <GADMediationRewardedAdEventDelegate> _adEventDelegate;
}
@end

- (void)loadRewardedAdForAdConfiguration:(GADMediationRewardedAdConfiguration *)adConfiguration
                       completionHandler:
                           (GADMediationRewardedLoadCompletionHandler)completionHandler {
  __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
  __block GADMediationRewardedLoadCompletionHandler originalCompletionHandler =
      [completionHandler copy];

  _loadCompletionHandler = ^id<GADMediationRewardedAdEventDelegate>(
      _Nullable id<GADMediationRewardedAd> ad, NSError *_Nullable error) {
    // Only allow completion handler to be called once.
    if (atomic_flag_test_and_set(&completionHandlerCalled)) {
      return nil;
    }

    id<GADMediationRewardedAdEventDelegate> delegate = nil;
    if (originalCompletionHandler) {
      // Call original handler and hold on to its return value.
      delegate = originalCompletionHandler(ad, error);
    }

    // Release reference to handler. Objects retained by the handler will also be released.
    originalCompletionHandler = nil;

    return delegate;
  };

  NSString *adUnit = adConfiguration.credentials.settings[@"parameter"];
  _rewardedAd = [[SampleRewardedAd alloc] initWithAdUnitID:adUnit];
  _rewardedAd.delegate = self;
  SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
  adRequest.testMode = adConfiguration.isTestRequest;
  [_rewardedAd fetchAd:adRequest];
}

Cho dù quảng cáo được tìm nạp thành công hay gặp lỗi, bạn vẫn sẽ gọi GADMediationRewardedLoadCompletionHandler. Trong trường hợp thành công, hãy truyền qua lớp triển khai GADMediationRewardedAd với giá trị nil cho thông số lỗi; trong trường hợp không thành công, hãy truyền lỗi mà bạn gặp phải.

Thông thường, các phương thức này được triển khai bên trong lệnh gọi lại từ SDK bên thứ ba mà bộ chuyển đổi của bạn triển khai. Trong ví dụ này, SDK mẫu có một SampleRewardedAdDelegate với các lệnh gọi lại có liên quan:

Swift

func rewardedDidLoad(_ interstitial: SampleRewarded) {
  if let handler = completionHandler {
    delegate = handler(self, nil)
  }
}

func rewarded(
  rewarded: SampleRewarded, didFailToLoadAdWith errorCode: SampleErrorCode
) {
  let error =
    SampleCustomEventUtils.SampleCustomEventErrorWithCodeAndDescription(
      code: SampleCustomEventErrorCode
        .SampleCustomEventErrorAdLoadFailureCallback,
      description:
        "Sample SDK returned an ad load failure callback with error code: \(errorCode)"
    )
  if let handler = completionHandler {
    delegate = handler(nil, error)
  }
}

Objective-C

- (void)rewardedDidLoad:(SampleRewarded *)rewarded {
  _adEventDelegate = _loadCompletionHandler(self, nil);
}

- (void)rewarded:(SampleInterstitial *)rewarded
    didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {
  NSError *error = SampleCustomEventErrorWithCodeAndDescription(
      SampleCustomEventErrorAdLoadFailureCallback,
      [NSString stringWithFormat:@"Sample SDK returned an ad load failure "
                                 @"callback with error code: %@",
                                 errorCode]);
  _adEventDelegate = _loadCompletionHandler(nil, error);
}

GADMediationrewardedAd yêu cầu triển khai một phương thức present(viewController:) để hiển thị quảng cáo:

Swift

func present(from viewController: UIViewController) {
  if let rewarded = rewarded, rewarded.isRewardedLoaded {
    rewarded.show()
  }
}

Objective-C

- (void)presentFromViewController:(UIViewController *)viewController {
  if ([_rewardedAd isRewardedLoaded]) {
    [_rewardedAd show];
  } else {
    NSError *error = SampleCustomEventErrorWithCodeAndDescription(
        SampleCustomEventErrorAdNotLoaded,
        [NSString stringWithFormat:
                      @"The rewarded ad failed to present because the ad was not loaded."]);
    [_adEventDelegate didFailToPresentWithError:error]
  }
}

Chuyển tiếp các sự kiện dàn xếp đến SDK quảng cáo trên thiết bị di động của Google

Sau khi bạn đã gọi GADMediationRewardedLoadCompletionHandler cùng với một quảng cáo đã tải, đối tượng uỷ quyền GADMediationRewardedAdEventDelegate được trả về có thể được bộ chuyển đổi sử dụng để chuyển tiếp các sự kiện trình bày từ SDK bên thứ ba đến SDK quảng cáo trên thiết bị di động của Google. Lớp SampleCustomEventRewarded triển khai giao thức SampleRewardedAdDelegate để chuyển tiếp lệnh gọi lại từ mạng quảng cáo mẫu đến SDK quảng cáo trên thiết bị di động của Google.

Điều quan trọng là sự kiện tuỳ chỉnh của bạn phải chuyển tiếp nhiều lệnh gọi lại này nhất có thể để ứng dụng của bạn nhận được các sự kiện tương đương này từ SDK quảng cáo trên thiết bị di động của Google. Dưới đây là ví dụ về cách sử dụng lệnh gọi lại:

Swift

func rewardedAdDidPresent(_ rewarded: SampleRewardedAd) {
  delegate?.willPresentFullScreenVideo()
  delegate?.didStartVideo()
}

func rewardedAdUserDidEarnReward(_ rewarded: SampleRewardedAd) {
  GADAdReward aReward = GADAdReward("", rewarded)
  delegate.didRewardUser()
}

Objective-C

- (void)rewardedAdDidPresent:(SampleRewardedAd *)rewardedAd {
  [_adEventDelegate willPresentFullScreenView];
  [_adEventDelegate didStartVideo];
}

- (void)rewardedAd:(nonnull SampleRewardedAd *)rewardedAd
    userDidEarnReward:(NSUInteger)reward {
  GADAdReward *aReward = [[GADAdReward alloc]
      initWithRewardType:@""
            rewardAmount:[NSDecimalNumber numberWithUnsignedInt:reward]];
  [_adEventDelegate didRewardUserWithReward];
}

Đến đây, bạn đã hoàn thành việc triển khai sự kiện tuỳ chỉnh cho quảng cáo có tặng thưởng. Bạn có thể xem toàn bộ ví dụ trên GitHub. Bạn có thể sử dụng đơn vị quảng cáo này với một mạng quảng cáo đã được hỗ trợ hoặc sửa đổi quảng cáo đó để hiển thị quảng cáo có tặng thưởng của sự kiện tuỳ chỉnh.