【问题标题】:How to configure Firebase Messaging with latest version in Flutter?如何在 Flutter 中使用最新版本配置 Firebase 消息传递?
【发布时间】:2021-03-11 08:29:55
【问题描述】:

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

代码行

 messaging.configure(onMessage: (Map<String, dynamic> message){}

DART 分析中的错误

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

【问题讨论】:

  • 能否请您发布您用于配置 Firebase 消息传递的整个代码。
  • 您使用的是哪个版本的 Firebase 消息传递?
  • 最新的`firebase_messaging ^8.0.0-dev.10`来自flutterfire链接firebase.flutter.dev/docs/migration

标签: firebase flutter dart firebase-cloud-messaging


【解决方案1】:

请检查以下示例。

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

【讨论】:

    【解决方案2】:

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

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

    使用FirebaseMessaging.onMessage 方法获取消息

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          RemoteNotification notification = message.notification;
          AndroidNotification android = message.notification?.android;
        });
    

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

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

    【讨论】:

    • 我搜索了 2 小时的明确解释,并在此处找到它而不是文档和更改日志
    • 谢谢。这个答案应该被标记为选中的答案。
    • 但这会自动在 iOS 中显示推送通知,即使您想在某些情况下避免。请指导
    • 在每次用户启动应用程序时,我们必须将这段代码放在哪里才能运行?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2023-03-23
      • 2021-10-20
      • 2018-09-07
      • 2021-09-04
      • 2020-07-09
      • 2021-07-22
      • 1970-01-01
      • 2021-02-19
      相关资源
      最近更新 更多