【问题标题】:Save and Retrieve notification with shared preferences list flutter使用共享首选项列表颤动保存和检索通知
【发布时间】:2020-07-08 04:45:08
【问题描述】:

我在我的颤振应用程序中实现了Firebase Cloud Messaging。一切正常,但我想用shared preferences 在本地存储所有消息列表并在另一个屏幕中检索它们。我的所有逻辑都无法正常工作,因为在调用 onMessage 函数时我无法保存消息。

PushNotificationService

class PushNotificationService {
  final FirebaseMessaging _fcm = FirebaseMessaging();
  List<String> titles;
  List<String> msgs;
  Future initialise() async {
    notiList = List<NotiMessage>();
    if (Platform.isIOS) {
      // request permissions if we're on android
      _fcm.requestNotificationPermissions(IosNotificationSettings());
      _fcm.configure();
      // For testing purposes print the Firebase Messaging token
      String token = await _fcm.getToken();
      print("FirebaseMessaging token: $token");
    } else{
      String token = await _fcm.getToken();
      print("FirebaseMessaging token: $token");
    }

    _fcm.configure(
      // Called when the app is in the foreground and we receive a push notification
      onMessage: (Map<String, dynamic> message) async {
        print('onMessage: $message');
//add list of messages to shared preferences
        _setMessage(message);
      },
      // Called when the app has been closed comlpetely and it's opened
      // from the push notification.
      onLaunch: (Map<String, dynamic> message) async {
        print('onLaunch: $message');
        _serialiseAndNavigate(message);
//add list of messages to shared preferences
        _setMessage(message);
      },
      // Called when the app is in the background and it's opened
      // from the push notification.
      onResume: (Map<String, dynamic> message) async {
        print('onResume: $message');
        _serialiseAndNavigate(message);
//add list of messages to shared preferences
        _setMessage(message);
      },
    );
  }

  void _serialiseAndNavigate(Map<String, dynamic> message) {
    var notificationData = message['data'];
    var view = notificationData['view'];

    if (view != null) {
      // Navigate to desired page
      if (view == 'create_post') {

      }
    }
  }
  _setMessage(Map<String, dynamic> message) {
//add list of messages to shared preferences
    final notification = message['notification'];
    final data = message['data'];
    final String title = notification['title'];
    final String body = notification['body'];
    String mMessage = data['message'];
    //add to list
    titles.add(title);
    msgs.add(mMessage);
    //save to shared preferences (does not work)
    storeTitles(titles);
    storeMsgs(msgs);
    print("Title: $title, body: $body, message: $mMessage");

  }
  void storeTitles(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setStringList("notiTitles", list);
//list returns null
  }
  void storeMsgs(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setStringList("notiMsgs", list);
  }
  Future<List<String>> getTitles(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    list = prefs.getStringList("notiTitles");
 return prefs.getStringList("notiTitles");
  }
  Future<List<String>> getMsgs(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    list = prefs.getStringList("notiMsgs");
  return prefs.getStringList("notiMsgs");
  }
}

实现这一目标的最佳方法是什么。我想永久保存消息并在另一个屏幕中调用它们。请帮帮我。

【问题讨论】:

  • 你的问题解决了吗?
  • 你能解决这个问题吗?

标签: flutter dart sharedpreferences


【解决方案1】:

shared_preferences 上保存列表的代码似乎没问题。问题可能在于如何获取数据。如果您要存储关键数据,我建议您最好改用provider 之类的东西。 shared_preferences 插件无法保证写入将在其docs 中提到的返回后持久化到磁盘。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2020-04-02
    • 2012-09-10
    • 1970-01-01
    • 2017-02-26
    • 2022-01-06
    • 2017-07-29
    相关资源
    最近更新 更多