Onesignal,Ionic App:单击通知不起作用时打开特定视图
Onesignal, Ionic App : Open specific view when click on notification not working
我创建了一个基于 Ionic 的混合 android 应用程序,使用 Onesignal 我正在向应用程序发送通知,它运行良好,用户应用程序收到通知,当我点击它时,它会打开应用主页视图。
我想做的是在用户单击通知时打开特定页面。
基于 documentation,这是我所做的:
我的 app.js 文件:
angular.module('starter', ['ionic','ngCordova', 'starter.controllers', 'ui.router' ])
.run(function($ionicPlatform,$ionicPopup,$cordovaSplashscreen,$state) {
$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);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(result) {
//var data = result.notification.payload.additionalData;
var state = $injector.get($state);
state.go('#/app/post/49726');
};
window.plugins.OneSignal.init("********-****-****-****-***********",
{googleProjectNumber: "**************"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);
});
})
这是stateProvider
.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {
$ionicConfigProvider.navBar.alignTitle('center');
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('app.home', {
url: "/home/",
views: {
'menuContent': {
templateUrl: "templates/posts.html",
controller: 'HomeCtrl'
}
}
})
.state('app.posts', {
url: "/posts/:categoryId",
views: {
'menuContent': {
templateUrl: "templates/posts.html",
controller: 'PostsCtrl'
}
}
})
.state('app.post', {
url: "/post/:postId",
views: {
'menuContent': {
templateUrl: "templates/post.html",
controller: 'PostCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/home/');
});
我自己犯规了,如果有人遇到这个问题,这里是正确的代码:
.run(function($ionicPlatform,$ionicPopup,$cordovaSplashscreen,$state,$injector) {
$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);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(result) {
$state.go('app.post', { "postId": "49726"});
};
window.plugins.OneSignal.init("********-****-****-****-***********",
{googleProjectNumber: "**************"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);
});
})
我创建了一个基于 Ionic 的混合 android 应用程序,使用 Onesignal 我正在向应用程序发送通知,它运行良好,用户应用程序收到通知,当我点击它时,它会打开应用主页视图。
我想做的是在用户单击通知时打开特定页面。
基于 documentation,这是我所做的:
我的 app.js 文件:
angular.module('starter', ['ionic','ngCordova', 'starter.controllers', 'ui.router' ])
.run(function($ionicPlatform,$ionicPopup,$cordovaSplashscreen,$state) {
$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);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(result) {
//var data = result.notification.payload.additionalData;
var state = $injector.get($state);
state.go('#/app/post/49726');
};
window.plugins.OneSignal.init("********-****-****-****-***********",
{googleProjectNumber: "**************"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);
});
})
这是stateProvider
.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {
$ionicConfigProvider.navBar.alignTitle('center');
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('app.home', {
url: "/home/",
views: {
'menuContent': {
templateUrl: "templates/posts.html",
controller: 'HomeCtrl'
}
}
})
.state('app.posts', {
url: "/posts/:categoryId",
views: {
'menuContent': {
templateUrl: "templates/posts.html",
controller: 'PostsCtrl'
}
}
})
.state('app.post', {
url: "/post/:postId",
views: {
'menuContent': {
templateUrl: "templates/post.html",
controller: 'PostCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/home/');
});
我自己犯规了,如果有人遇到这个问题,这里是正确的代码:
.run(function($ionicPlatform,$ionicPopup,$cordovaSplashscreen,$state,$injector) {
$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);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(result) {
$state.go('app.post', { "postId": "49726"});
};
window.plugins.OneSignal.init("********-****-****-****-***********",
{googleProjectNumber: "**************"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);
});
})