import 'dart:async'; import 'package:flutter/material.dart'; import 'package:todoapp/elems/filehandler.dart'; import 'package:todoapp/pages/Home.dart'; import 'package:provider/provider.dart'; import 'package:todoapp/providers/todoprovider.dart'; import 'package:awesome_notifications/awesome_notifications.dart'; import 'package:disable_battery_optimization/disable_battery_optimization.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:math'; @pragma('vm:entry-point') void callbackDispatcher() async { Map content = await FileHandler.fileContent; int id = 15; for (String key in content.keys) { Map list = content[key]; for (String title in list.keys) { Map current = list[title]; DateTime dueTime = DateTime.parse(current["dueTime"]); bool checked = current["checked"]; DateTime currentTime = DateTime.now(); int timeDiff = dueTime .difference(currentTime) .inMinutes; if (timeDiff <= 45 && timeDiff >= 0 && !checked) { AwesomeNotifications().createNotification( content: NotificationContent( id: id, channelKey: "reminder_channel", title: "Task due!", body: "Task $title due in $timeDiff minutes!")); } id++; } } } @pragma('vm:entry-point') void onStart(ServiceInstance service) { Timer.periodic(const Duration(minutes: 15), (timer) { callbackDispatcher(); }); } Future initService() async { final service = FlutterBackgroundService(); await service.configure( iosConfiguration: IosConfiguration(), androidConfiguration: AndroidConfiguration( onStart: onStart, isForegroundMode: false, autoStart: true, autoStartOnBoot: true ) ); service.startService(); } void main() async { WidgetsFlutterBinding.ensureInitialized(); await AwesomeNotifications().initialize(null, [ NotificationChannel( channelKey: "reminder_channel", channelName: "Reminders", channelDescription: "Reminder for ToDo", importance: NotificationImportance.High, ) ]); await initService(); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MultiProvider( providers: [ChangeNotifierProvider(create: (context) => TodoProvider())], child: MaterialApp( theme: ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: Colors.blue, brightness: Brightness.dark), useMaterial3: true, ), home: const Home(), ), ); } }