类型错误:无法读取 cordovaLocalNotification 插件未定义的属性"插件"



我正在利用离子平台开发混合应用。我正在实现cordovalocalnotification功能,但它提示无法读取未定义的属性"插件"。以下是我的代码。任何人都可以帮我解决这个问题。

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 uncomment 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");
                });;
            };
            };
        });
    });

window.cordova.plugins代替cordova.plugins

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

添加插件

把上面的代码写在ondeviceready中,它应该可以工作了。

您可以使用开发人员工具进行验证。调试您的移动应用程序并检查cordova.plugins.Keyboard

最新更新