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.
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?
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.
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:
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?
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 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 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.
@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.
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.
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 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.)