AdMob不会在离子/角度应用中加载广告 [英] AdMob not loading ads in ionic/angular app

查看:283
本文介绍了AdMob不会在离子/角度应用中加载广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用广告时遇到问题。我不能说出我的大脑为什么它不工作。我安装了以下插件:

I'm having some problems getting ads to work. I can't rap my brain around why it isn't working. I have the following plugins installed:

com.google.playservices 19.0.0Google Play Services for Android
com.rjfun.cordova.plugin.admob 2.1.7AdMob
我已使用本教程: https:// blog.nraboy.com/2014/06/using-admob-ionicframework/1

com.google.playservices 19.0.0 "Google Play Services for Android" com.rjfun.cordova.plugin.admob 2.1.7 "AdMob" I've have used this tutorial: https://blog.nraboy.com/2014/06/using-admob-ionicframework/1

这是我在apps.js中的代码:

This is my code in the apps.js:

 .run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        // select the right Ad Id according to platform
        if (window.plugins && window.plugins.AdMob) {
            var admob_key = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
            var admob = window.plugins.AdMob;
            admob.createBannerView(
                {
                    'publisherId': admob_key,
                    'adSize': admob.AD_SIZE.BANNER,
                    'bannerAtTop': false
                },
                function () {
                    admob.requestAd(
                        {'isTesting': false},
                        function () {
                            admob.showAd(true);
                        },
                        function () {
                            console.log('failed to request ad');
                        }
                    );
                },
                function () {
                    console.log('failed to create banner view');
                }
            );
        }
    });
});

区域,但不会加载任何广告。这段代码目前是活的,在admob网站上我可以看到几百个会话。然而,我的印象和请求rpm都在零几天了。

This results in a black banner area at the bottom of the app, however no ad is ever loaded. This code is currently live, on the admob site I can see a couple hundred session. However my impressions and Request rpm are both at zero for a couple of days now. Anybody have any idea what might be wrong?

推荐答案

我也花了两天时间使它工作,阅读了很多docs得到它的工作终于。下面的代码为我工作。我已经写了详细的博客文章和工作代码下载以及工作apk。 在此阅读或按以下步骤操作(我假设您已拥有发布商ID和所有其他内容)

I also spent 2 days to make it work, After reading lot of docs i got it working finally. Below code worked for me. I have written detailed blog post also and working code download as well as working apk. Read Here OR follow below steps (I assume you already have publisher id and all other things)

1)安装admob插件

1) Install admob plugin

ionic plugin add cordova-admob

2)包括 angular-admob.js file

2) Include angular-admob.js file

 <script src="lib/angular-admob/angular-admob.js"></script>

3)将主体onload函数调用到init admob

3) Call body onload function to init admob

 <body ng-app="starter" onload="runads()">

4)将以下代码放在页面底部(不要忘记将发布商ID替换为ca -app-pub-XXXXXXXXXXXXXXXX / IIIIIIIIII')。它只适用于移动设备,而不是在pc浏览器。应用程式开始后等待20-25秒才能载入广告。

4) Put below code at the bottom of page (Dont forget to replace your publisher id with 'ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII'). It works only on mobile devices and not in pc browser. Once app started wait for a 20-25 seconds to load ads.

<script type="text/javascript">
  function runads(){
    document.addEventListener("deviceready", onDeviceReady, false);
  }

  function initAds() {
    if (admob) {
      var adPublisherIds = {
        ios : {
          banner : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
        },
        android : {
          banner : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",
          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,
        tappxIdiOs:       "/XXXXXXXXX/Pub-XXXX-iOS-IIII",
        tappxIdAndroid:   "/XXXXXXXXX/Pub-XXXX-Android-AAAA",
        tappxShare:       0.5
      });

      registerAdEvents();

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

  function onAdLoaded(e) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      admob.showInterstitialAd();
      showNextInterstitial = setTimeout(function() {
        admob.requestInterstitialAd();
      }, 2 * 60 * 1000); // 2 minutes
    }
  }

  // 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(admob.events.onInAppPurchaseRequested, function (e) {});
  }

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

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

    // request an interstitial
    admob.requestInterstitialAd();
  }
</script>

这篇关于AdMob不会在离子/角度应用中加载广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆