KingStakh KingStakh - 5 months ago 195
Javascript Question

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

I am having some trouble with ionic routing after receive Push Notification. Have notification with custom Key, ex.'myKey', when receive notification by click on message open application and Callback must open page in app. Ex.:

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


In my Ionic application use $stateProvider. When receive notification i want to open this state:

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


and i have tried this, where 'myKey' is received postID:

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


but it is not working, here is all the code:

.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();
}
});
})

Answer

Try passing $state into your run block:

.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
})
Comments