/
siblun
/
ReadFlow
Обзор
Документация
Войти
/
siblun
/
ReadFlow
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
lib/core/services/background_timer_service.dart
94 строки
3 KB
siblun
Add docs
21 апр 2026, 16:13
21 апр 2026, 16:13
c40d018
Код
Авторство
О чём код?
import 'dart:async'; import 'dart:ui'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; /// Сервис для работы таймера чтения в фоновом режиме. /// /// Использует [flutter_background_service] для поддержания активности /// и [flutter_local_notifications] для обновления времени в шторке уведомлений. /// /// Методы помечены @pragma('vm:entry-point') для корректной работы /// в отдельном изоляте. /// class BackgroundTimerService { static const notificationId = 888; static const notificationChannelId = 'read_flow_timer_channel'; static Future<void> initialize() async { final service = FlutterBackgroundService(); const AndroidNotificationChannel channel = AndroidNotificationChannel( notificationChannelId, 'Активная сессия чтения', description: 'Показывает время текущей сессии', importance: Importance.low, ); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(channel); await service.configure( androidConfiguration: AndroidConfiguration( onStart: onStart, autoStart: false, isForegroundMode: false, notificationChannelId: notificationChannelId, initialNotificationTitle: 'ReadFlow', initialNotificationContent: 'Сессия запущена', foregroundServiceNotificationId: notificationId, ), iosConfiguration: IosConfiguration( autoStart: false, onForeground: onStart, onBackground: onIosBackground, ), ); } } @pragma('vm:entry-point') void onStart(ServiceInstance service) async { DartPluginRegistrant.ensureInitialized(); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); service.on('update').listen((event) { final String time = event?['time'] ?? '00:00'; if (service is AndroidServiceInstance) { service.setAsForegroundService(); } flutterLocalNotificationsPlugin.show( id: BackgroundTimerService.notificationId, title: 'Читаем: $time', body: 'Продолжайте чтение', notificationDetails: const NotificationDetails( android: AndroidNotificationDetails( BackgroundTimerService.notificationChannelId, 'Активная сессия', icon: 'ic_bg_service_small', ongoing: true, onlyAlertOnce: true, importance: Importance.low, ), ), ); }); service.on('stopService').listen((event) { if (service is AndroidServiceInstance) { service.setAsBackgroundService(); } service.stopSelf(); }); } @pragma('vm:entry-point') bool onIosBackground(ServiceInstance service) => true;