代码之家  ›  专栏  ›  技术社区  ›  Partho63 BASANT KUMAR

在PhoneGap项目中添加Google AdMob

  •  1
  • Partho63 BASANT KUMAR  · 技术社区  · 6 年前

    几天来,我一直试图在我的PhoneGap项目中添加AdMob。但我找不到在我的应用程序中显示附加内容的方法。我学过一些教程,但似乎没有什么帮助。我在使用谷歌提供的横幅测试广告单元 ca-app-pub-3940256099942544/6300978111 . 我在测试应用程序,从 Adobe PhoneGap Build 然后在我的Android手机上安装apk来运行应用程序。我就是这么做的:

    要在我的项目中添加AdMob插件:

    phonegap plugin add cordova-admob
    

    然后在index.js文件中

    代码#1

    var app = {
        initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function() {
            app.receivedEvent('deviceready');
            admob.setOptions({
                publisherId: "ca-app-pub-3940256099942544/6300978111",
                isTesting: false,
                autoShowBanner: true,
            });
            admob.createBannerView();
        },
        receivedEvent: function(id) {
            var parentElement = document.getElementById(id);
            if (parentElement != null) {
                var listeningElement = parentElement.querySelector('.listening');
                var receivedElement = parentElement.querySelector('.received');
                listeningElement.setAttribute('style', 'display:none;');
                receivedElement.setAttribute('style', 'display:block;');
            }
            console.log('Received Event: ' + id);
        }
    };
    

    代码#2

    var app = {
        initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function() {
            app.receivedEvent('deviceready');
            alert("device ready");
            app.adSetter();
        },
        adSetter: function(){
            alert(navigator.userAgent);
            var admobid = {};
            if( /(android)/i.test(navigator.userAgent) ) { 
                admobid = {
                    banner: 'ca-app-pub-3940256099942544/6300978111'
                };
            }
            if(AdMob) AdMob.createBanner( {
                adId:admobid.banner, 
                position:AdMob.AD_POSITION.BOTTOM_CENTER, 
                autoShow:true
            } );
        },
        receivedEvent: function(id) {
            var parentElement = document.getElementById(id);
            if (parentElement != null) {
                var listeningElement = parentElement.querySelector('.listening');
                var receivedElement = parentElement.querySelector('.received');
                listeningElement.setAttribute('style', 'display:none;');
                receivedElement.setAttribute('style', 'display:block;');
            }
            console.log('Received Event: ' + id);
        }
    };
    

    以上都不起作用。我做错什么了?

    我需要做什么才能在我的PhoneGap项目中添加AdMob,并在我的应用程序中显示adds?

    提前谢谢。:)

    0 回复  |  直到 6 年前