Cordova在Click上显示InterstitialAd

时间:2017-01-29 09:35:25

标签: javascript cordova cordova-plugins cordova-admob

我正在使用appfeel cordova admob插件。 https://github.com/appfeel/admob-google-cordova

我想在我的cordova应用上显示点击功能的InterstitialAd。通常使用默认选项,InterstitialAd会在我的应用上显示。但是点击按钮时必须显示InterstitialAd。

我的默认选项

admob.setOptions({
            publisherId: "xxxx-myidxxx", // Required
            interstitialAdId: "xxx-myidxxx", // Optional
            tappxIdiOs: "/XXXXXXXXX/Pub-XXXX-iOS-IIII", // Optional
            tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA", // Optional
            tappxShare: 0.5,
            adSize: admob.AD_SIZE.SMART_BANNER,
            bannerAtTop: false,
            overlap: false,
            offsetStatusBar: false,
            isTesting: true,
            adExtras : {},
            autoShowBanner: true,
            autoShowInterstitial: false // Optional
});

我可以调用此类型的函数

$( "#myButton" ).on( "click", function() {
           admob.requestInterstitial();
           admob.showInterstitialAd();
});

我正在尝试调用此类型的函数,但它不起作用。

按钮点击时如何调用功能?

1 个答案:

答案 0 :(得分:1)

请参阅此处:https://github.com/appfeel/admob-google-cordova/wiki/showInterstitialAd,有关于如何展示插页式广告的完整示例:

基本上,您所做的就是在设备准备就绪时使用autoShowInterstitial: falserequestInterstitialAd启动广告,在点击事件发生时显示此预加载的插页式广告,并在显示最后一个插页式广告时重新加载新的插页式广告:

var isAppForeground = true;
var isInterstitialReady = true;

function onAdLoaded(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      isInterstitialReady = true;
    }
  }
}

function onAdClosed(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      isInterstitialReady = false;
      admob.requestInterstitialAd();
    }
  }
}

function onPause() {
  if (isAppForeground) {
    admob.destroyBannerView();
    isAppForeground = false;
  }
}

function onResume() {
  if (!isAppForeground) {
    setTimeout(admob.createBannerView, 1);
    setTimeout(admob.requestInterstitialAd, 1);
    isAppForeground = true;
  }
}

// optional, in case respond to events
function registerAdEvents() {
  document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
  document.addEventListener(admob.events.onAdClosed, onAdClosed);

  document.addEventListener("pause", onPause, false);
  document.addEventListener("resume", onResume, false);
}

function initAds() {
  if (admob) {
    var adPublisherIds = {
      ios : {
        banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
        interstitial : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
      },
      android : {
        banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
        interstitial : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
      }
    };

    var admobid = (/(android)/i.test(navigator.userAgent)) ? adPublisherIds.android : adPublisherIds.ios;

    admob.setOptions({
      publisherId:          admobid.banner,
      interstitialAdId:     admobid.interstitial,
      autoShowInterstitial: false
    });

    registerAdEvents();

  } else {
    alert('AdMobAds plugin not ready');
  }
}

function onDeviceReady() {
  document.removeEventListener('deviceready', onDeviceReady, false);
  initAds();

  // display a banner at startup
  admob.createBannerView();

  // request an interstitial
  admob.requestInterstitialAd();
}

document.addEventListener("deviceready", onDeviceReady, false);

然后在你的按钮事件中:

$("#myButton").on( "click", function() {
    if (isInterstitialReady) {
        admob.showInterstitialAd();
    } else {
        // We do not have an interstitial ready, try to request a new one
        // It can be mainly because of 3 reasons:
        //   - Can't connect with Admob (i.e. no connectivity available)
        //   - Admob does not have available any interstitials at this moment
        //   - Not enought time between last requestInterstitialAd() and myButton.click()
        admob.requestInterstitialAd();
    }
});