一个信号离子2/3中的附加数据

时间:2017-07-19 20:13:49

标签: ionic-framework notifications ionic2 push onesignal

我正在尝试使用我的离子2应用程序中的一个信号插件

我安装了一个信号并且工作正常。

但我不知道如何使用handleNotificationOpened函数 没有文件(我找不到任何东西)

我有这个:

         this.oneSignal.handleNotificationReceived().subscribe((msg) => {
   // o something when notification is received
  });

但不知道如何使用msg获取数据

任何帮助?链接? ......坦克你

2 个答案:

答案 0 :(得分:4)

以下是我在应用程序从通知启动时将用户重定向到相关页面的方式。

app.component.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;
      }
    }
  }

答案 1 :(得分:1)

更好的解决方案是重置当前导航堆栈并重新创建。为什么?

让我们看看这种情况:

TodosPage(rootPage)-> TodoPage(push)-> CommentsPage(push)

如果您直接转到CommentsPage,则“返回”按钮将无法按预期方式工作(该按钮消失了或将您重定向到...谁知道:D)。

这是我的建议:

this.oneSignal.handleNotificationOpened().subscribe((data) => {
    // Service to create new navigation stack
    this.navigationService.createNav(data);
  });

navigation.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);
        });
      }

    }

我希望这会有所帮助:)