AdMob 未在 ionic/angular 应用中加载广告 [英] AdMob not loading ads in ionic/angular app

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

问题描述

我在使用广告时遇到了一些问题.我无法思考为什么它不起作用.我安装了以下插件:

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.0Android 版 Google Play 服务"com.rjfun.cordova.plugin.admob 2.1.7 AdMob"我已经使用过本教程:https://www.thepolyglotdeveloper.com/2014/06/using-admob-ionicframework/

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://www.thepolyglotdeveloper.com/2014/06/using-admob-ionicframework/

这是我在 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?

推荐答案

我也花了 2 天时间让它工作,在阅读了很多文档后我终于让它工作了.下面的代码对我有用.我也写了详细的博客文章和工作代码下载以及工作 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 文件

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

3) 调用 body onload 函数来初始化 admob

3) Call body onload function to init admob

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

4) 将下面的代码放在页面底部(不要忘记将您的发布商 ID 替换为ca-app-pub-XXXXXXXXXXXXXXXXXX/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 未在 ionic/angular 应用中加载广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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