Onesignal,Ionic App:单击通知时打开特定视图不起作用
Posted
技术标签:
【中文标题】Onesignal,Ionic App:单击通知时打开特定视图不起作用【英文标题】:Onesignal, Ionic App : Open specific view when click on notification not working 【发布时间】:2016-10-25 17:26:07 【问题描述】:我创建了一个基于 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);
);
)
这里是状态提供者
.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/');
);
【问题讨论】:
【参考方案1】:我自己犯规了,如果有人为此苦苦挣扎,这里是正确的代码:
.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);
);
)
【讨论】:
以上是关于Onesignal,Ionic App:单击通知时打开特定视图不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Ionic3 OneSignal 如何检查应用程序是不是由于通知打开事件而启动
将OneSignal插件添加到Ionic App后生成APK时出错
Ionic 5 电容器:使用 ONESIGNAL 的推送通知在 iOS 上不起作用