在PhoneGap项目中添加Google AdMob

时间:2019-01-07 19:16:35

标签: android admob phonegap phonegap-admob

几天来,我一直在尝试在PhoneGap项目中添加AdMob。但是我找不到在我的应用程序上显示添加项的方法。我遵循了一些教程,但是似乎没有什么帮助。我正在使用Google提供的横幅广告ca-app-pub-3940256099942544/6300978111的测试广告单元。我正在通过从Adobe PhoneGap Build构建apk来测试该应用,然后在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并在应用中显示添加内容,我需要做什么?

先谢谢了。 :)

0 个答案:

没有答案