在本地通知上打开特定页面单击 Ionic
Posted
技术标签:
【中文标题】在本地通知上打开特定页面单击 Ionic【英文标题】:Open a specific page on local notification click Ionic 【发布时间】:2019-03-11 04:38:14 【问题描述】:当用户单击本地通知并希望根据该通知打开特定页面时,我正在尝试打开我的应用程序。下面给出的是我的示例代码,但它不会重定向到我给出的页面。
platform.ready().then(() =>
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.localNotification.on('click', (event, notification, state) =>
this.isNotification = true;
console.log(this.isNotification);
this.notificationData = notification;
);
setTimeout(()=>
splashScreen.hide();
,500);
statusBar.styleDefault();
this.pushSettings();
if (localStorage.getItem("email"))
if (localStorage.getItem("userName"))
this.rootPage = TabstorePage
else
console.log(this.isNotification);
if(this.isNotification == true)
console.log("SalePage");
this.rootPage = SalePage
else
console.log("TabsPage");
this.rootPage = TabsPage
else
this.rootPage = LoginPage
);
【问题讨论】:
【参考方案1】:我必须面对同样的问题,这是我的方法。
在应用程序组件中,在 initializeApp() 调用订阅 observable 的服务函数,如下所示:
initializeApp()//in app components
...
this.localnotification.initHandler();//call to function in service
这是在本地通知服务中
initHandler()
this.localNotifications.on("click").subscribe(res =>
console.log(res);
if (res.data.redirect != null && res.data.redirect != undefined)
//make redirect to place you want
);
我希望这对某人有用。
【讨论】:
谢谢你这对我有用。您能否建议如何为此编写单元测试?【参考方案2】:对于电容器用户,有一个localNotificationActionPerformed
事件:
LocalNotifications.addListener('localNotificationActionPerformed', (payload) =>
// Redirect here
);
我是这样使用的:
LocalNotifications.schedule(
notifications: [
id: 1,
title: 'Hello',
body: 'World',
extra:
route: '/whatever',
,
,
],
);
LocalNotifications.addListener('localNotificationActionPerformed', (payload) =>
const route = payload.notification.extra.route;
// use route to redirect
);
【讨论】:
以上是关于在本地通知上打开特定页面单击 Ionic的主要内容,如果未能解决你的问题,请参考以下文章