【问题标题】:AdMob Pro and Cordova - Interstitial Ads working, Banner Ads not workingAdMob Pro 和 Cordova - 插页式广告有效,横幅广告无效
【发布时间】:2015-07-16 21:52:26
【问题描述】:

我目前正在使用 Ionic / Cordova 框架开发应用程序。

我已通过 AdMob(和 this plugin)成功实施了插页式广告,并且效果不错。

但是,我无法让横幅工作。而且代码很相似。

这是我使用的函数(非常标准):

$scope.showBanner = function()
  {
    var admobid = {};

    if(/(android)/i.test(navigator.userAgent) ) 
    {
        admobid = 
        {
            banner: 'ca-app-pub-XXXXXXXXXXX/YYYYYYYYYY',
            interstitial: 'ca-app-pub-XXXXXXXXXXX/AAAAAAAAAAAAAA'
        };
    } else if(/(ipod|iphone|ipad)/i.test(navigator.userAgent)) 
    {
        admobid = 
        {
            banner: 'ca-app-pub-XXXXXXXXXXX/CCCCCCCCCCCC',
            interstitial: 'ca-app-pub-XXXXXXXXXXX/ZZZZZZZZZZZZZZ'
        };
    } 

    if(window.AdMob)
    {
        window.AdMob.createBanner({
        adId: admobid.banner,
        position: window.AdMob.AD_POSITION.BOTTOM_CENTER,
        autoShow: true});
    }
    else
    {
        //alert("Admob plugin not present");
        console.log("Admob plugin not present");
    }
 }

这应该非常简单明了,但没有显示横幅。奇怪的是,插页式广告效果很好。

我做错了什么?

【问题讨论】:

  • 如果某个插件无法正常工作,您可以考虑在插件的 github 存储库中打开一个问题
  • 我已经在努力解决这个问题,但我认为我是唯一一个遇到这个问题的人。
  • 经过更多小时的测试,我发现如果从主 AngularJS 控制器调用 showBanner() 函数不起作用。它在从另一个控制器调用时起作用。我不知道为什么。
  • 广告应该在 $ionicPlatform.ready() 函数这里是它的完整教程pointdeveloper.com/…

标签: angularjs cordova admob ionic


【解决方案1】:

所有的广告初始化代码都应该在 app.js 文件的 $ionicPlatform.ready() 函数中

这里是 tabs 项目的 app.js 文件的完整代码。

注意: app.js 文件对于侧边菜单和选项卡项目会有些不同,因此请务必仔细编辑

完整教程在这里:

http://pointdeveloper.com/how-to-add-banner-ads-to-ionic-apps-using-admob-pro-plugin/

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

.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 && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }

//======admob code start=============

      var admobid = {};
        // select the right Ad Id according to platform
        if( /(android)/i.test(navigator.userAgent) ) { 
            admobid = { // for Android
                banner: 'ca-app-pub-6869992474017983/9375997553',
                interstitial: 'ca-app-pub-6869992474017983/1657046752'
            };
        } else if(/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {
            admobid = { // for iOS
                banner: 'ca-app-pub-6869992474017983/4806197152',
                interstitial: 'ca-app-pub-6869992474017983/7563979554'
            };
        } else {
            admobid = { // for Windows Phone
                banner: 'ca-app-pub-6869992474017983/8878394753',
                interstitial: 'ca-app-pub-6869992474017983/1355127956'
            };
        }

  if(window.AdMob) AdMob.createBanner( {
      adId:admobid.banner, 
      position:AdMob.AD_POSITION.BOTTOM_CENTER, 
      autoShow:true} );

//=======AdMob Code End=======

});//app ready function ends
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: "/tab",
    abstract: true,
    templateUrl: "templates/tabs.html"
  })

  // Each tab has its own nav history stack:

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.chats', {
      url: '/chats',
      views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chats.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/dash');

});

【讨论】:

  • 谢谢您,也感谢您发布教程链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多