Firebase OnMessage 函数刷新小部件

Firebase OnMessage function refreshes widget

我正在使用 firebase 云消息传递在我的包含聊天页面的应用程序上推送通知。

我在 main.dart 上定义了我的 firebase 推送函数,如下所示:

_firebaseMessaging.configure(
   onMessage: (Map<String, dynamic> message) async {
     print("onMessage: $message");
     //_showItemDialog(message);
   },
   onBackgroundMessage: myBackgroundMessageHandler,
   onLaunch: (Map<String, dynamic> message) async {
     print("onLaunch: $message");
     //_navigateToItemDetail(message);
   },
   onResume: (Map<String, dynamic> message) async {
     print("onResume: $message");
     //_navigateToItemDetail(message);
   },
 );

当聊天小部件打开并且我收到推送通知时,我的 OnMessage 方法正常到达。

问题是:考虑到打开的页面与声明达到的 OnMessage 函数的页面不同,刷新聊天页面的最佳方式是什么?

我在 Whosebug 上使用了以下代码来解决不同的问题。但是那里的问题和你的完全不一样,贴出相关代码。

你可以在这里使用BLOC。 FCM/NotificationService会向BLOC/NotificationsBloc发送通知,所有需要通知的widget都可以订阅通知。实施示例

集团

import 'package:rxdart/rxdart.dart';

class LocalNotification {
  final String type;
  final Map data;

  LocalNotification(this.type, this.data);
}

class NotificationsBloc {
  NotificationsBloc._internal();

  static final NotificationsBloc instance = NotificationsBloc._internal();

  final BehaviorSubject<LocalNotification> _notificationsStreamController = BehaviorSubject<LocalNotification>();

  Stream<LocalNotification> get notificationsStream {
    return _notificationsStreamController;
  }

  void newNotification(LocalNotification notification) {
    _notificationsStreamController.sink.add(notification);
  }

  void dispose() {
    _notificationsStreamController?.close();
  }
}

FCM 侦听器(NotificationService)

import 'package:firebase_messaging/firebase_messaging.dart';

import 'notifications_bloc.dart';

class LocalNotificationService {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  bool _started = false;

  LocalNotificationService._internal();

  static final LocalNotificationService instance = LocalNotificationService._internal();

  // ********************************************************* //
  // YOU HAVE TO CALL THIS FROM SOMEWHERE (May be main widget)
  // ********************************************************* //
  void start() {
    if (!_started) {
      _start();
      _started = true;
      _refreshToken();
    }
  }

  void _refreshToken() {
    _firebaseMessaging.getToken().then(_tokenRefresh, onError: _tokenRefreshFailure);
  }

  void _start() {
    _firebaseMessaging.requestNotificationPermissions();
    _firebaseMessaging.onTokenRefresh.listen(_tokenRefresh, onError: _tokenRefreshFailure);
    _firebaseMessaging.configure(
      onMessage: _onMessage,
      onLaunch: _onLaunch,
      onResume: _onResume,
    );
  }

  void _tokenRefresh(String newToken) async {
    print(" New FCM Token $newToken");
  }

  void _tokenRefreshFailure(error) {
    print("FCM token refresh failed with error $error");
  }

  Future<void> _onMessage(Map<String, dynamic> message) async {
    print("onMessage $message");
    if (message['notification'] != null) {
      final notification = LocalNotification("notification", message['notification'] as Map);
      NotificationsBloc.instance.newNotification(notification);
      return null;
    }
    if (message['data'] != null) {
      final notification = LocalNotification("data", message['data'] as Map);
      NotificationsBloc.instance.newNotification(notification);
      return null;
    }
  }

  Future<void> _onLaunch(Map<String, dynamic> message) {
    print("onLaunch $message");
    return null;
  }

  Future<void> _onResume(Map<String, dynamic> message) {
    print("onResume $message");
    return null;
  }
}

终于在您的小部件中了

  Stream<LocalNotification> _notificationsStream;

  @override 
  void initState() {
    super.initState();
    _notificationsStream = NotificationsBloc.instance.notificationsStream;
    _notificationsStream.listen((notification) {
      // TODO: Implement your logic here
      print('Notification: $notification');
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

希望这就是您要找的。