Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Play Admob integration with Phonegap

How do you add Google Admob to a phonegap app (android) in 2014?

I have checked many examples online e.g. link1 link2 Link3 Link4. Most link to Googles soon to be deprecated Android 6.4.1 tutorial.

Apart from specific android or iOS tutorials, I cannot find any that demonstrate how to implement the latest admob into phonegap for a cross platform app. I have seen inmobi however it looks like you have to publish your app before you can get a publisher id for your adverts.

Update with Android simplified instructions I am getting the black box for the advert however nothing else, no alert messages, no advert

phonegap create myproject
cd mypproject
phonegap run android

Close down the emulator

1.Install the Google Play Services plugin:  cordova plugin add     https://github.com/MobileChromeApps/google-play-services.git  
2.Install this plugin:  cordova plugin add https://github.com/floatinghotpot/cordova-plugin-admob.git  

Then I headed over to index.js and onDeviceReady I added

   onDeviceReady: function() {
     app.receivedEvent('deviceready');

if( window.plugins && window.plugins.AdMob ) {
    var admob_android_key = 'pub-6xxxxxxxxxxxxx';
    var am = window.plugins.AdMob;

    am.createBannerView( 
    {
    'publisherId': admob_android_key,
    'adSize': am.AD_SIZE.BANNER,
    'bannerAtTop': true
    }, 
    function() {
        am.requestAd(
            { 'isTesting':true }, 
            function(){
                am.showAd( true );
            }, 
            function(){ alert('failed to request ad'); }
        );
    }, 
    function(){ alert('failed to create banner view'); }
    );
    } else {
    alert('AdMob plugin not available/ready.');
    }

  }

Added to index.html

       <div>
            <button id='show-ad' onClick="if(window.plugins.AdMob){ window.AdMob.plugins.showAd(true); }">Show Ad</button>
            <button id='hide-ad' onClick="if(window.plugins.AdMob){ window.AdMob.plugins.showAd(false); }">Hide Ad</button>
        </div>

phonegap run android

like image 432
Tom Avatar asked Mar 13 '14 15:03

Tom


1 Answers

This admob plugin works with google play sdk: https://github.com/floatinghotpot/cordova-plugin-admob . Check readmes in main folder AND in subfolders for details.

like image 150
DonAngelo Avatar answered Sep 19 '22 12:09

DonAngelo