one signal additional data in ionic 2/3

Viewed 2711

I'm trying to work with one signal plugin in my ionic 2 app

I've installed Onesignal and it was working fine,but i don't know how to work with handleNotificationOpened function there is no document at all (nothing was found)

this is my code:

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

but I have no idea how to use msg for getting data.

any help? link? tank you

2 Answers

A better solution would be to reset the current nav stack and recreate it. Why?

Lets see this scenario:

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

If you go directly to CommentsPage the "go back" button won't work as expected (its gone or redirect you to... who knows where :D).

So this is my proposal:

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

    }

I hope it helps :)

Related