2

I have a fcm_service class (this service is not a widget) with my firebase configure method :

firebaseMessaging.configure(onLaunch: (Map<String, dynamic> msg) {
  print("onLaunch called");
}, onResume: (Map<String, dynamic> msg) {
  print("onResume called");
  Navigator.of(context).pop();
}, onMessage: (Map<String, dynamic> msg) {
  print("onMessage called : " + msg.toString());
  Navigator.of(context).pop();
});

I want to be redirected on my main page in "onResume" but nothing happens. When I press on the notification, onResume is called (the print works).

What I've been trying :

  • Calling my page like : new MainPage();

  • Setting the context of my parent widget in my fcm_service class and use the Navigator like in the code above.

Is it even possible to be redirected through this class which is not a widget ?

EDIT :

Here is my main class :

class PreesmApp extends StatefulWidget {
   @override
   _PreesmAppState createState() => _PreesmAppState();
}

class _PreesmAppState extends State<PreesmApp>{
  AuthenticationBloc _authenticationBloc;
  final FCMService fcmService = Injector.getInjector().get<FCMService>();

  @override
  void initState() {
    _authenticationBloc = AuthenticationBloc();
    _authenticationBloc.dispatch(AppStarted());
    super.initState();
    fcmService.setContext(context);
  }

  @override
  void dispose() {
    _authenticationBloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider<AuthenticationBloc>(
        bloc: _authenticationBloc,
        child: MaterialApp(
          supportedLocales: [
            const Locale('en', 'EN'),
        const Locale('fr', 'BE')
      ],
      localizationsDelegates: [
        const DemoLocalizationsDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      localeResolutionCallback:
          (Locale locale, Iterable<Locale> supportedLocales) {
        for (Locale supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode ||
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }

        return supportedLocales.first;
      },
      debugShowCheckedModeBanner: false,
      home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
        bloc: _authenticationBloc,
        builder: (BuildContext context, AuthenticationState state) {
          if (state is AuthenticationUninitialized) {
            return SplashScreen();
          }
          if (state is AuthenticationAuthenticated) {
            return DashboardPage();
          }
          if (state is AuthenticationUnauthenticated) {
            return AuthenticationPage();
          }
          if (state is AuthenticationLoading) {
            return LoadingIndicator();
          }
        },
      ),
      routes: {
        '/login': (context) =>  AuthenticationPage(),
        '/dashboard': (context) =>  DashboardPage(),
        'menu': (context) =>  MenuPage(),
        '/kanbanBoard': (context) =>  KanbanBoardPage(),
        '/listBoard': (context) =>  ListBoardPage(),
        '/ganttBoard': (context) =>  GanttBoardPage(),
        '/preesm': (context) =>  PreesmApp(),
      },
      theme: ThemeSwitcher.of(context).themeData,
    ));
  }
}

And this is my context setter in fcm_service

  setContext(BuildContext c) {
    this.context = c;
  }
Thomas Nicole
  • 737
  • 1
  • 9
  • 22

1 Answers1

2
Is it even possible to be redirected through this class which is not a widget ?

As long as you have BuildContext as context, I'd say yes. You can push new widgets like this

Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),

Have you tried to pass BuildContext along to your, let's say FirebaseMessaging class, when instantiating it? So that you can push new Widgets?

Here the Navigation cookbook ref

Not sure if it helps you tho.

Victor Hugo Montes
  • 1,270
  • 1
  • 17
  • 28
  • 2
    Thanks for you answer. The way to use the Navigator is not the problem, I tried your code but nothing changes. I think i tried all the Navigator possibilities btw. My FirebaseMessaging class is a dependency injection instantiated directly on launch and I set the context of my parent widget directly in his initState function – Thomas Nicole Apr 25 '19 at 15:12
  • 2
    Maybe I set the bad context. I have many pages but they are all children of my main page, and I use the context of this main page – Thomas Nicole Apr 25 '19 at 15:25
  • 2
    I see what you mean. Can I see your **main()**? Another option would be go with a **heavier** strategy by using **GlobalKeys** https://stackoverflow.com/a/50592224/3353024 – Victor Hugo Montes Apr 25 '19 at 15:38
  • 2
    I have understood my problem by looking to your link with gloabal keys! I was setting my context in the initState() of my main app but I think my context was not build (not sure). So, i copy/paste my setContext everywhere in my main app to try and it is working. Thank you for your help ! – Thomas Nicole Apr 26 '19 at 07:22