4

I have upgraded firebase from ^7.0.0 to ^8.0.0-dev.15 and now I am getting erros, that I am not sure how to fix.

Here is my legacy working code:

void initFirebase() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {});
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      setState(() {
        _notificationToken = token;
      });
    });
  }

I am getting the following errors:

lib/files/signin.dart:52:7: Error: No named parameter with the name 'onLaunch'.
      onLaunch: (Map<String, dynamic> message) async {
      ^^^^^^^^
lib/files/signin.dart:62:24: Error: The getter 'onIosSettingsRegistered' isn't defined for the class 'FirebaseMessaging'.
 - 'FirebaseMessaging' is from 'package:firebase_messaging/firebase_messaging.dart' ('/C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-8.0.0-dev.15/lib/firebase_messaging.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'onIosSettingsRegistered'.
    _firebaseMessaging.onIosSettingsRegistered
                       ^^^^^^^^^^^^^^^^^^^^^^^

Now, I started fixing the code as per the official documentation but cannot get it entirely right, will appreciate your help:

  void initFirebase() {
    FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) {
        print("onMessage: $message");
      });
      FirebaseMessaging.onMessageOpenedApp.listen(
      (RemoteMessage message) {
        print("onMessageOpenedApp: $message");
      });
    
    NotificationSettings settings = messaging.requestPermission (
        sound: true, badge: true, alert: true, provisional: true,
    );
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {});
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      setState(() {
        _notificationToken = token;
      });
    });
  }
BradG
  • 673
  • 1
  • 14
  • 26

4 Answers4

2

If you're using firebase_messaging: ^8.0.0-dev.15, you should also add with it firebase_core:^0.7.0. And there's no "OnResume", "onLaunch", "onMessage", there are other alternatives. Here's how to use it in this case as refered to in this answer. For more, you can check official doc

Fahima Mokhtari
  • 1,691
  • 2
  • 16
  • 30
1

Sorry I answered earlier with another question. Besides, I use another major version of flutter and firebase.

So I deleted my answer and opened a new post here: Flutter 2.0 with Firebase Cloud Messaging: onMessage not called on Android

-- EDIT

Does this bug relate to your problem? https://github.com/FirebaseExtended/flutterfire/issues/4054

Regards,

Uwe

Uwinator
  • 141
  • 1
  • 9
  • Uwinator, thanks for the answer. No this post is not related to my issue. My issue is described here: https://github.com/FirebaseExtended/flutterfire/issues/260. A possible solution is described in another post here: https://github.com/FirebaseExtended/flutterfire/discussions/4023. The problem is that the upgrade guides are not very clear and I cannot properly update my code. I was hoping for someone with more experience in flutterfire to be able to provide a solution. – BradG Mar 21 '21 at 22:15
0

This legacy upgrade had some breaking changes. I recreated your problem. The concepts of onresume, onlaunch etc has been reclassified into foreground and background messages.

To handle background messages, at a top level class like you main.dart file, instantiate firebase messaging.

Future<void>main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());

}

Create a method (_firebaseMessagingBackgroundHandler) in this case. (If you're going to use other Firebase services in the background, such as Firestore, don't forget to call initializeApp before using other Firebase services.).

Background

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print("Handling a background message: ${message.messageId}");
  print(message.data);
}

Foreground notifications are slightly different. By default they are not popped on the screen. You can tap into foreground notifications using the method below


FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  print("Handling a foreground message ${message.notification.body}");
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,
            icon: android?.smallIcon,
            // other properties...
          ),
        ));
  }
});

You could tap into the message using JSON formatting.

onLaunch: and onResume:

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  print("app launched or resumed!!");
});

The two codes above will display notifications respectively. The background handler also covers for the scenario of a terminated app.

The FirebaseMessaging.onMessageOpenedApp returns a [Stream] that is called when a user presses a notification displayed via FCM.

PS: In the use of Non-Google servers like PostMan, another breaking change is the requirement of an Oauth-2 Bearer token (short lived, and can be gotten from https://developers.google.com/oauthplayground with a service account.

seanFlutter
  • 1
  • 1
  • 1
0

For your question about-->Error: No named parameter with the name 'onLaunch'. onLaunch: (Map<String, dynamic> message) async { ^^^^^^^^. What you need to do is replace your code at the _firebaseMessaging.configure(); part to this code:

FirebaseMessaging.onMessage.listen(
   (RemoteMessage message) async {
      print('onMessage: $message');
      _setMessage(message.data);
   },
);
FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) async {
    print('onLaunch: $message');
    _setMessage(message.data);
  },
);
FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) async {
    print('onResume: $message');
    _setMessage(message.data);
  },
);
Yuri Haruno
  • 111
  • 1
  • 7