Sadqain Kazmi
Sadqain Kazmi

Reputation: 29

how to navigate to specific page when tapped on One Signal push notification in ionic 3?

I want to open a specific page when tapped on a push notification sent by One Signal. I have tried several ways but did not find any way work around !! here is my code.

i have tried all navigation ways


        // window["plugins"].OneSignal.sendTag("user_tag", "VIP");

        if (jsonData.notification.payload.additionalData != null) {


          let navigator: any = JSON.stringify(jsonData.notification.payload.additionalData.page);
          alert(navigator);
          alert(navigator == '"TodayspickPage"');
          if (navigator == '"TodayspickPage"') {
            //I TRIED THESE ALL WAYS
            // this.nav.setRoot(TodayspickPage);
            // this.rootPage=TodayspickPage;
            // this.app.nav.push(TodayspickPage);
           //this.nav.push(TodayspickPage);
          }



        }

      };


      window["plugins"].OneSignal
        .startInit("47c084d5-**************437ca9f9bbd", "8324*******08")
        .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
        .handleNotificationOpened(notificationOpenedCallback)
        .endInit();

Upvotes: 2

Views: 1487

Answers (1)

Marco Chavez
Marco Chavez

Reputation: 365

I also use OneSignal for my push notications, I use this way to implement that:

initializeApp() {    
this.platform.ready().then(() => {

    let self = this;
    var notificationOpenedCallback = async function(jsonData) {
      switch (self.user.role) {
        case "client":
          self.router.navigate(["history-customer"]);
          break;
        case "provider":
          self.router.navigate(["history-provider"]);
          break;
        default:
          self.router.navigate(["history-customer"]);
      }
    };

    window["plugins"].OneSignal.startInit(
      "03b**************c7",
      "1**********2"
    )
      .handleNotificationOpened(notificationOpenedCallback)
      .endInit();

    window["plugins"].OneSignal.setSubscription(true);
  });
}

All that inside on platform.ready on app.component.ts, change the code for your case.

I use

let self = this

because i guess for startInit recieve a callback so is necesary to do that, and i use Router to navigate beetween pages.

import { Router } from "@angular/router"; 

Result on my project, its works same with click notifications outside of app.

I hope I've helped :)

enter image description here

Upvotes: 2

Related Questions