2015-06-28 111 views
3

我使用AdMob廣告插件的離子以及與此代碼,我表現出間質廣告:顯示通過AdMob廣告在離子插頁式廣告,每2分鐘

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(); 
} 

是否有一種方式來顯示間質性廣告,每2分鐘?有人告訴我添加這個:setInterval(showInterstitial,1*60*1000),但我不知道在哪裏添加?

+0

提供的答案是否可以幫助您解決問題? – Nikola

+0

您正在使用哪個Admob插件? – Paul

回答

3

如果你想顯示它每2分鐘,你應該使用:

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

,你應該只是你的initAdd功能的右括號之前添加:

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); 
 

 
}

你可以看到這個jsFiddle example一個簡單的setInterval用法:

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

 
setInterval(a, 2*1000);

之所以你不應該這樣稱呼它(注意括號a後):setInterval(a(), 2*1000);是,那麼你的功能將只調用一次(你會看到只有一個警報彈出)。在jsFiddle例如:

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

 
setInterval(a(), 2*1000);

希望這有助於明確的東西一點。

+0

您從哪個AdMob插件編寫代碼?我問,因爲有幾個。 – Paul

+0

我用[這一個](https://github.com/floatinghotpot/cordova-plugin-admob)。你可以閱讀完整的教程[這裏](http://www.nikola-breznjak.com/blog/ionic/adding-admob-to-ionic-framework-application-step-by-step/)。 – Nikola

2

通過使用插件在https://github.com/appfeel/admob-google-cordova你可以聽onAdLoaded和onAdClosed事件,使autoShowInterstitial假:

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); 
2

我的科爾多瓦AdMob廣告插件的作者,如果您使用的離子ngCordova。這是我對你的目的的建議。

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,表示基於時間間隔插頁廣告是不是一個好主意,因爲它可能會帶來不良的用戶體驗,谷歌違反規則。

它會在後臺更好地prepareInterstitial(),然後showInterstitial()當一些頁面或狀態改變,例如,當玩家在和用戶點擊OK按鈕。

0

由於現在這在admob中是非法的,因此您的id可能會因爲此問題而被禁用,並且會顯示加載,後退按鈕,以及許多插頁式簡單應用等。底線是,如果您想製作任何金錢,你必須展示插頁式廣告,因爲admob支付點擊而不是觀看,並且沒有人點擊橫幅廣告。

所以最好的做法是X-點擊後顯示廣告(設置「點擊計數器」)和蓋你的IDS在AdMob,他們的自我。或者您的賬戶將像我一樣被禁止