【问题标题】:Flutter Updated Listview from FCM Push Notifications?Flutter 从 FCM 推送通知更新列表视图?
【发布时间】:2021-05-18 05:47:49
【问题描述】:

到目前为止,我能够从 Firebase Cloud Messaging 检索单个推送通知并将其显示在屏幕上。但我想将所有推送通知保存为列表视图。因此,每次收到新的推送通知时,都会重新构建列表视图,并在顶部使用最新的推送通知进行更新。理想情况下,用户应该能够向后滚动并查看他们曾经收到的所有推送通知。

这可能吗?这是显示单个通知的代码;

import 'package:flutter/material.dart';
import 'package:fcm_config/fcm_config.dart';

Future<void> _firebaseMessagingBackgroundHandler(
    RemoteMessage _notification) async {

  String title = _notification.data["title_key"];
  String body = _notification.data["body_key"];
  FCMConfig.displayNotification(title: title, body: body);
}

void main() async {   FCMConfig.init(onBackgroundMessage: _firebaseMessagingBackgroundHandler).then((value) {
    FCMConfig.subscribeToTopic("test_fcm_topic");
  });

  runApp(MyHomePage());
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with FCMNotificationMixin, FCMNotificationClickMixin {
  RemoteMessage _notification;
  final String serverToken ='myservertoken';

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ListTile(
                title: Text("title"),
                subtitle: Text(_notification?.notification?.title ?? ""),
              ),
              ListTile(
                title: Text("Body"),
                subtitle: Text(
                    _notification?.notification?.body ?? "No notification"),
              ),
              if (_notification != null)
                ListTile(
                  title: Text("data"),
                  subtitle: Text(_notification?.data?.toString() ?? ""),
                )
            ],
          ),
        ),
      ),
    );
  }

  @override
  void onNotify(RemoteMessage notification) {
    _firebaseMessagingBackgroundHandler(notification);
    setState(() {
      _notification = notification;
    });
  }

  @override
  void onClick(RemoteMessage notification) {
    setState(() {
      _notification = notification;
    });
    print(
        "Notification clicked with title: ${notification.notification.title} && body: ${notification.notification.body}");
  }
}

【问题讨论】:

    标签: flutter listview dart firebase-cloud-messaging


    【解决方案1】:

    您可以将所有通知保存在database,但我认为最好的方法是您必须将所有通知保存在服务器上

    【讨论】:

    • 这是个好建议。你知道我如何获得任何新的通知来触发列表的重建吗?
    • 是的,你可以在 Flutter 中使用状态管理请参考这个链接:pub.dev/packages/provider
    • 如何将通知保存在服务器而不是数据库上?您是指 JSON 文件还是文本文件?
    • 我的意思是用node js和MongoDB创建的服务器
    • 是的,你可以做到
    【解决方案2】:

    如果你只需要刷新列表 使用

    List<RemoteMessage> _notifications=[];
    

    当任何通知到来时

    _notifications.insert(0,notification);
    

    但如果您需要保存以备后用

    在最新版本的 fcm_config 中

    fcm_config 3.0.0-nullsafety.1
    

    你可以得到json格式的通知

    _notification.toMap()
    

    然后您可以将其保存为 shared_prefernces 或任何其他方式 你可以再次解析它

    RemoteMessage.fromMap(...)
    

    【讨论】:

    • 我收到了与 fcm_config 3.0.0-nullsafety.1 相关的一整串错误因为 fcm_config >=3.0.0-nullsafety.0 取决于 flutter_local_notifications ^5.0.0-nullsafety.0 和speakoholic 依赖于 flutter_local_notifications ^4.0.1+1,fcm_config >=3.0.0-nullsafety.0 是被禁止的。所以,因为speakoholic 依赖于fcm_config ^3.0.0-nullsafety.1,版本解析失败。 pub get failed (1; 所以,因为 speakoholic 依赖于 fcm_config ^3.0.0-nullsafety.1,版本求解失败。)
    • 升级后我遇到了更多问题;因为没有任何版本的 firebase_cloud_messaging 匹配 >4.0.4+1 =5.0.0-nullsafety.0依赖于平台^3.0.0,firebase_cloud_messaging ^4.0.4+1与flutter_local_notifications >=5.0.0-nullsafety.0不兼容。所以,因为 speakoholic 依赖于 flutter_local_notifications ^5.0.0-nullsafety.0 和 firebase_cloud_messaging ^4.0.4+1,所以版本求解失败。发布失败
    • 看来这个最新的 FCM_config 与 flutter_local_notifications 不兼容。
    • 我可以使用的最新 FCM_config 版本是 ^2.0.0-dev.18。 _notification.toMap() 是否适用于这个版本?
    • fcm_onfig 现在是空安全,必须依赖空安全包 firbase_messaging:9.0.0 和 flutter_local_notifications :5
    猜你喜欢
    • 2020-04-21
    • 2019-09-18
    • 2020-10-10
    • 2020-10-14
    • 2021-06-05
    • 1970-01-01
    • 2021-11-27
    • 2021-01-12
    • 2023-02-14
    相关资源
    最近更新 更多