[英]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.