单击 时是否可以导航到指定路径背景 FCM 通知?
我创建了一个 顶级功能并将其添加到导航器路径但它不起作用,当单击后台通知时,它只是打开应用程序
我想我发现了一个问题
Now, I changed
fcm
configuration fromhome page
tosplash screen
. The foreground doesn't navigate to the page, I think its because the Splash Screen is no longer available. When I click on the notification message, it just opens the app.
FCM 配置
onBackgroundMessage: backgroundMessageHandler
顶级功能Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
getIt<NavigationService>().navigateTo('/${message['data']['screen']}');
}
}
有效载荷 const payload: admin.messaging.MessagingPayload = {
notification:{
title: `New Enquiry`,
body:`${customerName} published to ${subName}`,
badge: '1',
sound: 'default'
},
data: {
click_action: `FLUTTER_NOTIFICATION_CLICK`,
sound: `default`,
status: `chat`,
screen: `homePage`
}
}
main.dart GetIt getIt = GetIt.instance;
void main() {
setupLocator();
runApp(MyApp());
}
MaterialApp return MaterialApp(
navigatorKey: NavigationService().navigatorKey,
onGenerateRoute: Router.generateRoute,
);
导航服务和 设置定位器 class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName) {
return navigatorKey.currentState.pushNamed(routeName);
}
}
void setupLocator() {
getIt.registerLazySingleton(() => NavigationService());
}
最佳答案
要处理来自后台状态的通知,您可以使用 FirebaseMessaging 库公开的流。
FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) {
// Handle navigation or perform any logic here
});
关于firebase - 在 Flutter 上单击背景通知时如何导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589234/