I'm having some trouble with a plugin I installed

I recently downloaded a plugin from Github using the command:

cordova plugin add .....

The plugin allows me to incorporate ad’s onto my app from Admob. Here is the link: https://github.com/gooogleadmob/cordova-admob-plugin

I followed the instructions and here is what I have so far:

In one of my controllers I have the $ionicPlatform function handy to see if the device is ready:

$ionicPlatform.ready(function() {

	
admobAd.initBanner("MY ADMOB ID WOULD GO HERE", admobAd.AD_SIZE.BANNER.width, admobAd.AD_SIZE.BANNER.height);//replace the demo ID with your ID before run
    admobAd.showBanner(admobAd.AD_POSITION.BOTTOM_CENTER);
		
});

Whenever I run my app I get:

Uncaught ReferenceError: admobAd is not defined 

Why is that so?

The plugin comes with a JS file, but I have not included it in my index.html file. I noticed how others would simply install a plugin and place their code in their ‘deviceready’ functions and it would work, but it seems that that isn’t working for me.

Can anyone go step by step on how I can fix this? As of now I see absolutely no ads.

You should check out this post by @nraboy about dealing with admob.

Thanks for the referral @mhartington :slight_smile:

@Gamma, if you still have trouble after reading my blog post let me know. I’m happy to help you out.

Cheers,

2 Likes

Thank you for taking your time in making that post. I have followed all the instructions and incorporated the plugins successfully, but there is one problem. I do not see any ad’s at all. When I add an “else” statement after the code you provided when checking if the plugin exists. I decided to make a quick check and I placed:

console.log(admob);

and I got:

undefined

I incorporated your code in my own controller that is stored in my controller object like so:

controllers.types = function($scope, $ionicPlatform, $ionicPopup)
{
	$ionicPlatform.ready(function() {
       if(window.plugins && window.plugins.AdMob) {
          var admob_key = device.platform == "Android" ? "I_PLACED_APP_ID_HERE" :     "IOS_PUBLISHER_KEY";
                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'); }
                );
            }else{
            	console.log(admob);
            }
        });
	
.....Other code associated with the app go after the code above

How can I fix this. And what am I doing wrong? nraboy, can you post an example app on github by any chance, for I and others struggling in order to compare syntax?

Hi @Gamma,

What happens when you use my code exactly? I’m not an IonicFramework expert, so I’m not sure if you’re allowed to have the $ionicPlatform.ready() method outside your modules run(). In other words I am not sure if you can have the $ionicPlatform in your controller.

I will work on creating a fully functional demo for you in the mean time.

Cheers,

First, before I start off, I’d like to say thank you for putting your time in helping me with this plugin. I truly appreciate it.

When I use the code, without the check to see if the plugin loaded, the app works fine, but displays no ad’s whats so over, in fact, I believe the plugin never loaded in the first place. I am sure a demo will help me see my mistakes. I changed my code to this:

var app = angular.module('Alter', ['ionic'])
    .run(function($ionicPlatform, $ionicPopup) {
        $ionicPlatform.ready(function() {
            if(window.plugins && window.plugins.AdMob) {
                var admob_key = device.platform == "Android" ? "ID GOES HERE" : "IOS_PUBLISHER_KEY";
                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'); }
                );
            }
        });
    });

And I do not see anything on the screen except for my app. Was I suppose to bind the ad to a DOM element manually or does the ad appear on it’s own when your code is implemented?

Hi @Gamma,

I’ve added a download to the bottom of my blog post. In it contains a README.md which I recommend you take a look at.

This project can be reproduced like the following:

ionic start AdmobProject blank
cd AdmobProject
ionic platform add android
cordova plugin add https://github.com/MobileChromeApps/google-play-services.git
cordova plugin add https://github.com/floatinghotpot/cordova-plugin-admob.git

Then just copy and paste the code from my blog into the app.js file that is already there for you. It will look like this:

angular.module('starter', ['ionic'])
.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();
    }
    if(window.plugins && window.plugins.AdMob) {
        var admob_key = device.platform == "Android" ? "ANDROID_PUBLISHER_KEY" : "IOS_PUBLISHER_KEY";
        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'); }
    );
}
  });
})

Then you can do:

ionic build android

And you’ll have ads in your app just like that. However, don’t forget to add your publisher keys. They usually look something like ca-app-pub-XXXXXXX

While the ad loads it will just be a black box at the bottom, but after a second or two it should show your banner.

Please let me know if this works for you.

Cheers,

@Gamma, did you get this to work? If yes, how?

I’ve been getting the same error too when trying out the admob plugin. I assumed it was something to do with the admob plugin but it looks to be an issue with plugins in general. Looks like plugins are not getting picked up at all. I tried it with ngCordova dialogs also.

I’ve raised an issue here: [solved] ngCordova - uncaught object error

I just discovered that whole plugin hadn’t even loaded, in fact that console is spitting out this error after I ran:

console.log(window.plugins.AdMob);

I got the error:

Uncaught TypeError: Cannot read property 'AdMob' of undefined 

But If @nraboy was able to show proof that it works for him, than it must work for everyone. I just don’t understand why it’s not loading. I followed the steps almost 3 times in a row. Thank you @nraboy for your patience and your help. I am still at this moment trying my best to get this thing running.

@sathishvj and @Gamma,

What version of Ionic and Cordova are you using? I am using the latest of both. Is your Android SDK up to date?

I may create a YouTube video as well, but I’m starting to think something is wrong with your installation of Ionic or Cordova.

Could you create me a demo project and I’ll try to run it with my installation? May help rule out if installations are the issue.

Cheers,

Also please confirm that you are not trying to run this from your browser. The plugin only gets picked up on the device

1 Like

Yeah… I’ve been testing on my browser. Let me give it another shot. I am so sorry. I thought it would show up on a browser.

I’m using Cordova version: 3.5.0-0.2.4

IT"S WORKING! <–Not screaming.

Thank you so much. I tested it on a device and it showed up. I appreciate it.

It won’t get picked up on the emulator also?

@sathishvj I tested it on the browser and it didn’t work. It turned out that you are never suppose to use a browser for testing the ad. The code was meant for the emulator. I just did, (While also following the other steps of the tutorial beforehand) :

cordova build android

Then:

ionic run android

and then my android emulator popped up with an ad at the bottom.

I just created a fresh test app starting with ionic start admob tabs and then followed the steps. I got this error:

/Users/vj/coding/ionic/trials/admob/platforms/android/src/com/rjfun/cordova/plugin/AdMob.java:126: error: cannot find symbol [javac] this.bannerOverlap = inputs.getBoolean( OVERLAP_ARG_INDEX ); [javac] ^ [javac] symbol: variable bannerOverlap [javac] /Users/vj/coding/ionic/trials/admob/platforms/android/src/com/rjfun/cordova/plugin/AdMob.java:153: error: cannot find symbol [javac] ViewGroup parentView = (ViewGroup) (bannerOverlap ? webView : webView.getParent());

Just reporting that quickly. Now will try again with a blank app and see if it works.

Glad it worked for you @Gamma!

@sathishvj it should work fine in the emulator. Cordova being for mobile would mean that the plugins are only going to work on mobile. The browser won’t recognize the plugin which is why we check to see if it exists first.

Let me know if I can help you guys further and be sure to follow my blog.

:frowning: No luck yet on this.

Created a new project. Updated android tools and sdk. Tried all over again. Getting that same error.

-compile: [javac] Compiling 3 source files to /Users/vj/coding/ionic/trials/myApp/platforms/android/ant-build/classes [javac] /Users/vj/coding/ionic/trials/myApp/platforms/android/src/com/rjfun/cordova/plugin/AdMob.java:126: error: cannot find symbol [javac] this.bannerOverlap = inputs.getBoolean( OVERLAP_ARG_INDEX ); [javac] ^ [javac] symbol: variable bannerOverlap [javac] /Users/vj/coding/ionic/trials/myApp/platforms/android/src/com/rjfun/cordova/plugin/AdMob.java:153: error: cannot find symbol [javac] ViewGroup parentView = (ViewGroup) (bannerOverlap ? webView : webView.getParent()); [javac] ^ [javac] symbol: variable bannerOverlap [javac] Note: /Users/vj/coding/ionic/trials/myApp/platforms/android/src/com/rjfun/cordova/plugin/AdMob.java uses unchecked or unsafe operations. [javac] Note: Recompile with -Xlint:unchecked for details. [javac] 2 errors
BUILD FAILED /Users/vj/coding/android/sdk/tools/ant/build.xml:720: The following error occurred while executing this line: /Users/vj/coding/android/sdk/tools/ant/build.xml:734: Compile failed; see the compiler error output for details.

Total time: 1 second

/Users/vj/coding/ionic/trials/myApp/platforms/android/cordova/node_modules/q/q.js:126 throw e; ^
Error code 1 for command: ant with args: debug,-f,/Users/vj/coding/ionic/trials/myApp/platforms/android/build.xml,-Dout.dir=ant-build,-Dgen.absolute.dir=ant-gen Error: /Users/vj/coding/ionic/trials/myApp/platforms/android/cordova/run: Command failed with exit code 8 at ChildProcess.whenDone (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:131:23) at ChildProcess.EventEmitter.emit (events.js:98:17) at maybeClose (child_process.js:743:16) at Process.ChildProcess._handle.onexit (child_process.js:810:5) ERROR: Unable to emulate app on platform android. Please see console for more info. Exiting.

fyi, I’ve raised an issue on https://github.com/floatinghotpot/cordova-plugin-admob/issues/77 for this.

I checked the file AdMob.java and I can’t find a member variable for bannerOverlap. Could it be in CordovaPlugin class and is inherited? I checked that also and it isn’t there either.

Incidentally, just adding that member variable to that class didn’t work either - but that was a shot in the dark :slight_smile: since I don’t know how to create a PhoneGap plugin.

Just fyi, that bug has been fixed now. The build isn’t failing anymore and it shows up in the emulator.

Btw, on iOS the ads are shown in the emulator itself, while on Android it is a blank screen. Is that normal behavior? (I haven’t tested on an actual device.)