flutter - 如何在列表 Flutter 中保存通知
问题描述
我实现了通知插件,我可以从服务器获取警报列表。如您所见,我正在尝试获取最后一个警报。我想获取最后 10 个警报,而不仅仅是一个并将其保存在列表中。
之后我想向用户显示所有 10 个通知。
这是我的代码:
Future<void> repeatNotification() async {
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID 3',
'CHANNEL_NAME 3',
"CHANNEL_DESCRIPTION 3",
playSound: true,
importance: Importance.max,
priority: Priority.high,
sound: RawResourceAndroidNotificationSound('notification'),
styleInformation: DefaultStyleInformation(true, true),
);
var iosChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidChannelSpecifics, iOS: iosChannelSpecifics);
SharedPreferences localStorage = await SharedPreferences.getInstance();
String token = localStorage.getString('access_token');
Map<String, String> headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token'
};
var response =
await http.get(Uri.parse(ApiUtil.GET_ALERT), headers: headers);
var data = json.decode(response.body);
var dataa = (data['data']['data']['data'][0]);
if (data['status'] == 200) {
flutterLocalNotificationsPlugin.show(
0,
dataa['boxName'],
dataa['alert_description'],
// RepeatInterval.everyMinute,
platformChannelSpecifics);
} else {
print("no message");
}
}
这是来自服务器的响应:
{
"code": 0,
"message": " success",
"data": {
"data": {
"current_page": 1,
"data": [
{
"id": 69,
"user_id": 53,
"boxIdentifiant": 1924589682265310,
"boxName": "box Malta",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne high",
"alert_level": "info"
},
{
"id": 68,
"user_id": 53,
"boxIdentifiant": 1924589682265310,
"boxName": "box Malta",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression",
"alert_level": "warning"
},
{
"id": 67,
"user_id": 53,
"boxIdentifiant": 1924589682265309,
"boxName": "box masr",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression",
"alert_level": "warning"
},
{
"id": 66,
"user_id": 53,
"boxIdentifiant": 1924589682265308,
"boxName": "box libya",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression",
"alert_level": "warning"
},
{
"id": 62,
"user_id": 53,
"boxIdentifiant": 1924589682265245,
"boxName": "Box Sfax",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression",
"alert_level": "warning"
},
{
"id": 61,
"user_id": 53,
"boxIdentifiant": 1924589682265243,
"boxName": "Box Tunis",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression Roux",
"alert_level": "info"
},
{
"id": 58,
"user_id": 53,
"boxIdentifiant": 1924589682265244,
"boxName": "Box Office",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression Roux",
"alert_level": "warning"
},
{
"id": 57,
"user_id": 53,
"boxIdentifiant": 1924589682265244,
"boxName": "Box Office",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne batterie",
"alert_level": "danger"
},
{
"id": 56,
"user_id": 53,
"boxIdentifiant": 1924589682265300,
"boxName": "box Maroc",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne batterie",
"alert_level": "danger"
},
{
"id": 54,
"user_id": 53,
"boxIdentifiant": 1924589682265246,
"boxName": "boxt mannouba",
"alert_date": "2021-05-30",
"alert_time": "09:45",
"alert_description": "Panne roue",
"alert_level": "info"
}
],
我怎样才能实现呢?
解决方案
尝试在通知列表上循环并为此列表中的每个项目推送通知
像这样:
var response =
await http.get(Uri.parse(ApiUtil.GET_ALERT), headers: headers);
var data = json.decode(response.body);
List notificationsList = data['data']['data']['data'];
if (data['status'] == 200) {
notificationsList.forEach((notification) {
flutterLocalNotificationsPlugin.show(
Random.nextInt(1000),
notification['boxName'],
notification['alert_description'],
// RepeatInterval.everyMinute,
platformChannelSpecifics);
});
} else {
print("no message");
}
推荐阅读
- html - 如何使两个标题在另一个下方并使它们固定在滚动上?
- ios - 如何为部分屏幕外的 UIView 设置动画
- api - 我试图简单地连接到 ibapi(Interactive Brokers API),但我在使用 Flask 时遇到了一些技术问题
- python - 从 Outlook REST API v1.0 中的基本身份验证迁移到 v2.0
- android - 全局对话框 - 如何?
- javascript - Vba to IE Click JavaScript button no Id
- wso2 - WSO2 - EI 6.2.0 - 使用 EI 配置 Oracle AQ JMS 数据库以向/从 Oracle AQ JMS 发布/使用消息
- linux - 使用 r 扩展时在 Linux 上无头运行 Netlogo Behaviorspace 时不会停止
- c# - 远程服务器返回错误:(415) Unsupported Media Type using API in asp.net
- php - 在php中从exec发送电子邮件