Admob插件无法与Phonegap应用配合使用

时间:2018-12-14 16:30:18

标签: javascript admob hybrid-mobile-app phonegap banner-ads

希望您能帮助我解决这个问题。最近5天我都尝试过,但每次都失败。 我已经使用HTML5,CSS和一点点Javascript构建了一个简单的应用程序。该应用程序运行良好。我不想要一个简单的横幅广告,该横幅广告会显示在应用程序底部,但我无法正常工作。有时,底部会显示一个黑框,仅此而已,但没有广告。我对Javascript不太满意,所以问题出在代码的问题上,但是我已经从插件示例中复制了大部分代码。

来源:https://www.npmjs.com/package/phonegap-admob

我正在使用: -Phonegap 6.1.2(构建服务) -admob-phonegap插件(<plugin name="phonegap-admob" spec="4.2.1" />) -其他工作正常的插件。

我需要什么: -页面底部只有一个小横幅广告。

平台: -只有Andriod

在我的config.xml中,有以下一行:

<plugin name="phonegap-admob" spec="4.2.1" />

在我的index.html <script></script>标记内,我有以下代码:

<script>
var isAppForeground = true;

    function initAds() {
      if (admob) {
        var adPublisherIds = {
          android : {
            banner : "ca-app-pub-1467389904102750/404191XXXX",
          }
        };

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

        admob.setOptions({
          publisherId:      admobid.banner,
          interstitialAdId: admobid.interstitial,

        });

        registerAdEvents();

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

    function onAdLoaded(e) {
      if (isAppForeground) {
        if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
          console.log("An interstitial has been loaded and autoshown. If you want to load the interstitial first and show it later, set 'autoShowInterstitial: false' in admob.setOptions() and call 'admob.showInterstitialAd();' here");
        } else if (e.adType === admob.AD_TYPE_BANNER) {
          console.log("New banner received");
        }
      }
    }

    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.onAdFailedToLoad, function (e) {});
      document.addEventListener(admob.events.onAdOpened, function (e) {});
      document.addEventListener(admob.events.onAdClosed, function (e) {});
      document.addEventListener(admob.events.onAdLeftApplication, function (e) {});

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

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

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

    }

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

 </script>     

我希望有人能帮助我。非常感谢

1 个答案:

答案 0 :(得分:0)

好的,整个代码是正确的。问题是我没有在Script标签中链接到Cordova。我在您不需要的地方阅读了它,Phonegap / Cordova会自动包含它。这就是所需要的。您是否要显示Admob(可能还有许多其他插件),请添加

<script type = "text / javascript" src = "cordova.js"> </script>.