关于flutter:Firebase OnMessage函数刷新小部件

Firebase OnMessage function refreshes widget

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
_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函数所在的页面不同,刷新聊天页面的最佳方法是什么?


我在StackOverflow上使用了以下代码来回答不同的问题。 但是那里的问题与您的问题完全不同,因此请粘贴相关代码。

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

集团

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
  }
}

最后在您的小部件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  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();
  }

希望这是您想要的。