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

4,665

Solution 1

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.

Solution 2

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);
      },);
Share:
4,665
BigPictureDeepDive
Author by

BigPictureDeepDive

Updated on December 28, 2022

Comments

  • BigPictureDeepDive
    BigPictureDeepDive over 1 year

    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);
                }
              }
            }