'Ionic OneSignal Push Notification set the root instead of pushing it from Home to Another Page

I'm having an issue with my OneSignal push notification page redirection. When I tap on my push notification when the App is running, it pushes the page (news-detail) to stack from the current page (home) that I'm viewing. (This is what I want when the app is closed)

When the app is killed, and I receive the push notification, tapping on the push notification will directly bring me to the news-detail page without stacking the home page. (I want the app to open from home page, and then push the page to news-detail page.)

Here's my current code to route the push notification to another page.

OneSignal.setNotificationOpenedHandler((data: any) => {
      // console.log('notificationOpenedCallback: ' + JSON.stringify(data));
      console.log(data);

      let postid = data.notification.additionalData.id;

      console.log(postid);

      this.api.routePath("news-detail", postid); 

      // this.showAlert("Notification opened", "You already read this before", additionalData);

  });

Under ApiService - api I have this code

routePath(path, data) {
    this.router.navigateByUrl(`/` + path + `/${data}`);
}


Solution 1:[1]

You can set replaceUrl to true to overwrite the current history entry.(see https://angular.io/api/router/NavigationBehaviorOptions#replaceUrl).

routePath(path, data) {
    this.router.navigateByUrl(`/` + path + `/${data}`, { replaceUrl: true });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RGe