2016-02-11 61 views
0

我已经使用我的Ionic App实施了cordova-admob-pro插件。如何仅在完成关卡后才显示AdMob插页式广告

我希望在完成某个级别后显示插页式广告,并在应用加载时显示而不是

这里是我的代码:

app.js

// AdMob 

    if (window.plugins && window.plugins.AdMob && showAds) { 
     DebugService.add('admob OK'); 
     if (window.plugins && window.plugins.AdMob) { 
      var admob_banner_key = device.platform == "Android" ? "ca-app-pub-7...6" : "ca-app-pub-7...0"; 
      var admob_interstitial_key = device.platform == "Android" ? "ca-app-pub-7...5 " : "ca-app-pub-7...6"; 
      var admob = window.plugins.AdMob; 
      admob.createBannerView(
        { 
         'publisherId': admob_banner_key, 
         'adSize': admob.AD_SIZE.BANNER, 
         'bannerAtTop': false 
        }, 
        function() { 
         admob.requestAd(
          {'isTesting': true}, 
          function() { 
           admob.showAd(true); 
          }, 
          function() { 
           DebugService.add('failed to request ad'); 
          } 
         ); 
        }, 
        function() { 
         DebugService.add('failed to create banner view'); 
        } 
      ); 
      admob.createInterstitialView(
        { 
         'publisherId': admob_interstitial_key, 
         'autoShow': false 
        }, 
        function() { 
         DebugService.add('interstitial successfully craeted'); 
        }, 
        function() { 
         DebugService.add('failed to add interstitial :-('); 
        } 
      ); 
     } 
    } 

controller.js:

  if ($scope.levelId % 3 === 0) { 
       // after every second level and every third level after that 
       DebugService.add("Show interstitial ad :-)"); 
       if ($localStorage.noAdsPurchased !== true && 
         window.plugins && window.plugins.AdMob) { 
        DebugService.add('Ready to show interstitial'); 
        window.plugins.AdMob.requestInterstitialAd(
         {'isTesting': true}, 
         function() { 
          window.plugins.AdMob.showInterstitialAd(true, 
           function(){ 
            DebugService.add('interstitial successfully shown'); 
           }, 
           function(){ 
            DebugService.add('interstitial show failed'); 
           }); 
          DebugService.add('interstitial successfully requested'); 
         }, 
         function() { 
          DebugService.add('failed to request interstitial'); 
         } 
        ); 
        DebugService.add('After showing interstitial'); 
       } 
      } 

的插页式广告,也显示了在启动,这是我不想要的。如果我删除了代码,或者尝试仅将app.js中的代码用于插页式广告,那么插页式广告根本不会显示。我在想,因为他们还没有开始。

如何在不开始插入广告的情况下启动插页式广告?

+0

此人响应也是你:http://stackoverflow.com/ a/35362973/513570 – Miquel

+0

我添加了'autoShow':false和showInterstitialAd(),广告不再在启动时显示,但它也不会在第2级后显示 – cw24

+0

您确定showInterstitialAd()为ca大会另作?另外,你确定你有广告展示吗? – Miquel

回答

0

感谢您的意见。这终究是我解决问题的方法。

似乎只要调用createInterstitialView方法,就会显示插页式广告。这就是它从一开始就表现出来的原因。

为了解决这个问题,我将所有的AdMob代码转移到了自己的服务中,并使用了初始化,创建和展示广告的方法。这样,直到要显示广告才会调用createInterstitialView。控制器调用createIntersititalView

wwdWordRecognition.service('AdmobService', ['DebugService', function (DebugService) { 

    var _bannerId; 
    var _interstitialId; 

    var _isAdmobMissing = function() { 
     if(window.plugins === undefined || window.plugins.AdMob === undefined) { 
      return true; 
     } 
     return false; 
    }; 

    this.init = function(bannerId, interstitialId) { 
     _bannerId = bannerId; 
     _interstitialId = interstitialId; 
    }; 

    this.createAdmobBanner = function() { 
     if(_isAdmobMissing()) { 
      return false; 
     } 
     var _admob = window.plugins.AdMob; 
     _admob.createBannerView(
       { 
        'publisherId': _bannerId, 
        'adSize': _admob.AD_SIZE.BANNER, 
        'bannerAtTop': false 
       }, 
       function() { 
        _admob.requestAd(
         {'isTesting': true}, 
         function() { 
          _admob.showAd(true); 
         }, 
         function() { DebugService.add('failed to request ad'); } 
        ); 
       }, 
       function() { DebugService.add('failed to create banner view');} 
     ); 
    }; 

    this.showInterstitial = function() { 
     if(_isAdmobMissing()) { 
      return false; 
     } 
     DebugService.add('Admob plugin found.'); 
     var _admob = window.plugins.AdMob; 
     _admob.createInterstitialView(
      { 
       'publisherId': _interstitialId 
      }, 
      function() { 
       DebugService.add('interstitial successfully craeted'); 
       _admob.requestInterstitialAd(
        {'isTesting': true}, 
        function() { 
         _admob.showInterstitialAd(true, 
          function(){ DebugService.add('interstitial successfully shown'); }, 
          function(){ DebugService.add('interstitial show failed'); }); 
         DebugService.add('interstitial successfully requested'); 
        }, 
        function() { DebugService.add('failed to request interstitial'); } 
       ); 
      }, 
      function() { DebugService.add('failed to add interstitial :-('); } 
     ); 
    }; 
}]); 

的app.js现在看起来是这样的:

// AdMob 

DebugService.add('Start ad setup...'); 
if (showAds) { 
    if (window.plugins && window.plugins.AdMob) { 
     var _admob_banner_key = device.platform == "Android" ? "ca-app-pub-7...6" : "ca-app-pub-7...0"; 
     var _admob_interstitial_key = device.platform == "Android" ? "ca-app-pub-7...5 " : "ca-app-pub-7...6"; 
     AdmobService.init(_admob_banner_key,_admob_interstitial_key); 
     AdmobService.createAdmobBanner(); 
    } else { 
     DebugService.add('Admob plugin not found'); 
    } 
} 

最后的controller.js:

if ($scope.listId % 3 === 0) { 
    // after every third list 
    DebugService.add("Show interstitial ad."); 
    if ($localStorage.noAdsPurchased !== true && 
      window.plugins && window.plugins.AdMob) { 
     AdmobService.showInterstitial(); 
    } 
}