【问题标题】:Flutter - Redirect after getting firebase notificationFlutter - 收到firebase通知后重定向
【发布时间】:2019-09-14 20:35:24
【问题描述】:

我的 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;
  }

【问题讨论】:

    标签: firebase flutter firebase-cloud-messaging


    【解决方案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

    不知道对你有没有帮助。

    【讨论】:

    • 感谢您的回答。使用导航器的方式不是问题,我尝试了您的代码但没有任何变化。我想我尝试了所有导航器的可能性。我的 FirebaseMessaging 类是在启动时直接实例化的依赖注入,我直接在他的 initState 函数中设置父小部件的上下文
    • 也许我设置了错误的上下文。我有很多页面,但它们都是我的主页的子页面,我使用这个主页的上下文
    • 我明白你的意思。我可以看看你的 main() 吗?另一种选择是使用 GlobalKeys stackoverflow.com/a/50592224/3353024 采用 heavier 策略
    • 通过查看您的全局键链接,我了解了我的问题!我在我的主应用程序的 initState() 中设置了我的上下文,但我认为我的上下文没有构建(不确定)。所以,我在我的主应用程序的任何地方复制/粘贴我的 setContext 来尝试,它正在工作。感谢您的帮助!
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多