Flutterfire: [firebase_mesaging] Correct place for FCM initialisation

Created on 8 Nov 2019  路  1Comment  路  Source: FirebaseExtended/flutterfire

My app has next routing:

return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'App',
      theme: ThemeUtil.instance.appTheme,
      routes: {
        Routes.onboarding: (_) => OnboardingPage(),
        Routes.login: (_) => LoginPage(),
        Routes.signUp: (_) => SignUpPage(),
        Routes.homePage: (_) => HomePage(),
        Routes.inbox: (_) => ConvoListPage(),
      },
      initialRoute: (isOnboarding) ? Routes.onboarding : Routes.homePage,
    );

HomePage has 5 tabs (pages) and this is a main apps part. Pushes should redirect user to inbox.

The question is where should be configured FCM?
When it is initiated in Home page it catch push and redirect user to Inbox, but when user should be returned to one of tabs of Home page, it is initiated again and makes a loop (user went to Home and onResume / onLaunch redirects to Inbox again and again).
I can't initiate FCM on higher level because in the routing isn't available there.

bug

Most helpful comment

Actually, you can initiate FCM on same level as you are initiating Material app.
All you need to do is to add navigatorKey: navigatorKey parameter to MaterialApp.

Here is example of declaration:
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

Then once you get push notification just refer to navigatorKey and push Route something like this:
navigatorKey.currentState.pushNamed

>All comments

Actually, you can initiate FCM on same level as you are initiating Material app.
All you need to do is to add navigatorKey: navigatorKey parameter to MaterialApp.

Here is example of declaration:
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

Then once you get push notification just refer to navigatorKey and push Route something like this:
navigatorKey.currentState.pushNamed

Was this page helpful?
0 / 5 - 0 ratings