skip to Main Content

I have to open specific screens when user clicks on a notification following the payload it has (classic…).
here is the code :

// Listener 2 :: When notif clicked and open the app :
FirebaseMessaging.onMessageOpenedApp.listen((message) async {
      switch (message.data["type"]) {
        case "case1" :
          navigatorKey.currentState!.push(MaterialPageRoute(builder: (_) => Case1Page());
          break;
        case "case2" :
          navigatorKey.currentState!.push(MaterialPageRoute(builder: (_) => Case2Page());
          break;
        default :
          // nothing
      }
    });

The problem is that it ONLY works when app is in background when user clicks the notification !
It doesn’t work when app was killed, it just opens the app on homepage

I spent some time searching for a fix but i only find the :

  FirebaseMessaging.configure(
    onLaunch: (Map<String, dynamic> message) async {
      navigateToSpecificScreen(message['data']['screen']);
    },
    onResume: (Map<String, dynamic> message) async {
      navigateToSpecificScreen(message['data']['screen']);
    },
  );

but it dosen’t exist anymore !
like told here :
https://firebase.flutter.dev/docs/migration/#messaging

it is said that this is replaced by the onMessageOpenedApp.listen() method but i also read that this last method ONLY works when app is in background and not when the app is killed.

So How to achieve that ?
What is the replacement of .configure() > .onLaunch() method in newest versions of the package ?
I just couldn’t find it anywhere 🙁

Thanks in advance for your help !

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Elemental, Indeed i did some tests and here is my conclusion :

    FirebaseMessaging.onMessageOpenedApp.listen((message)){} == "OLD" onResume()
    

    ONLY activated when app is launched and/or in background and notification clicked

    FirebaseMessaging.instance.getInitialMessage() == "OLD" onLaunch()
    

    ONLY works when app was killed ! and notification clicked

    So i have to implement both of them in order to have full behavior of my notifications on click redirections.

    Am i right ? ^^


  2. Simply put that is how the listen function works on firebase (it only works if the app is open and in background).

    For the case when the app is launched from the notifications you can do this:
    On every start up you can check
    FirebaseMessaging.instance.getInitialMessage();
    which will return your notification (or null if the app was started from the OS directly)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search