Ionic $ state.go in $ ionicPlatform.ready(function()

时间:2016-06-22 10:33:32

标签: javascript angularjs cordova ionic-framework

接收推送通知后,离线路由有问题。通过点击消息打开应用程序接收通知时,通过自定义密钥,ex.'myKey'进行通知,并且回拨必须在应用程序中打开页面。例:

var notificationOpenedCallback = function(jsonData) {
  if (jsonData.additionalData) {
    if (jsonData.additionalData.myKey)
      location.href = jsonData.additionalData.myKey;
  }
}

在我的Ionic应用程序中使用$ stateProvider。当收到通知我想打开这个状态时:

  .state('app.post', {
    url: "/posts/:postId",
    views: {
      'menuContent': {
        templateUrl: "templates/post.html",
        controller: 'PostCtrl'
      }
    }
  })

我试过这个,其中'myKey'收到postID:

var notificationOpenedCallback = function(jsonData) {
  if (jsonData.additionalData) {
    if (jsonData.additionalData.myKey)
      $state.go('app.post', {'postId': + jsonData.additionalData.myKey});
  }
}

但它不起作用,这里是所有代码:

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

 var notificationOpenedCallback = function(jsonData) {
  if (jsonData.additionalData) {
    if (jsonData.additionalData.myappurl)
      $state.go('app.post', {'postId': + jsonData.additionalData.myKey});
  }
}

    window.plugins.OneSignal.init("###############",
                                   {googleProjectNumber: "###########"},
                                   notificationOpenedCallback); 



    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }   
  });
})

1 个答案:

答案 0 :(得分:2)

尝试将$state传递到您的run区块:

.run(function($ionicPlatform, $state) {
  $ionicPlatform.ready(function() {
    var notificationOpenedCallback = function(jsonData) {
      if (jsonData.additionalData) {
        if (jsonData.additionalData.myappurl)
          $state.go('app.post', {'postId': + jsonData.additionalData.myKey});
      }
    }
  })
  // the rest of your code
})