How to configure Firebase Messaging with latest version in Flutter?

Viewed 35964

I was using the below line of code for firebase messaging configuration for flutter noticification configuration , but now after integrating to the latest version of the firebase messaging it is giving me error

CODE LINE

 messaging.configure(onMessage: (Map<String, dynamic> message){}

ERROR in DART Analysis

error: The method 'configure' isn't defined for the type 'FirebaseMessaging'. 
4 Answers

FirebaseMessaging.configure() is removed by firebase team:

Reason: The previous implementation of configure() caused unintended side effects if called multiple times (either to register a different handler or remove handlers). This change allows developers to be more explicit about registering handlers and removing them without affecting others via Streams.

Use FirebaseMessaging.onMessage method to get messages

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
    });

Use FirebaseMessaging.onMessageOpenedApp as a replacement for onLaunch and onResume handlers.

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('A new onMessageOpenedApp event was published!');
      Navigator.pushNamed(context, '/message',
          arguments: MessageArguments(message, true));
    });

Please check following example.

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart

  @override
  void initState() {
    super.initState();
    FirebaseMessaging.instance
        .getInitialMessage()
        .then((RemoteMessage message) {
      if (message != null) {
        Navigator.pushNamed(context, '/message',
            arguments: MessageArguments(message, true));
      }
    });

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;

      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                // TODO add a proper drawable resource to android, for now using
                //      one that already exists in example app.
                icon: 'launch_background',
              ),
            ));
      }
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('A new onMessageOpenedApp event was published!');
      Navigator.pushNamed(context, '/message',
          arguments: MessageArguments(message, true));
    });
  }

Building on Jitesh's answer, for me implementation of getInitialMessage is needed to make the navigation works when the app is terminated (replacement for onLaunch)

    // workaround for onLaunch: When the app is completely closed (not in the background) and opened directly from the push notification
    FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
      print('getInitialMessage data: ${message.data}');
      _serialiseAndNavigate(message);
    });

    // onMessage: When the app is open and it receives a push notification
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print("onMessage data: ${message.data}");
    });

    // replacement for onResume: When the app is in the background and opened directly from the push notification.
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('onMessageOpenedApp data: ${message.data}');
      _serialiseAndNavigate(message);
    });

Updated One:

FirebaseMessagingService is important to get started in the beginning of app. And for that sake, you need to follow this: Declare this function first :

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  debugPrint("Firebase Messaging firebase is initialized");
  await Firebase.initializeApp();
}

And call this function in the main(){} of app:

 FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

And then you will be able to use these functions:

FirebaseMessaging.onMessage method to get messages

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
    });

FirebaseMessaging.onMessageOpenedApp as a replacement for onLaunch and onResume handlers.

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('A new onMessageOpenedApp event was published!');
      Navigator.pushNamed(context, '/message',
          arguments: MessageArguments(message, true));
    });
Related