Home > Mobile >  When app is killed I am not able to route to a specific page with notification in flutter
When app is killed I am not able to route to a specific page with notification in flutter

Time:12-29

I am trying to go to a page when notification is clicked. It works fine when app is in background or foreground but it is not working when app is killed.

When app is killed it opens the app but doesn't go to the route defined in the notification data.

Need a quick solution

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)=>
    NotificationPop().onOpenApp(message, navigatorKey));

CodePudding user response:

When you want to navigate or catch data from your notification(also known as RemoteMessage) when the app is killed, you need to use the following function of Firebase Package:

FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) {
      if(message != null && message.data['title'] == 'SomeTitle') {
        //Navigate to a page
      }
    });

Place the above function in the init function of your desired page and whenever your app navigates to that page, the above function will get called. I am assuming you have integrated the firebase in your app correctly.

CodePudding user response:

Firebase package provides different methods to handle different scenarios, i.e. Terminated app, app in background and open app.

You have to implement getInitialMessage() method to handle notification in terminated app.

 FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) {
   //YourCode  
  });

You can also check other methods to manage diff scenarios like duplicate notifications:

 FirebaseMessaging.onMessage.listen((message) //YourCode  });
  • Related