Is it possible to navigate to a specified path when clicking background FCM notification?
I created a Top-Level function and add it to the navigator path but its not working, when clicking on a background notification, it just opens the app
I GUESS I FOUND AN ISSUE
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 Configuration
onBackgroundMessage: backgroundMessageHandler
Top-Level function
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
getIt<NavigationService>().navigateTo('/${message['data']['screen']}');
}
}
Payload
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,
);
NavigationService and setupLocator
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName) {
return navigatorKey.currentState.pushNamed(routeName);
}
}
void setupLocator() {
getIt.registerLazySingleton(() => NavigationService());
}