Search code examples
ionic-frameworkionic3cordova-plugin-fcm

Go to a specific page when clicking on a notification


How to go to a specific page when clicking on a notification? I use Firebase console to push the notification I used like this. but it didn't work

fcm.onNotification().subscribe(data=>{
  if(data.wasTapped){
this.navcntrl.push(MessagePage); 
  };
})

How i can do it correctly?

My full code: app.component.ts

 export class MyApp {
      @ViewChild(Nav) nav: Nav;

      rootPage: any = HomePage;
      pages: Array<{title: string, component: any}>;
      constructor(public platform: Platform,private fcm: FCM, public statusBar: StatusBar, public splashScreen: SplashScreen) {

        fcm.onNotification().subscribe(data=>{
          this.nav.push(ListPage)
        })
        this.initializeApp();
        this.pages = [
          { title: 'Home', component: HomePage },
          { title: 'List', component: ListPage }
        ];

      }

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

          this.statusBar.styleDefault();
          this.splashScreen.hide();
        });
      }

      openPage(page) {

        this.nav.setRoot(page.component);
      }
    }

Thanks in advance


Solution

  • I solve this issue. You have to add the property “click_ action”: “FCM_PLUGIN_ACTIVITY” to the “notification” object in order to properly use the background method.

    Example:

    {
     "to" : "tokenHere",
     "notification" : {
         "title": "title",
         "body" : "message",
         "sound": "default",
         "click_action":"FCM_PLUGIN_ACTIVITY"
     },
     "data": {
        "content": "whatever",
     },
     "priority":"high"
    }
    

    and use FCM API. Don’t use firebase console.