flutter 如何在StreamBuilder Widget中存储从Firebase Cloud消息传递收到的通知?

cetgtptt  于 11个月前  发布在  Flutter
关注(0)|答案(1)|浏览(99)
List<String> notifications = [];
StreamBuilder<RemoteMessage>(
  stream: FirebaseMessaging.onMessage,
  builder:
      (BuildContext context, AsyncSnapshot<RemoteMessage> snapshot) {
    if (snapshot.hasData) {
      List<String> messagesShow = [];
      RemoteMessage message = snapshot.data!;
      final messageText = message.notification?.title;
      final messageBody = message.notification?.body;
      for (var message in message.data) {} //this doesnt get called?

      _messageController.add('New Message');
      notifications.add(message.toString());
      return Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('${message.notification?.title}'),
            const SizedBox(
              height: 10.0,
            ),
            Text('${message.notification?.body}'),
          ],
        ),
      );

字符串
我想从Flutter云消息存储在通知页面的所有通知,但我只能得到一个消息的时间。

tez616oj

tez616oj1#

FirebaseMessaging.onMessage流在新消息到达时触发一个事件。它保留已经到达的消息的记录,或者它们到达时的记录。
因此,您看到的是预期的行为。如果您希望拥有已接收的所有消息的列表,则必须自己创建和维护该列表-例如,通过将来自onMessage侦听器的消息存储在共享存储中。
这是一种保存应用/设备接收的消息的持久记录的好方法。但由于FCM不保证消息传递,因此它不能确保您的应用记录了服务器发送给它的所有消息。
这就是为什么例如在聊天应用程序中,这两种情况都会发生:
1.当发生有趣的事情时,服务器通过FCM向您发送消息。
1.应用程序启动时还从服务器/数据库中检索所有相关消息。
这两个操作确保应用程序具有完整的数据,并且在应用程序未使用时获取数据,以便您可以向用户显示通知。

相关问题