如何在 Flutter 中使用最新版本配置 Firebase Messaging?

32 dart firebase flutter firebase-cloud-messaging

我使用下面的代码行进行颤振通知配置的 firebase 消息传递配置,但现在在集成到最新版本的 firebase 消息传递后,它给了我错误

代码行

 messaging.configure(onMessage: (Map<String, dynamic> message){}
Run Code Online (Sandbox Code Playgroud)

DART 分析中的错误

error: The method 'configure' isn't defined for the type 'FirebaseMessaging'. 
Run Code Online (Sandbox Code Playgroud)

jit*_*555 59

FirebaseMessaging.configure() 已被 firebase 团队移除:

原因:configure()如果多次调用(注册不同的处理程序或删除处理程序) ,先前的实现会导致意外的副作用。此更改允许开发人员更明确地注册和删除处理程序,而不会通过 Streams 影响其他人。

使用FirebaseMessaging.onMessage 方法获取消息

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
    });
Run Code Online (Sandbox Code Playgroud)

使用FirebaseMessaging.onMessageOpenedApp作为替代onLaunchonResume处理程序。

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('A new onMessageOpenedApp event was published!');
      Navigator.pushNamed(context, '/message',
          arguments: MessageArguments(message, true));
    });
Run Code Online (Sandbox Code Playgroud)

  • 我搜索了 2 个小时的清晰解释,并在这里找到了它,而不是文档和更改日志 (14认同)

小智 27

基于 Jitesh 的答案,对我来说,getInitialMessage需要实现 才能在应用程序终止时使导航正常工作(替换 onLaunch)

    // workaround for onLaunch: When the app is completely closed (not in the background) and opened directly from the push notification
    FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
      print('getInitialMessage data: ${message.data}');
      _serialiseAndNavigate(message);
    });

    // onMessage: When the app is open and it receives a push notification
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print("onMessage data: ${message.data}");
    });

    // replacement for onResume: When the app is in the background and opened directly from the push notification.
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('onMessageOpenedApp data: ${message.data}');
      _serialiseAndNavigate(message);
    });
Run Code Online (Sandbox Code Playgroud)


小智 22

请检查以下示例。

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart

  @override
  void initState() {
    super.initState();
    FirebaseMessaging.instance
        .getInitialMessage()
        .then((RemoteMessage message) {
      if (message != null) {
        Navigator.pushNamed(context, '/message',
            arguments: MessageArguments(message, true));
      }
    });

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      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,
                // TODO add a proper drawable resource to android, for now using
                //      one that already exists in example app.
                icon: 'launch_background',
              ),
            ));
      }
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('A new onMessageOpenedApp event was published!');
      Navigator.pushNamed(context, '/message',
          arguments: MessageArguments(message, true));
    });
  }
Run Code Online (Sandbox Code Playgroud)