AdMob not getting display on device ionic

2019-09-03 14:59发布

问题:

i am trying to display admob inside my application but it's not getting displayed on screen.

I added its plugins :- cordova plugin add com.rjfun.cordova.plugin.admob cordova plugin add https://github.com/floatinghotpot/cordova-plugin-admob.git

here is my code which i have done in my app.js class under run function :-

$ionicPlatform
  .ready(function() {

      if (window.plugins && window.plugins.AdMob) {
        alert('inside');
        var admob_key = device.platform == "Android" ? "ca-app-pub-6869992474017983/4748283957" : "IOS_PUBLISHER_KEY";
        var admob = window.plugins.AdMob;
        admob
          .createBannerView({
              'adId': admob_key,
              'position': admob.AD_POSITION.BOTTOM_CENTER,
              'adSize': admob.AD_SIZE.BANNER,
              'bannerAtTop': false
            },
            function() {
              admob
                .requestAd({
                    'isTesting': false
                  },
                  function() {
                    admob
                      .showAd(true);
                  },
                  function() {
                    alert("failed to request ad");
                    console
                      .log('failed to request ad');
                  });
            },
            function() {
              alert("failed to create banner view");
              console
                .log('failed to create banner view');
            });
      } else {
        alert("AdMob plugin not available/ready.");
      }

links from which i refer above code :-

1) https://blog.nraboy.com/2014/06/using-admob-ionicframework/

2) https://github.com/floatinghotpot/cordova-admob-pro

Please provide me any suggestion on it.

Thanks

回答1:

Some things have changed between the latest version of Admob and that in the blog by nraboy. I noticed the following differences a few weeks back when I used the plugin after I inspected the plugins .js file to figure out the changes:

  • Admob was no longer located under window.plugins.Admob I found it under window.Admob
  • Config option publisherId is now adId
  • Config option bannerAtTopis now position
  • admob.createBannerView() is now just admob.createBanner() which does all the work for you when it comes to showing the add (you don't need to call admob.requestAd()) etc.

Template to follow

           if (window.AdMob) {
                var admob = window.AdMob;
                admob.createBanner({
                    adId: admob_key,
                    adSize: admob.AD_SIZE.SMART_BANNER,
                    position: admob.AD_POSITION.BOTTOM_CENTER,
                    isTesting: false, //Live
                    //isTesting: true, //Test
                    autoShow: true
                }, function (data) {
                    console.log('Banner created... ' + angular.toJson(data));
                }, function (err) {
                    console.log('Failed to create banner view... ' + angular.toJson(err));
                });
            } else {
                //plugin not found
            }

Additional Info

In case you aren't aware of other AD_SIZE and AD_POSITION options (copied from the plugins .js file)

.AD_POSITION = {
  NO_CHANGE: 0,
  TOP_LEFT: 1,
  TOP_CENTER: 2,
  TOP_RIGHT: 3,
  LEFT: 4,
  CENTER: 5,
  RIGHT: 6,
  BOTTOM_LEFT: 7,
  BOTTOM_CENTER: 8,
  BOTTOM_RIGHT: 9,
  POS_XY: 10
};

.AD_SIZE = {
  SMART_BANNER: 'SMART_BANNER',
  BANNER: 'BANNER',
  MEDIUM_RECTANGLE: 'MEDIUM_RECTANGLE',
  FULL_BANNER: 'FULL_BANNER',
  LEADERBOARD: 'LEADERBOARD',
  SKYSCRAPER: 'SKYSCRAPER'
};