Navigating to a new page from a Service Provider

I need to navigate to a new page (by any means, either pushing or setting root) from within my Provider or my app.components.ts. The use case is Push Notifications via FCM, and I need to move the user to the correct page if he/she clicks on a push notification. this doesn’t work (I still don’t honestly understand why) because NavController doesn’t work within Providers or within app.conponent.ts.

Any advice on how I might be able to solve this problem will be highly appreciated! Many thanks!

THIS CODE MUST BE EITHER IN A PROVIDER OR APP.COMPONENTS.TS:
    this.fcm.onNotification().subscribe((data)=> {
        if(data.wasTapped) {
          this.navCtrl.push(MessagesPage);
        }
        else {
          console.log("Received in foreground");
        };
      });
      this.app.getRootNav().getActiveChildNav().select(3);

I would vastly prefer putting this in the app component over putting it in a service provider, which would eliminate any need for injecting app at all. Magic numbers like that ‘3’ also make for difficult to read and maintain code.

1 Like

I will likely do it this way eventually, for now it’s a bit cleaner for me to separate any much code to providers. But I agree, I don’t see any more place where to use this code, all the magic is happening in the backend of course.

Please how did you get this to work? I have a similar issue having to navigate from my notification to a particular page but i keep ending up on the homepage. I have been looking for solution for about 3 weeks now and no result. I would appreciate the help. Thanks.

Here is my code…

this.fcm.onNotification().subscribe( data => {
    if(data.wasTapped){
      //Notification was received on device tray and tapped by the user.
      this.postId = parseInt(data.id);
      
      this.nav.push(PostDetailsRel1Page, {
         item:  this.postId
      });
    } else {
      //Notification was received in foreground. Maybe the user needs to be notified.
    }
});

From where do you get this.app