Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to navigate when click background notification on Flutter?

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 from home page to splash 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());
}
like image 521
BIS Tech Avatar asked Mar 08 '20 15:03

BIS Tech


People also ask

How do you navigate screen on notification click in flutter?

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.

How do you handle background notifications in flutter?

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.

How do you handle local notification click in flutter?

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.

How do you handle notifications when an app is in foreground in Firebase flutter?

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.


2 Answers

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

like image 197
Trong Luong Avatar answered Oct 22 '22 08:10

Trong Luong


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
});
like image 3
Santiago Racca Avatar answered Oct 22 '22 08:10

Santiago Racca