sunnycupertino / cordova-plugin-admob-simple

Cordova plugin allowing Admob interstitials and banner ads.
MIT License
164 stars 150 forks source link

App doesn't show ads #25

Closed ManoloPololo closed 6 years ago

ManoloPololo commented 6 years ago

SOLVED!!!!

I hadn't ad: showInterstitialFunc() in onDeviceReady()

Sorry :)

Hi, I'm going to describe the steps I follow to show ads so you can, please, help me to fix this issue.

cordova create hallo com.example.hallo HalloWorld cd hallo cordova platform add android cordova plugin add cordova-plugin-admob-simple

Then, I open the project, and this is my index.js:

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
        initAd();
        showBannerFunc();
        console.log('Received Event: ' + id);
    }
};

//initialize the goodies
function initAd(){
        if ( window.plugins && window.plugins.AdMob ) {
            var ad_units = {
                ios : {
                    banner: 'ca-app-pub-3974331804911150/2893856500',       //PUT ADMOB ADCODE HERE
                    interstitial: 'ca-app-pub-3974331804911150/5847322900'  //PUT ADMOB ADCODE HERE
                },
                android : {
                    banner: 'ca-app-pub-3974331804911150/2893856500',       //PUT ADMOB ADCODE HERE
                    interstitial: 'ca-app-pub-3974331804911150/5847322900'  //PUT ADMOB ADCODE HERE
                }
            };
            var admobid = ( /(android)/i.test(navigator.userAgent) ) ? ad_units.android : ad_units.ios;

            window.plugins.AdMob.setOptions( {
                publisherId: admobid.banner,
                interstitialAdId: admobid.interstitial,
                adSize: window.plugins.AdMob.AD_SIZE.SMART_BANNER,  //use SMART_BANNER, BANNER, LARGE_BANNER, IAB_MRECT, IAB_BANNER, IAB_LEADERBOARD
                bannerAtTop: false, // set to true, to put banner at top
                overlap: true, // banner will overlap webview
                offsetTopBar: false, // set to true to avoid ios7 status bar overlap
                isTesting: true, // receiving test ad
                autoShow: true // auto show interstitial ad when loaded
            });

            registerAdEvents();
        } else {
            //alert( 'admob plugin not ready' );
        }
}
//functions to allow you to know when ads are shown, etc.
function registerAdEvents() {
        document.addEventListener('onReceiveAd', function(){});
        document.addEventListener('onFailedToReceiveAd', function(data){});
        document.addEventListener('onPresentAd', function(){});
        document.addEventListener('onDismissAd', function(){ });
        document.addEventListener('onLeaveToAd', function(){ });
        document.addEventListener('onReceiveInterstitialAd', function(){ });
        document.addEventListener('onPresentInterstitialAd', function(){ });
        document.addEventListener('onDismissInterstitialAd', function(){ });
    }

//display the banner
function showBannerFunc(){
    window.plugins.AdMob.createBannerView();
}
//display the interstitial
function showInterstitialFunc(){
    window.plugins.AdMob.showInterstitialAd();
}

app.initialize();

Finally, I type in cmd: cordova build android

But the INTERSTITIAL isn't showing...

sunnycupertino commented 6 years ago

Glad you solved it.