Firebase OnMessage函数刷新小部件

时间:2019-10-02 18:56:27

标签: flutter firebase-cloud-messaging chat

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

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

_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函数的页面不同,刷新聊天页面的最佳方法是什么?

1 个答案:

答案 0 :(得分:4)

对于以下关于StackOverflow的不同问题,我使用了以下代码。但是这里的问题与您的问题完全不同,因此请粘贴相关代码。

您可以在这里使用BLOC。 FCM / NotificationService将向BLOC / NotificationsBloc发送通知,所有需要通知的小部件都可以订阅通知。实施示例

BLOC

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

希望这就是您想要的。