Sometimes removing the plugin causes an error (it's been reported to cordova https://issues.apache.org/jira/browse/CB-12083). If that happens, remove first cordova-libgoogleadmobads manually:
To use in Phonegap Build, place the following tag in your config.xml file:
<gap:pluginname="phonegap-admob"source="npm"/>
To start showing ads, place the following code in your onDeviceReady callback. Replace corresponding id's with yours:
Note: ensure you have a proper AdMob and tappx accounts and get your publisher id's.
functiononDeviceReady(){document.removeEventListener('deviceready',onDeviceReady,false);// Set AdMobAds options:admob.setOptions({publisherId: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",// RequiredinterstitialAdId: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",// OptionalautoShowBanner: true,// OptionalautoShowRInterstitial: false,// OptionalautoShowRewarded: false,// OptionaltappxIdiOS: "/XXXXXXXXX/Pub-XXXX-iOS-IIII",// OptionaltappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA",// OptionaltappxShare: 0.5// Optional});// Start showing banners (atomatic when autoShowBanner is set to true)admob.createBannerView();// Request interstitial ad (will present automatically when autoShowInterstitial is set to true)admob.requestInterstitialAd();// Request rewarded ad (will present automatically when autoShowRewarded is set to true)admob.requestRewardedAd();}document.addEventListener("deviceready",onDeviceReady,false);
If you don't specify tappxId, no tappx requests will be placed (even if you specify a tappxShare). See Tappx configuration for more detailed info.
⚠️ Be sure to start ads on "deviceready" event otherwise, the plugin would not work.
Full documentation
Visit the wiki of Google AdMob Cordova plugin. Table of contents:
Note that the admob ads are configured inside onDeviceReady(). This is because only after device ready the AdMob Cordova plugin will be working.
varisAppForeground=true;functioninitAds(){if(admob){varadPublisherIds={ios : {banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",interstitial : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"},android : {banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",interstitial : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"}};varadmobid=(/(android)/i.test(navigator.userAgent)) ? adPublisherIds.android : adPublisherIds.ios;admob.setOptions({publisherId: admobid.banner,interstitialAdId: admobid.interstitial,autoShowBanner: true,autoShowInterstitial: false,autoShowRewarded: false,tappxIdiOS: "/XXXXXXXXX/Pub-XXXX-iOS-IIII",tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA",tappxShare: 0.5,});registerAdEvents();}else{alert('AdMobAds plugin not ready');}}functiononAdLoaded(e){if(isAppForeground){if(e.adType===admob.AD_TYPE.AD_TYPE_BANNER){console.log("New banner received");}elseif(e.adType===admob.INTERSTITIAL){console.log("An interstitial has been loaded and autoshown. If you want to automatically show the interstitial ad, set 'autoShowInterstitial: true' in admob.setOptions() or remove it");admob.showInterstitialAd();}elseif(e.adType===admob.AD_TYPE_REWARDED){console.log("New rewarded ad received");admob.showRewardedAd();}}}functiononPause(){if(isAppForeground){admob.destroyBannerView();isAppForeground=false;}}functiononResume(){if(!isAppForeground){setTimeout(admob.createBannerView,1);setTimeout(admob.requestInterstitialAd,1);isAppForeground=true;}}// optional, in case respond to eventsfunctionregisterAdEvents(){document.addEventListener(admob.events.onAdLoaded,onAdLoaded);document.addEventListener(admob.events.onAdFailedToLoad,function(e){});document.addEventListener(admob.events.onAdOpened,function(e){});document.addEventListener(admob.events.onAdClosed,function(e){});document.addEventListener(admob.events.onAdLeftApplication,function(e){});document.addEventListener("pause",onPause,false);document.addEventListener("resume",onResume,false);}functiononDeviceReady(){document.removeEventListener('deviceready',onDeviceReady,false);initAds();// display a banner at startupadmob.createBannerView();// request an interstitial adadmob.requestInterstitialAd();// request a rewarded adadmob.requestRewardedAd();}document.addEventListener("deviceready",onDeviceReady,false);
Contributing
You can use this cordova plugin for free. You can contribute to this project in many ways:
Testimonials of apps that are using this plugin gives your app free marketing and will be especially helpful. Open an issue.
Patching and bug fixing, especially when submitted with test code. Open a pull request.
Other enhancements.
You can also support this project by sharing 2% Ad traffic (it's not mandatory: if you are unwilling to share, please fork and remove the donation code).
Love the project? Wanna buy me a coffee (or a beer :D)? Click here
Screenshots
iPhone:
iPad Banner Portrait:
iPad Banner Landscape:
License
The MIT License
Copyright (c) 2014 AppFeel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
请发表评论