最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read property 'plugins' of undefined for cordovaLocalNotification plugin - Stack

programmeradmin1浏览0评论

I am developing hybrid application by using ionic platform. I am implementing cordovalocalnotification features but it prompt out with cannot read property 'plugins' of undefined. The following is my code. Anyone can help me to solve this problem.

index.html

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
  <title></title>

  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">

  <!-- IF using Sass (run gulp sass first), then unment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

  <!-- ionic/angularjs js -->
  <script src="lib/ionic/js/ionic.bundle.js"></script>

  <!-- cordova script (this will be a 404 during development) -->
  <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
  <script src="cordova.js"></script>

  <!-- your app's js -->
  <script src="js/app.js"></script>
</head>

<body ng-app="starter" ng-controller="SampleController">

  <ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Local Notification Sample</h1>
    </ion-header-bar>
    <ion-content>

        <button class="button button-block button-positive" ng-click="scheduleInstantNotification()">
          Instant 
        </button>

    </ion-content>
  </ion-pane>
</body>

</html>

app.js

angular.module('starter', ['ionic', 'ngCordova'])
  .run(function ($ionicPlatform, $rootScope) {
    $ionicPlatform.ready(function () {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        $rootScope.$on('$cordovaLocalNotification:schedule',
            function (event, notification, state) {
                console.log("SCHEDULE");
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:trigger',
            function (event, notification, state) {
                console.log("TRIGGER");
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:update',
            function (event, notification, state) {
                console.log('UPDATE');
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:cancel',
            function (event, notification, state) {
                console.log('CANCEL');
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });
    });
  })
  .controller('SampleController',
    function ($scope, $cordovaLocalNotification, $ionicPlatform) {
        $ionicPlatform.ready(function () {

            $scope.scheduleInstantNotification = function () {
                $cordovaLocalNotification.schedule({
                    id: 1,
                    text: 'Instant Notification',
                    title: 'Instant'
                }).then(function () {
                    alert("Instant Notification set");
                });;
            };
            };
        });
    });

I am developing hybrid application by using ionic platform. I am implementing cordovalocalnotification features but it prompt out with cannot read property 'plugins' of undefined. The following is my code. Anyone can help me to solve this problem.

index.html

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
  <title></title>

  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">

  <!-- IF using Sass (run gulp sass first), then unment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

  <!-- ionic/angularjs js -->
  <script src="lib/ionic/js/ionic.bundle.js"></script>

  <!-- cordova script (this will be a 404 during development) -->
  <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
  <script src="cordova.js"></script>

  <!-- your app's js -->
  <script src="js/app.js"></script>
</head>

<body ng-app="starter" ng-controller="SampleController">

  <ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Local Notification Sample</h1>
    </ion-header-bar>
    <ion-content>

        <button class="button button-block button-positive" ng-click="scheduleInstantNotification()">
          Instant 
        </button>

    </ion-content>
  </ion-pane>
</body>

</html>

app.js

angular.module('starter', ['ionic', 'ngCordova'])
  .run(function ($ionicPlatform, $rootScope) {
    $ionicPlatform.ready(function () {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        $rootScope.$on('$cordovaLocalNotification:schedule',
            function (event, notification, state) {
                console.log("SCHEDULE");
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:trigger',
            function (event, notification, state) {
                console.log("TRIGGER");
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:update',
            function (event, notification, state) {
                console.log('UPDATE');
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });

        $rootScope.$on('$cordovaLocalNotification:cancel',
            function (event, notification, state) {
                console.log('CANCEL');
                console.log('event', event);
                console.log('notification', notification);
                console.log('state', state);
            });
    });
  })
  .controller('SampleController',
    function ($scope, $cordovaLocalNotification, $ionicPlatform) {
        $ionicPlatform.ready(function () {

            $scope.scheduleInstantNotification = function () {
                $cordovaLocalNotification.schedule({
                    id: 1,
                    text: 'Instant Notification',
                    title: 'Instant'
                }).then(function () {
                    alert("Instant Notification set");
                });;
            };
            };
        });
    });
Share Improve this question asked Oct 9, 2016 at 13:14 Chan Yoong HonChan Yoong Hon 1,8227 gold badges37 silver badges73 bronze badges 6
  • Can you check whether plugin is added to the project? – bvakiti Commented Oct 10, 2016 at 18:29
  • @bvakiti I plugin is added. – Chan Yoong Hon Commented Oct 11, 2016 at 12:51
  • Debug your app using developer tools, and check the value of cordova.plugins.Keyboard – bvakiti Commented Oct 11, 2016 at 13:07
  • There could only be two scenarios to verify, In your project folder, check if keyboard plugin is present in plugins folder or not ? Next debug your mobile app using chrome debugging tools and check the cordova.plugins.Keyboard . – bvakiti Commented Oct 11, 2016 at 13:13
  • @bvakiti cordova keyboard is plugin based on ionic-pugin-keyboard 2.2.1 "keyboard" – Chan Yoong Hon Commented Oct 11, 2016 at 13:19
 |  Show 1 more ment

2 Answers 2

Reset to default 3

Use window.cordova.plugins instead of cordova.plugins

if (window.cordova && window.cordova.plugins.Keyboard) {
      //cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      window.cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}

Add your plugin using cordova plugin add ionic-plugin-keyboard

Write the above code in ondeviceready, It should be working.

You can verify using developer tools. Debug your mobile app and check for cordova.plugins.Keyboard

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论