cordovaLocalNotification:单击state.go不起作用

时间:2015-12-17 13:09:24

标签: angularjs cordova redirect ionic localnotification

我尝试为我的离子cordova应用设置本地通知,但是当我点击重定向到主页时不起作用:这是我的控制器:

app.controller('notificationCtrl', function($scope, $rootScope, $ionicPlatform, $cordovaLocalNotification, $state){
      $scope.add = function() {
        var alarmTime = new Date();
        alarmTime.setMinutes(alarmTime.getMinutes() + 1);
        $cordovaLocalNotification.add({
            id: 1,
            date: alarmTime,
            message: "Bonjour",
            title: "nouvelle commande",
            autoCancel: true,
            sound: null
        }).then(function () {
            alert("The notification has been set");
        });
    };
     $rootScope.$on('$cordovaLocalNotification:click',
    function (event, notification, state) {
      $state.go('home');
      alert('ok then ');
    });

});

1 个答案:

答案 0 :(得分:3)

根据Docs,您必须在 $ ionicPlatform.ready() 中执行此操作, 如果应用程序未运行,也会在deviceready之后调用click事件。

app.controller('NotificationCtrl', function($scope, $rootScope, $ionicPlatform, $cordovaLocalNotification, $state){

 $ionicPlatform.ready(function () {

  $scope.add = function() {
    var alarmTime = new Date();
    alarmTime.setMinutes(alarmTime.getMinutes() + 1);
    $cordovaLocalNotification.add({
        id: 1,
        date: alarmTime,
        message: "Bonjour",
        title: "nouvelle commande",
        autoCancel: true,
        sound: null
    }).then(function () {
        alert("The notification has been set");
    });
  };

  $rootScope.$on('$cordovaLocalNotification:click',
   function (event, notification, state) {
    $state.go('home');
    alert('ok then ');
  });

 });
});