【问题标题】:Cordova Show InterstitialAd on ClickCordova 在点击时显示插页式广告
【发布时间】:2017-01-29 09:35:24
【问题描述】:

我正在使用 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();
});

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

点击按钮时如何调用函数?

【问题讨论】:

    标签: javascript cordova cordova-plugins cordova-admob


    【解决方案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();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多