2016-05-05 12 views
0

私はそれを解決する方法がわからないという問題があります。私のlocalNotificationはボタンでうまくいっていますが、私が必要とするのは、ページを開いて、通知機能を自動的に呼び出すときです。どうすればいいですか?ページが開いたときの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> 

答えて

0

あなたは、ビュー/ページが完全に入力され、現在アクティブなビューであるた直後に特定のコードを実行するために、あなたの$の範囲で$ionicView.enterイベントを使用することができます。

.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

+0

ない

このイベントは、それが最初のロードまたは

あなたSampleController新しいキャッシュされたビューがあったかどうか、起動します私のために働いた男。 – Sobucki

+0

私はアラートを試みたが何も表示しなかった – Sobucki

関連する問題