You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何配置最新版Firebase Messaging?旧代码报错求助

Fix: The method 'configure' isn't defined for the type 'FirebaseMessaging'

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_notifications to display it manually.
  • Double-check your pubspec.yaml has the latest versions of firebase_core and firebase_messaging.

内容的提问来源于stack exchange,提问作者user14707939

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:31:48