如何实现 Firebase 消息传递颤振

时间:2021-05-11 20:49:26

标签: flutter firebase-cloud-messaging

我已将 Firebase 消息传递更新为最新版本,但现在我无法掌握如何实施新修改。

到目前为止我已经使用过:

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

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

这是我的原始代码

    class PushNotificationService{
  final FirebaseMessaging fcm = FirebaseMessaging();

  Future initialize(context) async {

    if(Platform.isIOS){
      fcm.requestNotificationPermissions(IosNotificationSettings());
    }

    fcm.configure(

      onMessage: (Map<String, dynamic> message) async {

        fetchRideInfo(getRideID(message), context);

      },
      onLaunch: (Map<String, dynamic> message) async {

        fetchRideInfo(getRideID(message), context);

      },
      onResume: (Map<String, dynamic> message) async {

        fetchRideInfo(getRideID(message), context);
      },

    );

  }

似乎无法找到实现它的方法。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您应该执行以下操作:

  • 更改 initialize 方法的主体以包含 FirebaseMessaging.onMessageFirebaseMessaging.onMessageOpenedApp 侦听器
  • 使用 FirebaseMessaging.instance.getInitialMessage() 获取初始消息并在消息不为空时运行 onLaunch 回调(即应用在终止时从通知启动)
    Future initialize(context) async {
    
       if(Platform.isIOS){
         fcm.requestNotificationPermissions(IosNotificationSettings());
       }

       //onMessage
       FirebaseMessaging.onMessage.listen((RemoteMessage message) {
         fetchRideInfo(getRideID(message), context);
       });

       //onResume
       FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
         fetchRideInfo(getRideID(message), context);
       });

       //onLaunch
       final Remote initialMessage = await FirebaseMessaging.instance.getInitialMessage();
       if (initialMessage != null) {
         fetchRideInfo(getRideID(initialMessage), context);
       }
      
    }