1
votes

I am using floatinghotpot plugin to show ads

cordova plugin add https://github.com/floatinghotpot/cordova-plugin-admob.git 

This is my code for admob

$ionicPlatform.ready(function($location) {
        var adMobId = {
            admob_banner_key: 'ca-app-pub-3378914045953959/7363511027'
        };

        var options = {
            interstitialAdId: 'ca-app-pub-3940256099942544/1033173712',
            autoShow: true
        };

        var adMobPosition = {
            BOTTOM_CENTER: 8
        };

       $scope.showBannerAd = function() {

            try {
                console.log('Show Banner Ad');
                $cordovaAdMob.createBannerView({
                    adId: adMobId.admob_banner_key,
                    position: adMobPosition.BOTTOM_CENTER,
                    isTesting: true,
                    autoShow: true
                });

            } catch (e) {
                alert(e);
            }
        }

        $scope.showInterstitialAd = function(st) {

            try {
                console.log('Show Interstitial Ad');

                $cordovaAdMob.createInterstitialView(options, function() {
                        admob.requestInterstitialAd({
                                'isTesting': true
                            },
                            function() {
                                admob.showAd(true);
                            },
                            function(error) {
                                console.log('failed to request ad ' + error);
                            }
                        );
                    },
                    function() {
                        console.log('failed to create Interstitial view');
                    });

            } catch (e) {
                alert("ALAS");
            }
        }
    });

But when I call showBannerAd() using ng-init="showBannerAd()" it does not show ads and show this error message. How can I solve this problem? Am I missing anythinh important?

TypeError: Cannot read property 'AdMob' of undefined

This is the lists of installed plugins

com.google.admobsdk 6.12.2 "AdMob SDK for Android, iOS and WP8"
cordova-plugin-admob 2.10.0 "AdMob Plugin Pro"
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.0 "Device"
cordova-plugin-extension 1.2.3 "Cordova Plugin Extension"
cordova-plugin-googleplayservices 19.0.3 "Google Play Services for Android"
cordova-plugin-inappbrowser 1.1.1 "InAppBrowser"
cordova-plugin-network-information 1.1.0 "Network Information"
cordova-plugin-splashscreen 3.0.0 "Splashscreen"
cordova-plugin-statusbar 2.0.0 "StatusBar"
cordova-plugin-whitelist 1.2.0 "Whitelist"
ionic-plugin-keyboard 1.0.8 "Keyboard"
2

2 Answers

0
votes

I have created a JS File as below

var admobid = {};

// TODO: replace the following ad units with your own
if( /(android)/i.test(navigator.userAgent) ) { 
  //console.log("android");
  admobid = { // for Android
    banner: 'ca-app-pub-123123/123123',
    interstitial: 'ca-app-pub-123123/123123'
  };
} else if(/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {
  admobid = { // for iOS
    banner: 'ca-app-pub-123123/123123',
    interstitial: 'ca-app-pub-123123/4123123'
  };
} else {
  admobid = { // for Windows Phone
    banner: 'ca-app-pub-123123/123123',
    interstitial: 'ca-app-pub-123123/123123'
  };
}

function initApp() {
  if (! AdMob ) { alert( 'admob plugin not ready' ); return; }

  // this will create a banner on startup
  AdMob.createBanner( {
    adId: admobid.banner,
    position: AdMob.AD_POSITION.BOTTOM_CENTER,
    isTesting: false, // TODO: remove this line when release
    overlap: false,
    offsetTopBar: false,
    bgColor: 'black'
  } );

  // this will load a full screen ad on startup
  AdMob.prepareInterstitial({
    adId: admobid.interstitial,
    isTesting: false, // TODO: remove this line when release
    autoShow: false
  });
}

if(( /(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent) )) {
    document.addEventListener('deviceready', initApp, false);
} else {
    initApp();
}

and included this js file into the index.html which is starter page of the application

Hope this may help you.

-1
votes

I have found with Apache Cordova that I have to put a setTimeout (aka sleep) and only execute my AdMob code after 1, 2 or 3 seconds. Eg:

$ionicPlatform.ready(function($location) {
    setTimeout(function()
    {
        var adMobId = {
            admob_banner_key: 'ca-app-pub-3378914045953959/7363511027'
        };

        var options = {
            interstitialAdId: 'ca-app-pub-3940256099942544/1033173712',
            autoShow: true
        };
        ...
        ...
        ...
        etc...
    }, 3000);
});