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());
}
Step-1: pass one key-value pair in the firebase notification as click_action: FLUTTER_CLICK_ACTION. Step-2: Using step 1 you'll receive the onTap callback of notification inside onResume or onLaunch method. Step-3: Perform the following scenario for navigating to a specific screen on click of notification.
It offers a bunch of features like for example scheduling when notifications should appear, periodically show a notification (interval-based), handle when a user has tapped on a notification when the app is in the foreground, background, or terminated, specify a custom notification sound and much more.
In Flutter, it's best practice to seclude your logic from your UI. To do this, we'll create a class called NotificationService in the notification_service. dart file. This class will handle all the notification logic and expose methods to create, send, schedule, and cancel notifications.
Foreground and Notification messages Notification messages which arrive while the application is in the foreground will not display a visible notification by default, on both Android and iOS. It is, however, possible to override this behavior: On Android, you must create a "High Priority" notification channel.
You can use this function to navigation when the app is killed and receive notifications in the background
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
print("FirebaseMessaging.getInitialMessage");
if (message != null) {
Navigator.of(context).pushNamed('/call');
}
});
This function only run once when the app open and get the last message.
You can read more at this doc: https://github.com/FirebaseExtended/flutterfire/blob/62e09975f9b3d14141585e62ddab6b98e667b7cf/docs/messaging/notifications.mdx
To handle notifications coming from a background state you can use the stream exposed by the FirebaseMessaging library.
FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) {
// Handle navigation or perform any logic here
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With