Save and Retrieve notification with shared preferences list flutter

Viewed 2438

I have implemented Firebase Cloud Messaging in my flutter application. Everything works well but I want to store all list of messages locally with shared preferences and retrieve them in another screen. All my logic does not work well as I can't save the messages when the onMessage function is called.

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

Whats the best way to achieve this. I want to save the messages persistently and call them in another screen. Please help me.

1 Answers

The code on saving the List on shared_preferences seems to be ok. The issue might be on how the data is fetched. If you're storing critical data, I suggest to better use something like provider instead. The shared_preferences plugin is unable to guarantee that writes will be persisted to disk after returning as mentioned in its docs.

Related