보상형 전면 광고 (베타)

보상형 전면 광고는 자연스러운 앱 전환 중에 자동으로 게재되는 광고에 대해 보상을 제공하는 인센티브형 광고 형식입니다. 보상형 광고와 달리 사용자는 수신 동의하지 않고도 보상형 전면 광고를 볼 수 있습니다.

기본 요건

  • Google 모바일 광고 SDK 19.2.0 이상
  • 시작 가이드를 모두 읽어보세요.

구현

보상형 전면 광고를 통합하는 기본 단계는 다음과 같습니다.

  • 광고 로드
  • 전체 화면 이벤트 콜백 등록
  • 보상 콜백 처리
  • 광고 게재
  • [선택사항] SSV 콜백 확인

광고 로드

광고는 RewardedInterstitialAd 클래스의 정적 load() 메서드를 사용하여 로드됩니다. 로드 메서드에는 컨텍스트, 광고 단위 ID, AdRequest 객체, 광고 로드에 성공하거나 실패할 때 알림을 받을 RewardedInterstitialAdLoadCallback가 필요합니다. 로드된 RewardedInterstitialAd 객체는 onRewardedInterstitialAdLoaded() 콜백의 매개변수로 제공됩니다.

다음 예는 MainActivity에서 RewardedInterstitialAd를 로드하는 방법을 보여줍니다.

Java

public class MainActivity extends AppCompatActivity {
  private RewardedInterstitialAd rewardedInterstitialAd;
  private String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              MobileAds.initialize(this, initializationStatus -> {});
              // Load an ad on the main thread.
              runOnUiThread(
                  () -> {
                    loadAd();
                  });
            })
        .start();
  }

  public void loadAd() {
    // Use the test ad unit ID to load an ad.
    RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
        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, loadAdError.toString());
        rewardedInterstitialAd = null;
      }
    });
  }
}

Kotlin

import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback

class MainActivity : AppCompactActivity() {

  private var rewardedInterstitialAd? = null
  private final var TAG = "MainActivity"

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      // Initialize the Google Mobile Ads SDK on a background thread.
      MobileAds.initialize(this@MainActivity) {}
      // Load an ad on the main thread.
      runOnUiThread {
        loadAd()
      }
    }
  }

  private fun loadAd() {
    RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
      AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
      override fun onAdLoaded(ad: RewardedInterstitialAd) {
        Log.d(TAG, "Ad was loaded.")
        rewardedInterstitialAd = ad
      }

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

콜백 등록

표시 이벤트에 대한 알림을 받으려면 FullScreenContentCallback 객체를 광고의 setter에 전달해야 합니다. FullScreenContentCallback 객체는 광고 표시 성공 및 실패 시와 광고가 닫혔을 때의 콜백을 처리합니다. 다음 코드는 RewardedInterstitialAdLoadCallback 내에서 익명의 FullScreenContentCallback 객체를 설정하는 방법을 보여줍니다.

Java

public void loadAd(){
  RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
      new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
    @Override
    public void onAdLoaded(RewardedInterstitialAd ad) {
      rewardedInterstitialAd = ad;
      rewardedInterstitialAd.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.");
          rewardedInterstitialAd = null;
        }

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

Kotlin

private fun loadAd() {
  RewardedInterstitialAd.load(this, "ca-app-pub-3940256099942544/5354046379",
    AdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
    override fun onAdLoaded(ad: RewardedInterstitialAd) {
      rewardedInterstitialAd = ad
      rewardedInterstitialAd?.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.")
          rewardedInterstitialAd = null
        }

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

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

보상 처리

보상형 전면 광고를 표시하려면 사용자가 보상을 받을 때 알림을 받도록 MainActivity에서 OnUserEarnedRewardListener 인터페이스를 구현합니다.

Java

public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
  ...
  @Override
  public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
    Log.i(TAG, "User earned reward.");
    // TODO: Reward the user!
  }
}

Kotlin

class MainActivity : AppCompatActivity(), OnUserEarnedRewardListener {
  ...
  override fun onUserEarnedReward(rewardItem: RewardItem) {
    Log.d(TAG, "User earned reward.")
    // TODO: Reward the user!
  }
}

광고 게재

OnUserEarnedRewardListener 인터페이스를 구현한 후 다음과 같이 광고의 show() 메서드를 사용하여 광고를 표시할 수 있습니다.

Java

rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
    OnUserEarnedRewardListener */ MainActivity.this);

Kotlin

rewardedInterstitialAd?.show(/* Activity */ this, /*
    OnUserEarnedRewardListener */ this)

[선택사항] 서버 측 확인 (SSV) 콜백 확인

서버 측 확인 콜백에서 추가 데이터가 필요한 앱은 보상형 광고의 맞춤 데이터 기능을 사용해야 합니다. 보상형 광고 객체에 설정된 모든 문자열 값은 SSV 콜백의 custom_data 쿼리 매개변수에 전달됩니다. 맞춤 데이터 값이 설정되지 않은 경우 custom_data 쿼리 매개변수 값은 SSV 콜백에 표시되지 않습니다.

다음 코드 샘플은 광고를 요청하기 전에 보상형 전면 광고 객체에 맞춤 데이터를 설정하는 방법을 보여줍니다.

Java

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

Kotlin

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

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

맞춤 보상 문자열을 설정하려면 광고를 게재하기 전에 해야 합니다.

GitHub의 예

다음 단계

다음 주제를 살펴보세요.