flutter - 满足特定条件时向 Flutter App 推送本地通知
问题描述
当满足特定条件时,我想向我的颤振应用程序发送警报。例如,我的应用程序检测到水位上升,并将向我的应用程序发送水位上升的警报。当我运行这段代码时,它总是给我圆形指示器。
下面的代码是我的条件小部件:
Widget displayWarning() {
if (inches1 < 10) {
return Text(" 10 inches, WATER LEVEL is LOW");
} else if (inches1 < 30) {
return Text(" 30 inches, WATER LEVEL IS MEDIUM");
} else if (inches1 < 50) {
return Text("50 inches, WATER LEVEL IS HIGH");
}
return Text("Water level is CRITICAL");
}
下面的代码是我的通知类:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class NotificationApi {
static final _notifications = FlutterLocalNotificationsPlugin();
static Future _notificationDetails() async {
return NotificationDetails(
android: AndroidNotificationDetails(
'channel id',
'channel name',
//'channel description',
importance: Importance.max,
),
iOS: IOSNotificationDetails(),
);
}
static Future showNotification({
int id = 0,
String? title,
String? body,
String? payload,
}) async => _notifications.show(
id,
title,
body,
await _notificationDetails(),
payload: payload,
);
}
下面的代码用于小部件或我的主 dart 文件:
FutureBuilder(
future: NotificationApi.showNotification(
title: 'ALERT',
body: "There is a rise in water level",
),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if(snapshot.hasData) {
return displayWarning();
}
else {
return Container(child: CircularProgressIndicator());
}
}
),