Prevent main.dart on FCM onLaunch

Viewed 151

Is there a proper way/flutter-firebase guidelines to prevent a flutter app to re-start from main.dart when loading from onLaunch() of FCM firebase notification .

I can see ,onLaunch being triggered but soon main.dart overrides it .

_fcm.configure(
  // Called when the app is in the foreground and we receive a push notification
  onMessage: (Map<String, dynamic> message) async {
    print('onMessage: $message');
  },
  // Called when the app has been closed comlpetely and it's opened
  // from the push notification.
  onLaunch: (Map<String, dynamic> message) async {
    print('onLaunch: $message');
    _serialiseAndNavigate(message); //THIS WORKS , BUT SUDDENLY main.dart KICKS IN .
  },
  // Called when the app is in the background and it's opened
  // from the push notification.
  onResume: (Map<String, dynamic> message) async {
    print('onResume: $message');
    _serialiseAndNavigate(message);
  },
);
1 Answers

When you click on the FCM notification, app is likely to be launch from onLaunch method and it does invoke the main.dart. I would suggest please try to inject fcm.configure() not in main.dart, instead you can inject in whatever stateful widget/screen you want to invoke the screen directly to launch when notification gets clicked (onLaunch).

ex:-> main.dart -> homescreen.dart -> somescreen -> forever flow

if you want to invoke onLaunch to open homescreen.dart, run the fcm.configure() to homescreen.dart -> initState

Remember it will also run main.dart at first as flutter starts the flow of the app from main.dart itself, you cannot change it I guess, but on notification tap it will not open main.dart, instead it will launch homescreen.dart.

Related