Flutter 2.0 - The method 'configure' isn't defined for the type 'FirebaseMessaging'
Asked Answered
P

2

5

I am a newbie to flutter. I just upgraded a project built for me to flutter 2.0 and I am not certain how to resolve this particular issue. I have googled and found examples. I understand there is improvement here which potentially resolves one of the issues my app was facing. But I do not know enough to know how to adjust this particular section of code because it is more complicated than any examples I can find online. Hoping someone can point me in the right direction.

void initFCMNotification() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        mapEntry = message;
        String type =
            message['Notificationtype'] ?? message['data']['Notificationtype'];
        print("notification onMessage $type");

        if (type == '5' || type == '11' || type == '3') {
          Function reload = context.read<EventsProvider>().reload;
          Future.delayed(Duration(seconds: 1), () {
            reload(true);
          });
        } else if (type == '4') {
          var notification = getNotification(message);
          nav.currentState.push(MaterialPageRoute(
              builder: (context) => MeetAgainScreen(notification)));
        }
        if (type != '11') {
          if (Platform.isIOS) {
            notiType = message['Notificationtype'];
            print('isIOS on message ${message['aps']['alert']['title']}');
            if (type == "0") {
              reloadData(type, message);
            } else {
              showSilentNotification(flutterLocalNotificationsPlugin,
                  title:
                      "${message['title'] ?? message['aps']['alert']['title'] ?? message['notification']['title'] ?? ''}",
                  payload: "${message['Notificationtype'] ?? ''}",
                  body:
                      "${message['body'] ?? message['aps']['alert']['body'] ?? message['notification']['body'] ?? ''}",
                  id: 1);
            }
          } else if (Platform.isAndroid) {
            notiType = message['data']['Notificationtype'];

            print('Android on message /*${message['data']['body']}*/');
            if (type == "0") {
              reloadData(type, message);
            } else {
              showSilentNotification(flutterLocalNotificationsPlugin,
                  title:
                      "${message['data']['title'] ?? message['notification']['title'] ?? ''}",
                  payload: "${message['data']['Notificationtype'] ?? ''}",
                  body:
                      "${message['data']['body'] ?? message['notification']['body'] ?? ''}",
                  id: 1);
            }
          }
        }
Padriac answered 12/3, 2021 at 6:27 Comment(0)
P
8

Start from version 8.0.0-dev.1, configure() has been removed in favor of calling specific static methods which return Streams.

You should use FirebaseMessaging.onMessage(),FirebaseMessaging.onMessageOpenedApp() instead.

Here the example

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        ...
});

You can find more on this pub's changelog.

Perforation answered 12/3, 2021 at 6:45 Comment(3)
Sorry, my mistake. I probly wasn't clear enough. I know what the new call is that is readily available online. I was hoping to find direction on what code to remove/replace.Padriac
_firebaseMessaging.configure( onMessage: (Map<String, dynamic> message) this line should be replaced with the answer I provided.Perforation
I ended up abandoning the code and starting the project over from scratch as this was only one of many issues. But thanks for the suggestions anyways.Padriac
P
1

In newer version of firebase messaging you have to do like this

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
        if (Platform.isAndroid)
          showNotification(
              message.notification.title,
              message.notification.body,
              message.messageId);
        else
          showNotification(message.notification.title,
              message.notification.body,
              message.messageId);
      },);
Pingpingpong answered 6/10, 2021 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.