I am experiencing an issue with Ionic routing after receiving a Push Notification. The notification contains a custom key, for example 'myKey'. When the user clicks on the notification message, it should open the application and the callback function must navigate to a specific page in the app. See below:
var notificationOpenedCallback = function(jsonData) {
if (jsonData.additionalData) {
if (jsonData.additionalData.myKey)
location.href = jsonData.additionalData.myKey;
}
}
In my Ionic application, I am using $stateProvider. When a notification is received, I want to navigate to the following state:
.state('app.post', {
url: "/posts/:postId",
views: {
'menuContent': {
templateUrl: "templates/post.html",
controller: 'PostCtrl'
}
}
})
I have attempted the following code, where 'myKey' corresponds to the postID:
var notificationOpenedCallback = function(jsonData) {
if (jsonData.additionalData) {
if (jsonData.additionalData.myKey)
$state.go('app.post', {'postId': + jsonData.additionalData.myKey});
}
}
However, this is not working as expected. Here is the complete code snippet:
.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();
}
});
})