Ionic 2视频广告插件

时间:2017-07-03 07:12:04

标签: javascript cordova ionic-framework ionic2 admob

我正在尝试为离子2的广告显示(视频)找到一些好的插件。我已尝试使用https://github.com/cranberrygame/cordova-plugin-ad-adcolony,但我无法调整它以使用ionic2和angular2。我还审核了AdMob,它是ionic2插件,但不支持视频广告,因为我读过(仅限横幅广告)。对于我的ionic2应用程序,你能告诉我我应该使用什么以及如何使用。感谢。

顺便说一下,当我尝试以window.plugins.adcolony.showIntersitialAd()方式运行adcolony时,使用下面的HTML

<button ion-button (click)="window.plugins.adcolony.showInterstitialAd();">showInterstitialAd</button>

它返回

HomePage.html:15 ERROR TypeError: Cannot read property 'plugins' of undefined

当我试图像这样运行它时

<button ion-button (click)="window.adcolony.showInterstitialAd();">showInterstitialAd</button>

它返回以下错误:

HomePage.html:15 ERROR TypeError: Cannot read property 'adcolony' of undefined

任何帮助将不胜感激,谢谢:)

顺便说一句,我正在通过https://apps.ionic.io/apps

测试应用

1 个答案:

答案 0 :(得分:1)

尝试使用此插件:https://github.com/appfeel/admob-google-cordova

cordova plugin add cordova-admob

请注意,您必须等待加载插页式广告,并且最好在您的应用处于后台时禁用插页式加载,因为在您请求插页式广告与展示插页式广告的那一刻之间会有延迟:

<button ion-button (click)="tryToShowInterstitial();">showInterstitialAd</button>

在你的javascript代码中:

var isAppForeground = true;
var isInterstitialAvailable = false;

function tryToShowInterstitial() {
    if (isInterstitialAvailable) {
        admob.showInterstitialAd();
    } else {
        // Do your button action here when there are no ads to show
    }
}

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

function onAdClosed(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      // Would you like to prepare next interstitial?
      setTimeout(admob.requestInterstitialAd, 1);
      isInterstitialAvailable = false;
    }
  }
}

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);