fix:修复重启设备后没有通知提醒的问题

This commit is contained in:
2025-11-24 11:58:14 +08:00
parent 607b27d54f
commit 1a5efb1b48
6 changed files with 111 additions and 163 deletions

View File

@@ -1,3 +1,9 @@
import 'package:flisp_app/models/calendar.dart';
import 'package:flisp_app/models/todo.dart';
import 'package:flisp_app/service/calendar_service.dart';
import 'package:flisp_app/service/todo_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common/utils/date_utils.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
@@ -45,6 +51,51 @@ class NotifyService {
);
await _notifications.initialize(settings);
WidgetsBinding.instance.addPostFrameCallback((_) {
_refreshScheduled();
});
}
/// 重新刷新提醒 防止每次重启设备后提醒丢失的问题
void _refreshScheduled() async {
await cancelAllNotifications();
TodoService todoService = TodoService();
final List<Todo> todoResult = todoService.getAllTodos(
TodoSortMode.priority,
);
final List<Todo> todayTodos =
todoResult
.where(
(todo) => !todo.isCompleted && isAfterToday(todo.scheduledTime),
)
.toList();
for (Todo todo in todayTodos) {
await scheduleNotification(
id: todo.id,
title: '待办提醒',
body: todo.title,
scheduledTime: todo.scheduledTime!,
);
}
CalendarService calendarService = CalendarService();
final List<Calendar> calendarResult = calendarService.getAllCalendars();
final List<Calendar> todayCalendars =
calendarResult
.where((item) => isAfterToday(item.scheduledTime))
.toList();
for (Calendar calendar in todayCalendars) {
await scheduleNotification(
id: calendar.id,
title: '日程提醒',
body: calendar.title,
scheduledTime: calendar.scheduledTime!,
);
}
}
// 创建Android通知详情