插屏广告

插屏广告一般采用全屏或整页形式,会暂时覆盖应用的整个界面,通常出现在应用自然的暂停或过渡节点,例如游戏关卡完成后、主要视图切换时等。

以下各节将向您介绍如何加载和显示插屏广告。

加载插屏广告

以下代码展示了如何附加监听器以及加载第一则插屏广告:

import { InterstitialAd } from 'react-native-applovin-max';

const INTERSTITIAL_AD_UNIT_ID = Platform.select({
  android: '«android-ad-unit-ID»',
  ios: '«ios-ad-unit-ID»',
});

const MAX_EXPONENTIAL_RETRY_COUNT = 6;
const retryAttempt = useRef(0);

const initializeInterstitialAds = () => {
  InterstitialAd.addAdLoadedEventListener((adInfo: AdInfo) => {
    // Interstitial ad is ready to show. InterstitialAd.isReady(INTERSTITIAL_AD_UNIT_ID) now returns 'true'

    // Reset retry attempt
    retryAttempt.current = 0;
  });
  InterstitialAd.addAdLoadFailedEventListener((errorInfo: AdLoadFailedInfo) => {
    // Interstitial ad failed to load
    // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)

    retryAttempt.current += 1;
    if (retryAttempt.current > MAX_EXPONENTIAL_RETRY_COUNT) return;
    const retryDelay = Math.pow(2, Math.min(MAX_EXPONENTIAL_RETRY_COUNT, retryAttempt.current));

    console.log('Interstitial ad failed to load - retrying in ' + retryDelay + 's');

    setTimeout(function() {
      loadInterstitial();
    }, retryDelay * 1000);
  });
  InterstitialAd.addAdClickedEventListener((adInfo: AdInfo) => { ... });
  InterstitialAd.addAdDisplayedEventListener((adInfo: AdInfo) => { ... });
  InterstitialAd.addAdFailedToDisplayEventListener((adInfo: AdDisplayFailedInfo) = {
    // Interstitial ad failed to display. AppLovin recommends that you load the next ad
    loadInterstitial();
  });
  InterstitialAd.addAdHiddenEventListener((adInfo: AdInfo) => {
    loadInterstitial();
  });

  // Load the first interstitial
  loadInterstitial();
}

const loadInterstitial = () => {
  InterstitialAd.loadAd(INTERSTITIAL_AD_UNIT_ID);
}

显示插屏广告

调用 showAd() 以显示插屏广告:

const isInterstitialReady = await InterstitialAd.isAdReady(«ad-unit-ID»);
if (isInterstitialReady) {
  InterstitialAd.showAd(«ad-unit-ID»);
}

这篇文章有帮助吗?
这篇文章有帮助吗?
search