โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

เลือกแพลตฟอร์ม: Android iOS Unity Flutter

โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลเป็นรูปแบบโฆษณาที่เสนอสิ่งจูงใจประเภทหนึ่งที่ช่วยให้คุณนำเสนอรางวัลสำหรับโฆษณาที่ปรากฏโดยอัตโนมัติระหว่างจุดเปลี่ยนหน้าตามปกติของแอป ผู้ใช้ไม่จําเป็นต้องเลือกดูโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล ซึ่งต่างจากโฆษณาที่มีการให้รางวัล

ข้อกำหนดเบื้องต้น

การใช้งาน

ขั้นตอนหลักในการผสานรวมโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลมีดังนี้

  • โหลดโฆษณา
  • [ไม่บังคับ] ตรวจสอบการเรียกกลับ SSV
  • ลงทะเบียนเพื่อรับการโทรกลับ
  • แสดงโฆษณาและจัดการเหตุการณ์รางวัล

โหลดโฆษณา

การโหลดโฆษณาทำได้โดยใช้เมธอด load(adUnitID:request) ในคลาส GADRewardedInterstitialAd

Swift

func loadRewardedInterstitialAd() async {
  do {
    rewardedInterstitialAd = try await RewardedInterstitialAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "adUnitID", request: Request())
    rewardedInterstitialAd?.fullScreenContentDelegate = self
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}

SwiftUI

import GoogleMobileAds

class RewardedInterstitialViewModel: NSObject, ObservableObject,
  FullScreenContentDelegate
{
  @Published var coins = 0
  private var rewardedInterstitialAd: RewardedInterstitialAd?

  func loadAd() async {
    do {
      rewardedInterstitialAd = try await RewardedInterstitialAd.load(
        with: "ca-app-pub-3940256099942544/6978759866", request: Request())
      rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print(
        "Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

- (void)loadRewardedInterstitialAd {
  [GADRewardedInterstitialAd loadWithAdUnitID:"adUnitID"
                                      request:[GADRequest request]
                            completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
                              if (error) {
                                NSLog(@"Failed to load rewarded interstitial ad with error: %@",
                                      error.localizedDescription);
                                return;
                              }
                              self.rewardedInterstitialAd = ad;
                              self.rewardedInterstitialAd.fullScreenContentDelegate = self;
                            }];
}

แทนที่ adUnitID ด้วยรหัสหน่วยโฆษณา

[ไม่บังคับ] ตรวจสอบความถูกต้องของ Callback การยืนยันฝั่งเซิร์ฟเวอร์ (SSV)

แอปที่ต้องการข้อมูลเพิ่มเติมในการเรียกกลับการยืนยันฝั่งเซิร์ฟเวอร์ควรใช้ ฟีเจอร์ข้อมูลที่กำหนดเองของโฆษณาที่มีการให้รางวัล ค่าสตริงที่ตั้งค่าไว้ในออบเจ็กต์โฆษณาที่มีการให้รางวัล จะส่งไปยังพารามิเตอร์การค้นหา custom_data ของการเรียกกลับ SSV หากไม่ได้ตั้งค่าข้อมูลที่กำหนดเอง ค่าพารามิเตอร์การค้นหาcustom_dataจะไม่ อยู่ในการเรียกกลับ SSV

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าข้อมูลที่กำหนดเองในออบเจ็กต์โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล ก่อนที่จะขอโฆษณา

Swift

private func validateServerSideVerification() async {
  do {
    rewardedInterstitialAd = try await RewardedInterstitialAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "adUnitID", request: Request())
    let options = ServerSideVerificationOptions()
    options.customRewardText = ""SAMPLE_CUSTOM_DATA_STRING""
    rewardedInterstitialAd?.serverSideVerificationOptions = options
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}

Objective-C

- (void)validateServerSideVerification {
  // Replace this ad unit ID with your own ad unit ID.
  [GADRewardedInterstitialAd loadWithAdUnitID:"adUnitID"
                                      request:[GADRequest request]
                            completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
                              if (error) {
                                NSLog(@"Rewarded interstitial ad failed to load with error: %@",
                                      error.localizedDescription);
                                return;
                              }
                              self.rewardedInterstitialAd = ad;
                              GADServerSideVerificationOptions *options =
                                  [[GADServerSideVerificationOptions alloc] init];
                              options.customRewardString = @""SAMPLE_CUSTOM_DATA_STRING"";
                              ad.serverSideVerificationOptions = options;
                            }];
}

แทนที่ SAMPLE_CUSTOM_DATA_STRING ด้วยข้อมูลที่กำหนดเอง

ลงทะเบียนเพื่อรับการโทรกลับ

หากต้องการรับการแจ้งเตือนสำหรับเหตุการณ์การนำเสนอ คุณต้องกำหนด GADFullScreenContentDelegateให้กับพร็อพเพอร์ตี้ fullScreenContentDelegate ของ โฆษณาที่แสดง

Swift

rewardedInterstitialAd?.fullScreenContentDelegate = self

SwiftUI

rewardedInterstitialAd?.fullScreenContentDelegate = self

Objective-C

self.rewardedInterstitialAd.fullScreenContentDelegate = self;

GADFullScreenContentDelegate โปรโตคอลจะจัดการการเรียกกลับเมื่อโฆษณา แสดงสำเร็จหรือไม่สำเร็จ และเมื่อมีการปิดโฆษณา โค้ดต่อไปนี้ แสดงวิธีใช้โปรโตคอลและกำหนดให้กับโฆษณา

Swift

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
  // Clear the rewarded interstitial ad.
  rewardedInterstitialAd = nil
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called with error: \(error.localizedDescription).")
}

SwiftUI

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the rewarded interstitial ad.
  rewardedInterstitialAd = nil
}

Objective-C

- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the rewarded interstitial ad.
  self.rewardedInterstitialAd = nil;
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}

แสดงโฆษณาและจัดการเหตุการณ์รางวัล

เมื่อแสดงโฆษณา คุณต้องระบุออบเจ็กต์ GADUserDidEarnRewardHandler เพื่อจัดการรางวัลสำหรับผู้ใช้

โค้ดต่อไปนี้แสดงวิธีที่ดีที่สุดในการแสดงโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

Swift

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

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

SwiftUI

รอเหตุการณ์ UI ในมุมมองเพื่อแสดงโฆษณา

var rewardedInterstitialBody: some View {
  // ...
  }
  .onChange(
    of: showAd,
    perform: { newValue in
      if newValue {
        viewModel.showAd()
      }
    }
  )

แสดงโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลจาก ViewModel

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

  rewardedInterstitialAd.present(from: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}

Objective-C

- (void)showRewardedInterstitialAd {
  [self.rewardedInterstitialAd presentFromRootViewController:self
                                    userDidEarnRewardHandler:^{
                                      GADAdReward *reward = self.rewardedInterstitialAd.adReward;

                                      NSString *rewardMessage = [NSString
                                          stringWithFormat:@"Reward received with "
                                                           @"currency %@ , amount %ld",
                                                           reward.type, [reward.amount longValue]];
                                      NSLog(@"%@", rewardMessage);
                                      // TODO: Reward the user.
                                    }];
}

ตัวอย่างใน GitHub

ดูตัวอย่างโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลทั้งหมดในภาษาที่คุณต้องการ

ขั้นตอนถัดไป

ดูข้อมูลเพิ่มเติมเกี่ยวกับความเป็นส่วนตัวของผู้ใช้