Flutter-在收到Firebase通知后重定向

时间:2019-04-25 11:58:27

标签: firebase flutter firebase-cloud-messaging

我的Firebase配置方法具有fcm_service类(此服务不是小部件):

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();
});

我想在“ onResume”的主页上重定向,但是什么也没有发生。当我按通知时,将调用onResume(打印有效)。

我一直在尝试的东西:

  • 像这样调用我的页面:new MainPage();

  • 在fcm_service类中设置父窗口小部件的上下文,并像上面的代码一样使用导航器。

是否有可能通过不是窗口小部件的此类进行重定向?

编辑:

这是我的主要班级:

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,
    ));
  }
}

这是我在fcm_service中的上下文设置器

  setContext(BuildContext c) {
    this.context = c;
  }

1 个答案:

答案 0 :(得分:1)

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

只要您将BuildContext作为上下文,我会的。您可以像这样推送新的小部件

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

在实例化类时,您是否尝试过将BuildContext传递给您的FirebaseMessaging类?这样您就可以推送新的小部件?

Here the Navigation cookbook ref

不确定是否对您有帮助。