我有一个带有我的 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):
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 中的上下文 setter
setContext(BuildContext c) {
this.context = c;
}
最佳答案
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
不确定它是否对您有帮助。
关于firebase - Flutter - 收到 firebase 通知后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55848807/