我有一个连接到 Firebase 的应用程序,我正在制作一个屏幕来显示所有发送的通知,因为我正在使用 SharedPreferences。但是当通知到达地图时,我将它放在列表 <Map <dynamic, dynamic >> 中,以显示通知。
String title, body;
Map<dynamic, dynamic> notific;
List<Map<dynamic, dynamic>> notifList = [];
///Widget
return Scaffold(
extendBody: true,
backgroundColor: widget._colors.white,
appBar: appBar,
body: ListView.builder(
itemCount: notifList.length,
itemBuilder: (context, i) {
return Card(
margin: EdgeInsets.all(10),
elevation: 4,
child: ListTile(
title: Text(
notifList.elementAt(i)['title'],
),
subtitle: Text(
notifList.elementAt(i)['body'],
),
),
);
},
),
);
}
Firebase 方法
Future<dynamic> fcmMessageReceiver() async {
FirebaseMessaging.instance.getInitialMessage().then((value) {
if (value != null) {}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
notific = {
'title': message.notification.title,
'body': message.notification.body
};
notifList.add(notific);
setState(() {
title = message.notification.title;
body = message.notification.body;
});
print('MENSAGEM: $notific');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {});
}
Shared Preferences 方法,在 initState() 上调用
void savePush() async {
SharedPreferences sharePref = await SharedPreferences.getInstance();
final strList = sharePref.getStringList('push')??[];
sharePref.setStringList('push', notifList.toString());
}
我的问题是,我怎样才能保留这些通知,以便每当我想看到它们时,我都可以轻松获取它们,并使用通知设置卡片?