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

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

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

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

  • SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google เวอร์ชัน 19.2.0 ขึ้นไป

การใช้งาน

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

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

โหลดโฆษณา

การโหลดโฆษณาจะทำได้โดยใช้วิธี load() แบบคงที่ในคลาส RewardedInterstitialAd เมธอดโหลดต้องมี Context, รหัสหน่วยโฆษณา, ออบเจ็กต์ AdRequest และ RewardedInterstitialAdLoadCallback เพื่อรับการแจ้งเตือนเมื่อโหลดโฆษณาสำเร็จหรือไม่สำเร็จ ออบเจ็กต์ RewardedInterstitialAd ที่โหลดจะระบุเป็นพารามิเตอร์ใน Callback onRewardedInterstitialAdLoaded()

ตัวอย่างต่อไปนี้แสดงวิธีโหลด RewardedInterstitialAd ใน MainActivity

Java

RewardedInterstitialAd.load(
    MainActivity.this,
    "AD_UNIT_ID",
    new AdRequest.Builder().build(),
    new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        Log.d(TAG, "Ad was loaded.");
        rewardedInterstitialAd = ad;
      }

      @Override
      public void onAdFailedToLoad(LoadAdError loadAdError) {
        Log.d(TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
        rewardedInterstitialAd = null;
      }
    });

Kotlin

RewardedInterstitialAd.load(
  this,
  "AD_UNIT_ID",
  AdRequest.Builder().build(),
  object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(rewardedAd: RewardedInterstitialAd) {
      Log.d(TAG, "Ad was loaded.")
      rewardedInterstitialAd = rewardedAd
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      Log.d(TAG, "onAdFailedToLoad: ${adError.message}")
      rewardedInterstitialAd = null
    }
  },
)

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

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

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

Java

rewardedInterstitialAd.setFullScreenContentCallback(
    new FullScreenContentCallback() {
      @Override
      public void onAdDismissedFullScreenContent() {
        // Called when fullscreen content is dismissed.
        Log.d(TAG, "The ad was dismissed.");
        // Make sure to set your reference to null so you don't
        // show it a second time.
        rewardedInterstitialAd = null;
        if (googleMobileAdsConsentManager.canRequestAds()) {
          loadRewardedInterstitialAd();
        }
      }

      @Override
      public void onAdFailedToShowFullScreenContent(AdError adError) {
        // Called when fullscreen content failed to show.
        Log.d(TAG, "The ad failed to show.");
        // Make sure to set your reference to null so you don't
        // show it a second time.
        rewardedInterstitialAd = null;
      }

      @Override
      public void onAdShowedFullScreenContent() {
        // Called when fullscreen content is shown.
        Log.d(TAG, "The ad was shown.");
      }

      @Override
      public void onAdImpression() {
        // Called when an impression is recorded for an ad.
        Log.d(TAG, "The ad recorded an impression.");
      }

      @Override
      public void onAdClicked() {
        // Called when ad is clicked.
        Log.d(TAG, "The ad was clicked.");
      }
    });

Kotlin

rewardedInterstitialAd?.fullScreenContentCallback =
  object : FullScreenContentCallback() {
    override fun onAdDismissedFullScreenContent() {
      // Called when fullscreen content is dismissed.
      Log.d(TAG, "Ad was dismissed.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      rewardedInterstitialAd = null
    }

    override fun onAdFailedToShowFullScreenContent(adError: AdError) {
      // Called when fullscreen content failed to show.
      Log.d(TAG, "Ad failed to show.")
      // Don't forget to set the ad reference to null so you
      // don't show the ad a second time.
      rewardedInterstitialAd = null
    }

    override fun onAdShowedFullScreenContent() {
      // Called when fullscreen content is shown.
      Log.d(TAG, "Ad showed fullscreen content.")
    }

    override fun onAdImpression() {
      // Called when an impression is recorded for an ad.
      Log.d(TAG, "Ad recorded an impression.")
    }

    override fun onAdClicked() {
      // Called when an ad is clicked.
      Log.d(TAG, "Ad was clicked.")
    }
  }

แสดงโฆษณา

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

Java

rewardedInterstitialAd.show(
    MainActivity.this,
    new OnUserEarnedRewardListener() {
      @Override
      public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
        Log.d(TAG, "The user earned the reward.");
        // Handle the reward.
        int rewardAmount = rewardItem.getAmount();
        String rewardType = rewardItem.getType();
      }
    });

Kotlin

rewardedInterstitialAd?.show(this) { rewardItem ->
  Log.d(TAG, "User earned the reward.")
  // Handle the reward.
  val rewardAmount = rewardItem.amount
  val rewardType = rewardItem.type
}

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

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

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

Java

RewardedInterstitialAd.load(
    context,
    AD_UNIT_ID,
    new AdRequest.Builder().build(),
    new RewardedInterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(RewardedInterstitialAd ad) {
        rewardedInterstitialAd = ad;
        ServerSideVerificationOptions options =
            new ServerSideVerificationOptions.Builder()
                .setCustomData("SAMPLE_CUSTOM_DATA_STRING")
                .build();
        rewardedInterstitialAd.setServerSideVerificationOptions(options);
      }
    });

Kotlin

RewardedInterstitialAd.load(
  context,
  AD_UNIT_ID,
  AdRequest.Builder().build(),
  object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      rewardedInterstitialAd = ad
      val options =
        ServerSideVerificationOptions.Builder().setCustomData("SAMPLE_CUSTOM_DATA_STRING").build()
      rewardedInterstitialAd?.setServerSideVerificationOptions(options)
    }
  },
)

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

หากต้องการตั้งค่าสตริงรางวัลที่กำหนดเอง คุณต้องตั้งค่าก่อนแสดงโฆษณา

ตัวอย่างใน GitHub

  • ตัวอย่างโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล Java | Kotlin

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

ดูหัวข้อต่อไปนี้