【发布时间】:2021-04-14 15:50:27
【问题描述】:
我有一个连接到 Firebase 的应用程序,我正在制作一个屏幕来显示所有发送的通知,因为我正在使用 SharedPreferences。但是当通知到达地图时,我将其放在列表
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());
}
我的问题是,我怎样才能保留这些通知,以便每当我想看到它们时,我都可以轻松获取它们,并使用通知设置卡片?
【问题讨论】:
-
那么,基本上你想要的是将 List
-
是的,但我不知道该怎么做。我需要保存列表,以便稍后在 Scaffold 上显示通知。