科尔多瓦中的admob插件错误

Posted

技术标签:

【中文标题】科尔多瓦中的admob插件错误【英文标题】:admob plugin error in cordova 【发布时间】:2015-08-15 18:11:34 【问题描述】:

我从 github 下载示例 cordova admob 项目。我在android studio中提取和测试并在galaxy s4上调试。当应用程序打开时,它会提示“admob 插件未加载”。我已经在我的项目中安装了 admob 插件和 internet 插件

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
    <title>Hello World</title>
    <script type="text/javascript" src="cordova.js"></script>
    <style type="text/css">
        html, body  width:100%; height:100%; margin:0; padding:0; overflow:hidden; background-color:white; 
        div#fullpage  width:100%; height:100%; margin:0; padding:0; border:0px solid red; text-align:center; vertical-align:middle; 
        button  font-size: 22px; 
    </style>
</head>
<body onload="onDocLoad()" onresize="onResize()">
<script>
    function onDocLoad() 
        if(( /(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent) )) 
            document.addEventListener('deviceready', initApp, false);
         else 
            initApp();
        
    

    function initApp() 
        initAd();

        // display the banner at startup
        window.plugins.AdMob.createBannerView();
    
    function initAd()
        if ( window.plugins && window.plugins.AdMob ) 
            var ad_units = 
                ios : 
                    banner: 'ca-app-pub-6869992474017983/4806197152',
                    interstitial: 'ca-app-pub-6869992474017983/7563979554'
                ,
                android : 
                    banner: 'ca-app-pub-6869992474017983/9375997553',
                    interstitial: 'ca-app-pub-6869992474017983/1657046752'
                ,
                wp8 : 
                    banner: 'ca-app-pub-6869992474017983/8878394753',
                    interstitial: 'ca-app-pub-6869992474017983/1355127956'
                
            ;
            var admobid = "";
            if( /(android)/i.test(navigator.userAgent) ) 
                admobid = ad_units.android;
             else if(/(iphone|ipad)/i.test(navigator.userAgent)) 
                admobid = ad_units.ios;
             else 
                admobid = ad_units.wp8;
            

            window.plugins.AdMob.setOptions( 
                publisherId: admobid.banner,
                interstitialAdId: admobid.interstitial,
                bannerAtTop: false, // set to true, to put banner at top
                overlap: false, // set to true, to allow banner overlap webview
                offsetTopBar: false, // set to true to avoid ios7 status bar overlap
                isTesting: false, // receiving test ad
                autoShow: true // auto show interstitial ad when loaded
            );

            registerAdEvents();

         else 
            alert( 'admob plugin not ready' );
        
    
    // optional, in case respond to events
    function registerAdEvents() 
        document.addEventListener('onReceiveAd', function());
        document.addEventListener('onFailedToReceiveAd', function(data));
        document.addEventListener('onPresentAd', function());
        document.addEventListener('onDismissAd', function() );
        document.addEventListener('onLeaveToAd', function() );
        document.addEventListener('onReceiveInterstitialAd', function() );
        document.addEventListener('onPresentInterstitialAd', function() );
        document.addEventListener('onDismissInterstitialAd', function() );
    
    function onResize() 
        var msg = 'web view: ' + window.innerWidth + ' x ' + window.innerHeight;
        document.getElementById('sizeinfo').innerHTML = msg;
    
    </script>
<div id="fullpage">
    <p>Demo for AdMob Plugin</p>
    <p><button onclick="window.plugins.AdMob.createBannerView();">create Ad</button> <button onclick="window.plugins.AdMob.destroyBannerView();">remove Ad</button></p>
    <p><button onclick="window.plugins.AdMob.showAd(true,function(),function(e)alert(JSON.stringify(e)););">show Ad</button> <button onclick="window.plugins.AdMob.showAd(false);">hide Ad</button></p>
    <p><button onclick="window.plugins.AdMob.createInterstitialView();">create Interstitial Ad</button></p>
    <p><button onclick="window.plugins.AdMob.showInterstitialAd(true,function(),function(e)alert(JSON.stringify(e)););">show Interstitial Ad</button></p>
    <div id="sizeinfo">width * height</div>
    <div>Try rotate screen to test the orientation change</div>

</div>

</body>
</html>

【问题讨论】:

【参考方案1】:

由于您没有指定实际使用的 admob 插件,我可以使用另一个插件 (https://github.com/sunnycupertino/cordova-plugin-admob-simple) 来满足您的需求:

添加插件

cordova plugin add cordova-plugin-admob-simple

整合如下:

-添加以下javascript函数,放入您自己的广告代码,根据需要使用变量。

-从 onDeviceReady() 调用 initAd(),并调用 showBannerFunc() 和 showInterstitialFunc() 来展示广告。

//initialize the goodies
function initAd()
        if ( window.plugins && window.plugins.AdMob ) 
            var ad_units = 
                ios : 
                    banner: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx',       //PUT ADMOB ADCODE HERE
                    interstitial: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx'  //PUT ADMOB ADCODE HERE
                ,
                android : 
                    banner: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx',       //PUT ADMOB ADCODE HERE
                    interstitial: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx'  //PUT ADMOB ADCODE HERE
                
            ;
            var admobid = ( /(android)/i.test(navigator.userAgent) ) ? ad_units.android : ad_units.ios;

            window.plugins.AdMob.setOptions( 
                publisherId: admobid.banner,
                interstitialAdId: admobid.interstitial,
                adSize: window.plugins.AdMob.AD_SIZE.SMART_BANNER,  //use SMART_BANNER, BANNER, IAB_MRECT, IAB_BANNER, IAB_LEADERBOARD
                bannerAtTop: false, // set to true, to put banner at top
                overlap: true, // banner will overlap webview 
                offsetTopBar: false, // set to true to avoid ios7 status bar overlap
                isTesting: false, // receiving test ad
                autoShow: false // auto show interstitial ad when loaded
            );

            registerAdEvents();
            window.plugins.AdMob.createInterstitialView();  //get the interstitials ready to be shown
            window.plugins.AdMob.requestInterstitialAd();

         else 
            //alert( 'admob plugin not ready' );
        

//functions to allow you to know when ads are shown, etc.
function registerAdEvents() 
        document.addEventListener('onReceiveAd', function());
        document.addEventListener('onFailedToReceiveAd', function(data));
        document.addEventListener('onPresentAd', function());
        document.addEventListener('onDismissAd', function() );
        document.addEventListener('onLeaveToAd', function() );
        document.addEventListener('onReceiveInterstitialAd', function() );
        document.addEventListener('onPresentInterstitialAd', function() );
        document.addEventListener('onDismissInterstitialAd', function()
            window.plugins.AdMob.createInterstitialView();          //REMOVE THESE 2 LINES IF USING AUTOSHOW
            window.plugins.AdMob.requestInterstitialAd();           //get the next one ready only after the current one is closed
        );
    

//display the banner
function showBannerFunc()
    window.plugins.AdMob.createBannerView();

//display the interstitial
function showInterstitialFunc()
    window.plugins.AdMob.showInterstitialAd();

【讨论】:

以上是关于科尔多瓦中的admob插件错误的主要内容,如果未能解决你的问题,请参考以下文章

科尔多瓦推送通知插件抛出错误

Cordova AdMob 插件 404 错误:http://registry.cordova.io/com.admob.AdmobPlugin

离子科尔多瓦文件传输插件超时错误代码3

如何使用 Cordova 从 MainViewController 切换 admob 视图

Cordova - Admob cordova 插件错误,黑色和空白广告

无法从本地文件夹添加科尔多瓦插件