Show Interstitial Ad via AdMob in Ionic every 2 mi

2019-03-31 07:16发布

问题:

I'm using AdMob plugin in Ionic and with this code I show an Interstital ad:

 function initAd(){
     // it will display smart banner at top center, using the default options
     if(AdMob) AdMob.createBanner( {
         adId: admobid.banner,
         bannerId: admobid.banner,
         position: AdMob.AD_POSITION.BOTTOM_CENTER,
         autoShow: true,
         isTesting: false,
         success: function() {
             console.log('banner created');
         },
         error: function() {
             console.log('failed to create banner');
         }
     });


    window.AdMob.prepareInterstitial({
        adId:admobid.interstitial, autoShow:false
    });
    window.AdMob.showInterstitial();
}

Is there a way to show intersitial ad every 2 minutes? Someone told me to add this: setInterval(showInterstitial,1*60*1000), but I don't know where to add?

回答1:

If you would like to show it every 2 minutes you should use:

setInterval(window.AdMob.showInterstitial, 2*60*1000);

and you should add it just before the closing bracket of your initAdd function:

function initAd(){


 // it will display smart banner at top center, using the default options
 if(AdMob) AdMob.createBanner( {
                          adId: admobid.banner,
                          bannerId: admobid.banner,
                          position:AdMob.AD_POSITION.BOTTOM_CENTER,
                          autoShow: true,
                          isTesting: false,
                          success: function(){
                          console.log('banner created');
                          },
                          error: function(){
                         console.log('failed to create banner');
                          }
                          } );

                                       window.AdMob.prepareInterstitial( 
                           {adId:admobid.interstitial, autoShow:false} );
    window.AdMob.showInterstitial();
  
  
  
  //!!!add the code here!!! - so, just paste what I wrote above:
  setInterval(window.AdMob.showInterstitial, 2*60*1000);

 }

You can see a simple setInterval usage on this jsFiddle example:

function a(){
    alert("hi every 2 seconds");
};

setInterval(a, 2*1000);

The reason why you shouldn't call it like this (note the brackets after a): setInterval(a(), 2*1000); is that then your function would be called only once (you would see only one alert popping up). Example on jsFiddle:

function a(){
    alert("hi every 2 seconds");
};

setInterval(a(), 2*1000);

Hope this helps clear things a bit.



回答2:

I am the author of the cordova admob plugin, if you are using Ionic ngCordova. Here is my suggestion to your purpose.

var interstitialReady = false;

// update the state when ad preloaded
document.addEventListener('onAdLoaded', function(e){
    if(e.adType == 'interstitial') {
        interstitialReady = true;
    }
});

// when dismissed, preload one for next show
document.addEventListener('onAdDismiss', function(e){
    if(e.adType == 'interstitial') {
        interstitialReady = false;
        AdMob.prepareInterstitial({
           adId:admobid.interstitial, 
           autoShow:false
        });
    }
});

setInterval(function(){
    if(interstitialReady) AdMob.showInterstitial();
}, 2*60*1000);

// preload the first ad
AdMob.prepareInterstitial({
    adId:admobid.interstitial, 
    autoShow:false
});

BTW, showing interstitial ad based on time interval is not a good idea, as it may bring poor user experience and violate Google rules.

It will be better to prepareInterstitial() in background, then showInterstitial() when some page or state change, for example, when gamer over and user click the OK button.



回答3:

By using the plugin at https://github.com/appfeel/admob-google-cordova you could listen to onAdLoaded and onAdClosed event and make autoShowInterstitial false:

var isAppForeground = true;

function initAds() {
  if (admob) {
    var adPublisherIds = {
      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"
      }
    };

    var admobid = (/(android)/i.test(navigator.userAgent)) ? adPublisherIds.android : adPublisherIds.ios;

    admob.setOptions({
      publisherId:          admobid.banner,
      interstitialAdId:     admobid.interstitial,
      autoShowInterstitial: false
    });

    registerAdEvents();

  } else {
    alert('AdMobAds plugin not ready');
  }
}

function onAdLoaded(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      admob.showInterstitialAd();
    }
  }
}

function onAdClosed(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      setTimeout(admob.requestInterstitialAd, 1000 * 60 * 2);
    }
  }
}

function onPause() {
  if (isAppForeground) {
    admob.destroyBannerView();
    isAppForeground = false;
  }
}

function onResume() {
  if (!isAppForeground) {
    setTimeout(admob.createBannerView, 1);
    setTimeout(admob.requestInterstitialAd, 1);
    isAppForeground = true;
  }
}

// optional, in case respond to events
function registerAdEvents() {
  document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
  document.addEventListener(admob.events.onAdClosed, onAdClosed);

  document.addEventListener("pause", onPause, false);
  document.addEventListener("resume", onResume, false);
}

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

  // display a banner at startup
  admob.createBannerView();

  // request an interstitial
  admob.requestInterstitialAd();
}

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


回答4:

As by now this is illegal in admob, your id will get probably disabled for this, as well as showing add on load, back buttons, to many interstitial for simple apps,etc Bottom line is, if you wanna make any money at all, you have to show interstitial ads,as admob pays for click not for views, and no one ever clicks on banner.

So best practice would be show ad after X-clicks(set up "click counter") and cap your ids in admob it self. Or your account will bet banned like my did