This commit is contained in:
2026-04-28 22:36:55 +08:00
parent 71c940ab46
commit b8f1a9e8d7
3 changed files with 24 additions and 6 deletions

View File

@@ -132,8 +132,18 @@ class _SmsListScreenState extends State<SmsListScreen> {
if (newVal) {
// Request notification permission for foreground service
await Permission.notification.request();
_startForegroundService();
final notifResult = await FlutterForegroundTask.requestNotificationPermission();
if (notifResult != NotificationPermission.granted) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('需要通知权限才能保持后台运行')),
);
}
settings.setAutoUpload(false);
setState(() {});
return;
}
await _startForegroundService();
_startAutoUpload();
} else {
_stopForegroundService();
@@ -143,12 +153,20 @@ class _SmsListScreenState extends State<SmsListScreen> {
setState(() {});
}
void _startForegroundService() {
FlutterForegroundTask.startService(
Future<void> _startForegroundService() async {
final result = await FlutterForegroundTask.startService(
serviceId: 888,
notificationTitle: 'SMS Monitor',
notificationText: '正在监听新短信...',
callback: startCallback,
);
if (result is ServiceRequestFailure && mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('后台服务启动失败: ${result.error}')),
);
settings.setAutoUpload(false);
setState(() {});
}
}
void _stopForegroundService() {