Ionic framework 离子2/3中的一个信号附加数据
我正在尝试在我的ionic 2应用程序中使用一个信号插件 我安装了一个信号,它工作正常,但我不知道如何使用Ionic framework 离子2/3中的一个信号附加数据,ionic-framework,notifications,ionic2,push,onesignal,Ionic Framework,Notifications,Ionic2,Push,Onesignal,我正在尝试在我的ionic 2应用程序中使用一个信号插件 我安装了一个信号,它工作正常,但我不知道如何使用handleNotificationOpened功能 根本没有文档(未找到任何文档) 这是我的代码: this.oneSignal.handleNotificationReceived().subscribe((msg) => { // o something when notification is received }); 但是我不知道如何使用msg获取
handleNotificationOpened
功能
根本没有文档(未找到任何文档)
这是我的代码:
this.oneSignal.handleNotificationReceived().subscribe((msg) => {
// o something when notification is received
});
但是我不知道如何使用msg
获取数据
有什么帮助吗?链接
告诉你当应用程序从通知启动时,我是如何将用户重定向到相关页面的 应用程序组件.ts
this.oneSignal.handleNotificationOpened().subscribe((data) => {
let payload = data; // getting id and action in additionalData.
this.redirectToPage(payload);
});
redirectToPage(data) {
let type
try {
type = data.notification.payload.additionalData.type;
} catch (e) {
console.warn(e);
}
switch (type) {
case 'Followers': {
this.navController.push(UserProfilePage, { userId: data.notification.payload.additionalData.uid });
break;
} case 'comment': {
this.navController.push(CommentsPage, { id: data.notification.payload.additionalData.pid })
break;
}
}
}
更好的解决方案是重置当前导航堆栈并重新创建它。为什么? 让我们看看这个场景: TodosPage(根页面)->TodoPage(推送)->评论页面(推送) 如果您直接转到CommentsPage,“返回”按钮将无法按预期工作(它已消失或将您重定向到…谁知道在哪里:D) 这就是我的建议:
this.oneSignal.handleNotificationOpened().subscribe((data) => {
// Service to create new navigation stack
this.navigationService.createNav(data);
});
导航.service.ts
import {Injectable} from '@angular/core';
import {App} from 'ionic-angular';
import {TodosPage} from '../pages/todos/todos';
import {TodoPage} from '../pages/todo/todo';
import {CommentsPage} from '../pages/comments/comments';
@Injectable()
export class NavigationService {
pagesToPush: Array<any>;
constructor(public app: App) {
}
// Function to create nav stack
createNav(data: any) {
this.pagesToPush = [];
// Customize for different push notifications
// Setting up navigation for new comments on TodoPage
if (data.notification.payload.additionalData.type === 'NEW_TODO_COMMENT') {
this.pagesToPush.push({
page: TodoPage,
params: {
todoId: data.notification.payload.additionalData.todoId
}
});
this.pagesToPush.push({
page: CommentsPage,
params: {
todoId: data.notification.payload.additionalData.todoId,
}
});
}
// We need to reset current stack
this.app.getRootNav().setRoot(TodosPage).then(() => {
// Inserts an array of components into the nav stack at the specified index
this.app.getRootNav().insertPages(this.app.getRootNav().length(), this.pagesToPush);
});
}
}
从'@angular/core'导入{Injectable};
从“离子角度”导入{App};
从“../pages/todos/todos”导入{TodosPage};
从“../pages/todo/todo”导入{TodoPage};
从“../pages/comments/comments”导入{CommentsPage};
@可注射()
导出类导航服务{
pagesToPush:数组;
构造函数(公共应用程序:应用程序){
}
//函数创建导航堆栈
createNav(数据:任意){
this.pagesToPush=[];
//针对不同的推送通知进行自定义
//为TodoPage上的新注释设置导航
if(data.notification.payload.additionalData.type==='NEW\u TODO\u COMMENT'){
这个是.pagesToPush.push({
页码:TodoPage,
参数:{
todoId:data.notification.payload.additionalData.todoId
}
});
这个是.pagesToPush.push({
第页:评论页,
参数:{
todoId:data.notification.payload.additionalData.todoId,
}
});
}
//我们需要重置当前堆栈
this.app.getRootNav().setRoot(TodosPage)。然后(()=>{
//在指定索引处将组件数组插入导航堆栈
this.app.getRootNav().insertPages(this.app.getRootNav().length(),this.pagesToPush);
});
}
}
我希望有帮助:)