gpt4 book ai didi

android - AdMob 未在 ionic /Angular 应用中加载广告

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:14:15 25 4
gpt4 key购买 nike

我在转换广告时遇到了一些问题。我无法思考为什么它不起作用。我安装了以下插件:

com.google.playservices 19.0.0 “Android 版 Google Play 服务”com.rjfun.cordova.plugin.admob 2.1.7 “AdMob”我用过这个教程:https://www.thepolyglotdeveloper.com/2014/06/using-admob-ionicframework/

这是我在 apps.js 中的代码:

 .run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}

// select the right Ad Id according to platform
if (window.plugins && window.plugins.AdMob) {
var admob_key = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
var admob = window.plugins.AdMob;
admob.createBannerView(
{
'publisherId': admob_key,
'adSize': admob.AD_SIZE.BANNER,
'bannerAtTop': false
},
function () {
admob.requestAd(
{'isTesting': false},
function () {
admob.showAd(true);
},
function () {
console.log('failed to request ad');
}
);
},
function () {
console.log('failed to create banner view');
}
);
}
});
});

这会导致应用底部出现黑色横幅区域,但不会加载任何广告。这段代码目前是实时的,在 admob 网站上我可以看到几百个 session 。但是,几天来我的印象和请求 rpm 都为零。有人知道哪里出了问题吗?

最佳答案

我也花了 2 天时间让它工作,在阅读了很多文档之后我终于让它工作了。下面的代码对我有用。我也写了详细的博客文章和工作代码下载以及工作 apk。 Read Here或按照以下步骤操作(我假设您已经拥有发布者 ID 和所有其他内容)

1) 安装admob插件

ionic plugin add cordova-admob

2) 包含 angular-admob.js 文件

 <script src="lib/angular-admob/angular-admob.js"></script>

3) 调用body onload函数初始化admob

 <body ng-app="starter" onload="runads()">

4) 将下面的代码放在页面底部(不要忘记将您的发布者 ID 替换为“ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII”)。它仅适用于移动设备,不适用于 PC 浏览器。应用启动后等待 20-25 秒加载广告。

<script type="text/javascript">
function runads(){
document.addEventListener("deviceready", onDeviceReady, false);
}

function initAds() {
if (admob) {
var adPublisherIds = {
ios : {
banner : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
},
android : {
banner : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",
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,
tappxIdiOs: "/XXXXXXXXX/Pub-XXXX-iOS-IIII",
tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA",
tappxShare: 0.5
});

registerAdEvents();

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

function onAdLoaded(e) {
if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
admob.showInterstitialAd();
showNextInterstitial = setTimeout(function() {
admob.requestInterstitialAd();
}, 2 * 60 * 1000); // 2 minutes
}
}

// optional, in case respond to events
function registerAdEvents() {
document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
document.addEventListener(admob.events.onAdFailedToLoad, function (e) {});
document.addEventListener(admob.events.onAdOpened, function (e) {});
document.addEventListener(admob.events.onAdClosed, function (e) {});
document.addEventListener(admob.events.onAdLeftApplication, function (e) {});
document.addEventListener(admob.events.onInAppPurchaseRequested, function (e) {});
}

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

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

// request an interstitial
admob.requestInterstitialAd();
}
</script>

关于android - AdMob 未在 ionic /Angular 应用中加载广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28292927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com