简体   繁体   中英

Flutter - Redirect after getting firebase notification

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;
  }
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM