页面打开时的LocalNotification

时间:2016-05-05 02:29:41

标签: javascript html cordova

我有一个问题,我不知道如何解决它。我的localNotification使用按钮很好用,但我需要的是当页面打开时,自动调用通知功能,我该怎么做?

 angular.module('starter', ['ionic', 'ngCordova', 'starter.controllers'])
    //enter code here
    .run(function($ionicPlatform, $rootScope) {
      $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.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
          cordova.plugins.Keyboard.disableScroll(true);

        }
        if (window.StatusBar) {
          // org.apache.cordova.statusbar required
          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");
                    });;
                };

            });
        })

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

        .state('app', {
        url: '/app',
        abstract: true,
        templateUrl: 'templatesInfo/menu.html',
        controller: 'AppCtrl'
      })

      .state('app.search', {
        url: '/search',
        views: {
          'menuContent': {
            templateUrl: 'templatesInfo/search.html'
          }
        }
      })

      .state('app.browse', {
          url: '/browse',
          views: {
            'menuContent': {
              templateUrl: 'templatesInfo/browse.html'
            }
          }
        })
        .state('app.playlists', {
          url: '/playlists',
          views: {
            'menuContent': {
              templateUrl: 'templatesInfo/playlists.html',
              controller: 'PlaylistsCtrl'
            }
          }
        })

      .state('app.single', {
        url: '/playlists/:playlistId',
        views: {
          'menuContent': {
            templateUrl: 'templatesInfo/playlist.html',
            controller: 'PlaylistCtrl'
          }
        }
      });
      // if none of the above states are matched, use this as the fallback
      $urlRouterProvider.otherwise('/app/playlists');
    });

查看:

 <ion-view view-title="Information">
      <ion-content>


         <div class="list card" >

              <div class="item item-avatar" >

                <h2>Dr. Therese Ouellet</h2>
                <p>Agriculture and Angri food Canada</p>
              </div>

              <div class="item item-body">
                <img class="full-image" src="img/therese.jpg">
                <p>
                  This is a "Facebook" styled Card. The header is created from a Thumbnail List item,
                  the content is from a card-body consisting of an image and paragraph text. The footer
                  consists of tabs, icons aligned left, within the card-footer.
                </p>

              </div>


        </div>

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

      </ion-content>
    </ion-view>

1 个答案:

答案 0 :(得分:0)

您可以在$ scope中使用$ionicView.enter事件在视图/页面完全进入后立即运行特定代码,现在是活动视图。

  

此事件将触发,无论是第一次加载还是缓存视图

您的新 SampleController

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

            $scope.$on("$ionicView.enter", function(event, data){
                 // handle event
                 $scope.scheduleInstantNotification();
            });

        });
    })

在此处查看完整详情:ionView