【问题标题】:How to integrate AdMob ads into a Cordova Project for both Android and iOS?如何将 AdMob 广告集成到适用于 Android 和 iOS 的 Cordova 项目中?
【发布时间】:2016-03-28 19:01:08
【问题描述】:

我正在使用最新版本 (6) 在 Cordova 中编写一个多平台应用程序,但在尝试让 AdMob 广告在 iOS 和 Android 上运行时遇到了很多麻烦。我已经下载了 AdMob 的代码示例,但是从 javascript 控制它让我很困惑。我对插件架构有所了解,但我似乎无法让它发挥作用。

请帮忙。

【问题讨论】:

    标签: android ios cordova plugins admob


    【解决方案1】:

    最好的办法是为此使用预制插件。正如您所提到的,我有使用 Cordova 6 在 iOS 和 Android 上都适用于我的体验。

    完整说明在此处https://github.com/sunnycupertino/cordova-plugin-admob-simple 或此处https://www.npmjs.com/package/cordova-plugin-admob-simple

    安装:

    cd yourappfolder
    
    cordova plugin add cordova-plugin-admob-simple
    

    如果您使用的是 Eclipse,请将 google-play-services.jar 复制到 libs 文件夹中。

    将以下行添加到清单文件中,就在结束应用程序标记之前

    <meta-data android:name="com.google.android.gms.version" android:value="8487000" />
    

    现在在您的 javascript 中,添加以下函数:

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

    从 onDeviceReady() 调用 init()

    调用 showInterstitialFunc() 和 showBannerFunc() 来展示广告。

    请记住,在显示插页式广告之前,您必须稍等片刻,因为加载需要时间。

    希望这会有所帮助。

    【讨论】:

    • 是否有机会控制用户是否关闭奖励视频以防止他通过此插件赚取“积分”?
    • 小心:这个插件会占用你的部分广告收入github.com/sunnycupertino/cordova-plugin-admob-simple/blob/…
    • @syonip 怎么会这样? if 检查发布者 ID 是否包含“xxx”,因此您将不得不错过配置一些东西
    【解决方案2】:

    这对我来说是最简单的方法:

    https://www.npmjs.com/package/cordova-plugin-admobpro-firebase 我已经在使用它了。 IOS 和 Android 都易于使用...

    感谢@ir2pid “此用户在未说明的情况下获得了高达 30% 的收入折扣。” 所以它会占用您的部分收入。

    【讨论】:

    • 此用户在未说明的情况下获得高达 30% 的收入折扣。请检查插件的问题列表以获得解释,除非它已被所有者删除。
    • ""此用户在没有说明的情况下获得高达 30% 的收入折扣" 哪个用户@ir2pid?
    • 插件的创建者
    【解决方案3】:

    您可以使用以下插件:

    Admob-plus

    这似乎是目前最流行的包。它是cordova-plugin-admob-free 插件的继承者。我目前正在研究它以供我使用。

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多