繁体   English   中英

页面打开时的LocalNotification

[英]LocalNotification when the page open

我有一个我不知道如何解决的问题。 我的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>

您可以在$ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM