Flutter中如何配置最新版Firebase Messaging?旧代码报错求助
Hey there! I bumped into this exact problem when upgrading Firebase Messaging to the latest version—totally get how frustrating it is when old code breaks after an update. The configure() method was deprecated and removed in firebase_messaging v10.0.0+; the team switched to a more stream-based approach for handling notifications. Here's how to rewrite your code to match the new API:
1. Replace onMessage (Foreground Notifications)
Instead of passing an onMessage callback to configure(), use the onMessage stream listener:
// Listen for messages when the app is in the foreground FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Received foreground message: ${message.messageId}'); // Access message data and notification content final messageData = message.data; final notification = message.notification; // If you want to show a system notification in the foreground, // you'll need to use a package like flutter_local_notifications here if (notification != null) { print('Notification title: ${notification.title}, body: ${notification.body}'); } });
2. Replace Background Message Handling (Previously onBackgroundMessage)
For messages received when the app is in the background or terminated, you need a top-level/global function (can't be a class method) and register it with onBackgroundMessage:
// Global background message handler (must be top-level) Future<void> _firebaseBackgroundMessageHandler(RemoteMessage message) async { // Make sure to initialize Firebase if needed await Firebase.initializeApp(); print('Handled background message: ${message.messageId}'); } void main() async { WidgetsFlutterBinding.ensureInitialized(); // Initialize Firebase first await Firebase.initializeApp(); // Register the background handler FirebaseMessaging.onBackgroundMessage(_firebaseBackgroundMessageHandler); runApp(MyApp()); }
3. Handle App Open from Terminated/Background State
Previously covered by onLaunch and onResume, now split into two parts:
- Check for initial messages when the app starts from terminated state:
// In your initState or main setup RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage(); if (initialMessage != null) { // Navigate to a specific screen or handle the message print('App opened from terminated state with message: ${initialMessage.data}'); } - Listen for messages when opening the app from the background:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { print('App opened from background with message: ${message.data}'); // Add your navigation logic here });
Key Notes
- Always make sure to call
Firebase.initializeApp()before setting up any Firebase Messaging listeners. - For foreground notifications, Firebase won't show a system notification by default—you'll need to use a package like
flutter_local_notificationsto display it manually. - Double-check your
pubspec.yamlhas the latest versions offirebase_coreandfirebase_messaging.
内容的提问来源于stack exchange,提问作者user14707939

