flutter如何自动获取Notification
How get Notification automatically flutter
我实现了一个通知功能,可以从 API 获取警报并每隔一分钟将其作为通知显示给用户。通知只能在我按下按钮时显示。我想通过脚本或函数更改按下动作可以每 5 秒自动执行相同的工作。 (没有任何按钮).
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.periodicallyShow(
0,
dataa['boxName'],
dataa['alert_description'],
RepeatInterval.everyMinute,
platformChannelSpecifics);
} else {
print("no message");
}
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
Color color;
Widget body = new FutureBuilder<UserAlert>(
future: boxApi.getAlert(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new ListView.builder(
itemCount: snapshot.data.alerts.length,
itemBuilder: (context, index) {
var alert = snapshot.data.alerts[index].alertLevel;
switch (alert) {
case "danger":
color = Colors.red;
break;
case "warning":
color = Colors.yellow;
break;
case "info":
color = Colors.green;
break;
default:
color = Colors.grey;
}
var boxName = snapshot.data.alerts[index].boxName;
var date = snapshot.data.alerts[index].alertDate;
var time = snapshot.data.alerts[index].alertTime;
var description =
snapshot.data.alerts[index].alertDescription;
return Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text("$date" + " " + "$time"),
subtitle: Text("$description"),
trailing: Text("$boxName"),
leading: Transform.translate(
offset: const Offset(-15.0, 0.0),
child: Container(
color: color,
child: SizedBox(
width: 5,
height: 100,
} else {
return new Center(
child: new CircularProgressIndicator(),
);
}
});
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => //Or whenever you actually want to trigger it
_showNotification(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
body: body);
}
}
如何替换按钮操作??
这里有一个示例代码,可以帮助您运行执行特定时间重复的操作。
const _time = const Duration(minutes:5) ;
new Timer.periodic(_time, (Timer t) => print('flutter'));
我实现了一个通知功能,可以从 API 获取警报并每隔一分钟将其作为通知显示给用户。通知只能在我按下按钮时显示。我想通过脚本或函数更改按下动作可以每 5 秒自动执行相同的工作。 (没有任何按钮).
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.periodicallyShow(
0,
dataa['boxName'],
dataa['alert_description'],
RepeatInterval.everyMinute,
platformChannelSpecifics);
} else {
print("no message");
}
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
Color color;
Widget body = new FutureBuilder<UserAlert>(
future: boxApi.getAlert(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new ListView.builder(
itemCount: snapshot.data.alerts.length,
itemBuilder: (context, index) {
var alert = snapshot.data.alerts[index].alertLevel;
switch (alert) {
case "danger":
color = Colors.red;
break;
case "warning":
color = Colors.yellow;
break;
case "info":
color = Colors.green;
break;
default:
color = Colors.grey;
}
var boxName = snapshot.data.alerts[index].boxName;
var date = snapshot.data.alerts[index].alertDate;
var time = snapshot.data.alerts[index].alertTime;
var description =
snapshot.data.alerts[index].alertDescription;
return Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text("$date" + " " + "$time"),
subtitle: Text("$description"),
trailing: Text("$boxName"),
leading: Transform.translate(
offset: const Offset(-15.0, 0.0),
child: Container(
color: color,
child: SizedBox(
width: 5,
height: 100,
} else {
return new Center(
child: new CircularProgressIndicator(),
);
}
});
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => //Or whenever you actually want to trigger it
_showNotification(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
body: body);
}
}
如何替换按钮操作??
这里有一个示例代码,可以帮助您运行执行特定时间重复的操作。
const _time = const Duration(minutes:5) ;
new Timer.periodic(_time, (Timer t) => print('flutter'));