Сохранить и получить уведомление с трепетанием списка общих настроек - PullRequest
0 голосов
/ 27 марта 2020

Я реализовал Firebase Cloud Messaging в своем приложении. Все работает хорошо, но я хочу сохранить весь список сообщений локально с помощью shared preferences и получить их на другом экране. Все мои логи c не работают, так как я не могу сохранить сообщения при вызове функции 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");
  }
}

Что лучший способ достичь этого. Я хочу постоянно сохранять сообщения и вызывать их на другом экране. Пожалуйста, помогите мне.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...