dart 如何在Flutter中使用最新版本配置Firebase消息传递?

pb3skfrl  于 2022-12-25  发布在  Flutter
关注(0)|答案(5)|浏览(119)

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

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

DART分析中的错误

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

1wnzp6jl1#

Firebase团队删除了Firebase消息传递.configure():
原因:configure()的以前实现在多次调用时(注册不同的处理程序或删除处理程序)会导致意外的副作用。此更改允许开发人员更明确地注册和删除处理程序,而不会通过流影响其他人。

使用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));
    });
rbpvctlc

rbpvctlc2#

请检查以下示例。
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));
    });
  }
nafvub8i

nafvub8i3#

根据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);
    });
goqiplq2

goqiplq24#

更新一个:
***FirebaseMessagingService***对于在应用程序开始时就开始使用非常重要。为此,您需要遵循以下操作:首先声明此函数:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  debugPrint("Firebase Messaging firebase is initialized");
  await Firebase.initializeApp();
}

并在app的main(){}中调用此函数:

FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

然后,您将能够使用以下功能:

FirebaseMessaging.onMessage方法获取消息

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

FirebaseMessaging.onMessageOpenedApp作为onLaunch和onResume处理程序的替代。

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

nx7onnlm5#

事件处理:

事件处理经过重新设计,为开发人员提供了更直观的API。基于前台的事件现在可以通过流访问:

FirebaseMessaging.onMessage当Flutter示例在前台时收到传入FCM有效载荷时,返回调用的Stream,其中包含[RemoteMessage]。
FirebaseMessaging.onMessageOpenedApp返回用户按下通过FCM显示的通知时调用的[Stream]。这将替换以前的onLaunch和onResume处理程序。
**FirebaseMessaging.onBackgroundMessage()**设置应用在后台或终止时触发的后台消息处理程序。IosNotificationSettings:

相关问题