每 2 分钟在 Ionic 中通过 AdMob 显示插页式广告
Show Interstitial Ad via AdMob in Ionic every 2 minutes
我在 Ionic 中使用 AdMob 插件,使用这段代码我展示了插页式广告:
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)
,但我不知道在哪里添加?
如果您想每 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);
希望这有助于澄清一些事情。
通过使用 https://github.com/appfeel/admob-google-cordova 的插件,您可以监听 onAdLoaded 和 onAdClosed 事件并使 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);
如果您使用的是 Ionic ngCordova,我是 cordova admob 插件的作者。这是我对你的目的的建议。
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
});
顺便说一句,根据时间间隔显示插页式广告不是一个好主意,因为它可能会带来糟糕的用户体验并违反 Google 规则。
最好在后台准备Interstitial(),然后在某些页面或状态发生变化时显示Interstitial(),例如,当玩家结束和用户单击“确定”按钮时。
因为现在这在 admob 中是非法的,您的 ID 可能会因此被禁用,以及显示加载时添加、后退按钮、简单应用程序的许多插页等底线是,如果你想制作任何钱,你必须显示插页式广告,因为 admob 支付点击而不是浏览量,而且没有人点击横幅。
因此,最佳做法是在 X 次点击后展示广告(设置 "click counter")并在 admob 中自行限制您的 ID。否则您的帐户将像我一样被禁止下注
我在 Ionic 中使用 AdMob 插件,使用这段代码我展示了插页式广告:
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)
,但我不知道在哪里添加?
如果您想每 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);
希望这有助于澄清一些事情。
通过使用 https://github.com/appfeel/admob-google-cordova 的插件,您可以监听 onAdLoaded 和 onAdClosed 事件并使 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);
如果您使用的是 Ionic ngCordova,我是 cordova admob 插件的作者。这是我对你的目的的建议。
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
});
顺便说一句,根据时间间隔显示插页式广告不是一个好主意,因为它可能会带来糟糕的用户体验并违反 Google 规则。
最好在后台准备Interstitial(),然后在某些页面或状态发生变化时显示Interstitial(),例如,当玩家结束和用户单击“确定”按钮时。
因为现在这在 admob 中是非法的,您的 ID 可能会因此被禁用,以及显示加载时添加、后退按钮、简单应用程序的许多插页等底线是,如果你想制作任何钱,你必须显示插页式广告,因为 admob 支付点击而不是浏览量,而且没有人点击横幅。
因此,最佳做法是在 X 次点击后展示广告(设置 "click counter")并在 admob 中自行限制您的 ID。否则您的帐户将像我一样被禁止下注