Bruno Alex
Bruno Alex

Reputation: 73

Admob not working in phonegap and android

im trying to add ads to my android phonegap app but im not getting anywere... i use phonegap 0.3.3

my admob key ad key is like: ca-app-pub-9718633180389415/1973821888

im my config.xml i have:

<plugin name="com.admob.google" spec="4.0.8" source="pgb" />

and then tryed on my index:

function onDeviceReady() {
      document.removeEventListener('deviceready', onDeviceReady, false);

      // Set AdMobAds options:
      admob.setOptions({
        publisherId:          "ca-app-pub-9718633180389415/1973821888",  // Required
        interstitialAdId:     "ca-app-pub-9718633180389415/1973821888",  // Optional
        tappxIdiOs:           "/XXXXXXXXX/Pub-XXXX-iOS-IIII",            // Optional
        tappxIdAndroid:       "/XXXXXXXXX/Pub-XXXX-Android-AAAA",        // Optional
        tappxShare:           0.5                                        // Optional
      });

      // Start showing banners (atomatic when autoShowBanner is set to true)
      admob.createBannerView();

      // Request interstitial (will present automatically when autoShowInterstitial is set to true)
      admob.requestInterstitial();
    }

but noting is displayd: im using https://github.com/sunnycupertino/cordova-plugin-admob-simple

Upvotes: 1

Views: 1453

Answers (2)

Mike Spear
Mike Spear

Reputation: 882

Not sure if this is useful, but I thought I was using the same plugin as you, when, in fact, I was using this one:

https://github.com/admob-google/admob-cordova

To use that plugin, I wrote the following code:

var AdmobManager = {
    // admob configuration
    admobCfg: {},

    // Initialize admob support, and start showing a banner right away
    initAdmob: function () {
        // If we don't have admob support, don't try to configure anything
        if (!admob) 
            return;

        // Configure the IDs for the iOS and Android banner and interstitial ads
        AdmobManager.admobCfg.iosCfg = {
            banner: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
            interstitial: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
        };
        AdmobManager.admobCfg.androidCfg = {
            banner: "ca-app-pub-XXXXXXXXXXXXXXXX/RRRRRRRRRR",
            interstitial: "ca-app-pub-XXXXXXXXXXXXXXXX/HHHHHHHHHH"
        };

        // Choose ad IDs based on platform
        AdmobManager.admobCfg.activeCfg = (/(android)/i.test(navigator.userAgent)) ? AdmobManager.admobCfg.androidCfg : AdmobManager.admobCfg.iosCfg;

        // Now we can init admob: set the IDs, and build a param for testing mode
        admob.initAdmob(AdmobManager.admobCfg.activeCfg.banner, AdmobManager.admobCfg.activeCfg.interstitial);
        AdmobManager.admobCfg.extraParams = new admob.Params();
        AdmobManager.admobCfg.extraParams.isTesting = true;

        // Request banner ads immediately upon app start
        admob.showBanner(admob.BannerSize.BANNER, admob.Position.BOTTOM_CENTER, AdmobManager.admobCfg.extraParams);
    },

    // start allowing Admob interstitials
    enableAdmobInterstitial: function () {
        // If we don't have admob support, don't try to configure anything
        if (!admob) 
            return;

        // Set up a listener for showing an interstitial once it arrives
        document.addEventListener(admob.Event.onInterstitialReceive, function (message) {
            admob.showInterstitial();
        }, false);

        // Request an interstitial to be sent
        admob.cacheInterstitial(AdmobManager.admobCfg.extraParams);
    }
}

I call AdmobManager.initAdmob() before I even show the login screen, and then, after login, I call AdmobManager.enableAdmobInterstitial().

Of course, once you're done testing, you'll want to change isTesting to false.

Upvotes: -1

Miquel
Miquel

Reputation: 8989

The author of cordova-admob plugin here. Have you tried with isTesting: true? Are you running in real device? Please note that it will not work in browsers (only emulators or real devices).

In your config.xml:

<gap:plugin name="phonegap-admob" source="npm"/>

In your javascript:

function onDeviceReady() {
  document.removeEventListener('deviceready', onDeviceReady, false);

  // Set AdMobAds options:
  admob.setOptions({
    publisherId:          "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",  // Required
    interstitialAdId:     "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",  // Optional
    isTesting:            true  // Optional
  });

  // Start showing banners (atomatic when autoShowBanner is set to true)
  admob.createBannerView();

  // Request interstitial (will present automatically when autoShowInterstitial is set to true)
  admob.requestInterstitialAd();
}

document.addEventListener("deviceready", onDeviceReady, false);

See more details here: https://github.com/appfeel/admob-google-cordova/wiki/Setup

Upvotes: 2

Related Questions