引导、通知栏计时

This commit is contained in:
ytc1012
2025-11-24 10:33:09 +08:00
parent 149d1ed6cd
commit 2c6ced5c14
7 changed files with 496 additions and 28 deletions

View File

@@ -189,6 +189,88 @@ class NotificationService {
}
}
/// Cancel a specific notification by ID
Future<void> cancel(int id) async {
if (kIsWeb || !_initialized) return;
try {
await _notifications.cancel(id);
} catch (e) {
if (kDebugMode) {
print('Failed to cancel notification $id: $e');
}
}
}
/// Show ongoing focus session notification (for background)
/// This notification stays visible while the timer is running
Future<void> showOngoingFocusNotification({
required int remainingMinutes,
required int remainingSeconds,
}) async {
if (kIsWeb || !_initialized) return;
try {
// Format time display
final timeStr = '${remainingMinutes.toString().padLeft(2, '0')}:${(remainingSeconds % 60).toString().padLeft(2, '0')}';
const androidDetails = AndroidNotificationDetails(
'focus_timer',
'Focus Timer',
channelDescription: 'Shows ongoing focus session timer',
importance: Importance.low,
priority: Priority.low,
ongoing: true, // Makes notification persistent
autoCancel: false,
showWhen: false,
enableVibration: false,
playSound: false,
// Show in status bar
showProgress: false,
);
const iosDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: false,
presentSound: false,
);
const notificationDetails = NotificationDetails(
android: androidDetails,
iOS: iosDetails,
);
await _notifications.show(
2, // Use ID 2 for ongoing notifications
'⏱️ Focus session in progress',
'$timeStr remaining',
notificationDetails,
payload: 'focus_ongoing',
);
} catch (e) {
if (kDebugMode) {
print('Failed to show ongoing notification: $e');
}
}
}
/// Update ongoing notification with new time
Future<void> updateOngoingFocusNotification({
required int remainingMinutes,
required int remainingSeconds,
}) async {
// On Android, showing the same notification ID updates it
await showOngoingFocusNotification(
remainingMinutes: remainingMinutes,
remainingSeconds: remainingSeconds,
);
}
/// Cancel ongoing focus notification
Future<void> cancelOngoingFocusNotification() async {
await cancel(2); // Cancel notification with ID 2
}
/// Check if notifications are supported on this platform
bool get isSupported => !kIsWeb;
}