50

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'. 

8 Answers8

116

FirebaseMessaging.configure() was removed by the firebase team:, per https://firebase.flutter.dev/docs/migration/#messaging

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));
    });
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
34

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));
    });
  }
takinok
  • 521
  • 4
  • 12
25

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);
    });
Fransiska
  • 429
  • 5
  • 8
  • Perfect explaination for old users! Now I got the onMessageOpenedApp – Rajesh Jan 25 '22 at 18:37
  • thankyou, I can't find this information anywhere – Muklas Apr 12 '22 at 06:11
  • Please after printing `message.data` it gives me an empty map, I don't know why – Samuel May 10 '22 at 14:33
  • @Samuel are you sending the data inside the [payload notification](https://firebase.google.com/docs/cloud-messaging/concept-options#notification-messages-with-optional-data-payload)? Try printing message.notification to validate if you are handling the notification – luishromero May 21 '22 at 03:14
  • Please , getInitialMessage return null message when I click on the notification when app is closed , do you know why ? – Ahmed Elsayed Jun 20 '22 at 12:39
2

After some tests, here are my conclusions :

FirebaseMessaging.onMessageOpenedApp.listen((message)){} == "OLD" onResume() ONLY activated when app is launched and/or in background and notification click

FirebaseMessaging.instance.getInitialMessage() == "OLD" onLaunch() ONLY works when app was killed ! and notification clicked

Gob
  • 77
  • 7
0

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));
    });
Bilal Saeed
  • 2,092
  • 8
  • 31
0

Event handling:

Event handling has been reworked to provide a more intuitive API for developers. Foreground-based events can now be accessed via Streams:

FirebaseMessaging.onMessage Returns a Stream called when an incoming FCM payload is received whilst the Flutter instance is in the foreground, containing a [RemoteMessage].

FirebaseMessaging.onMessageOpenedApp Returns a [Stream] that is called when a user presses a notification displayed via FCM. This replaces the previous onLaunch and onResume handlers.

FirebaseMessaging.onBackgroundMessage() Sets a background message handler to trigger when the app is in the background or terminated. IosNotificationSettings:

kavi raghul
  • 101
  • 1
  • 3
0

i know the changes about the .configure() onlaunch/onresume methods. they were replaced by

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

but this ONLY works when the app is still in background ! it won't work if the app was killed. how to achieve this ? What is the "new" equivalent for onLaunch() method please ?

Gob
  • 77
  • 7
  • You need to create a [new question](https://stackoverflow.com/questions/ask). If you are afraid that it will be considered as duplicate, then kindly explain the difference along with the link of that question. – Mearaj Jun 23 '23 at 20:46
  • 1
    i already did it here : https://stackoverflow.com/questions/76522968/firebase-messaging-onmessageopenedapp-listen-doesnt-work-when-app-killed thanks ^^ – Gob Jun 26 '23 at 11:26
0

Yes i did all that ^^ And everything works BUT the redirection on a Notification clic when the app was killed. I do assure that the onMessageOpenedApp.listen() doesn't work when the app was killed.

The proof is that i have a screen that makes an historic of all notifications received and the one that are received in background whenn app was killed are :

  • received and shown
  • persisted and shown in the historic screen

When the app was killed and you click on a notification the onMessageOpenedApp CAN'T be fired (as onLaunch() / onResume() worked before...).

That's why i asked how to achieve this with new version of Firebase ?

Gob
  • 77
  • 7