From 22b1d3b75b678bc027822c4821e247648f52e424 Mon Sep 17 00:00:00 2001 From: brausjonas Date: Mon, 16 Sep 2024 00:47:09 +0200 Subject: [PATCH] background notifications --- android/app/build.gradle | 2 + android/app/src/main/AndroidManifest.xml | 3 +- lib/main.dart | 110 ++++++++++++----------- lib/pages/Home.dart | 9 +- pubspec.lock | 106 ++++++++++++++++++++-- pubspec.yaml | 3 +- 6 files changed, 170 insertions(+), 63 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index a9a8fba..25a0c05 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -35,6 +35,8 @@ android { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig = signingConfigs.debug + minifyEnabled false + shrinkResources false } } } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 4fe34a8..42b9cdf 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + + diff --git a/lib/main.dart b/lib/main.dart index 71ca3e1..f46e708 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,72 +1,80 @@ +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:workmanager/workmanager.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'; @pragma('vm:entry-point') -void callbackDispatcher() { - Workmanager().executeTask((task, inputData) async { - AwesomeNotifications().initialize( - null, - [ - NotificationChannel( - channelKey: "reminder_channel", - channelName: "Reminders", - channelDescription: "Reminder for ToDo") - ]); - - Map content = await FileHandler.fileContent; - 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 <= 30 && timeDiff >= 0 && !checked) { - AwesomeNotifications().createNotification( - content: NotificationContent( - id: 10, - channelKey: "reminder_channel", - title: "Task due!", - body: "Task $title due in $timeDiff minutes!" - ) - ); - } +void callbackDispatcher() async { + WidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.getInstance(); + print("test1"); + Map content = await FileHandler.fileContent; + for (String key in content.keys) { + Map list = content[key]; + for (String title in list.keys) { + print("test2"); + 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 <= 30 && timeDiff >= 0 && !checked) { + AwesomeNotifications().createNotification( + content: NotificationContent( + id: 10, + channelKey: "reminder_channel", + title: "Task due!", + body: "Task $title due in $timeDiff minutes!")); } } - return Future.value(true); + } +} + +@pragma('vm:entry-point') +void onStart(ServiceInstance service) { + Timer.periodic(const Duration(seconds: 10), (timer) { + callbackDispatcher(); }); } +Future initService() async { + final service = FlutterBackgroundService(); -void main() async { - - - AwesomeNotifications().initialize( - null, - [ - NotificationChannel( - channelKey: "reminder_channel", - channelName: "Reminders", - channelDescription: "Reminder for ToDo") - ]); - - Workmanager().initialize( - callbackDispatcher, - isInDebugMode: true + await service.configure( + iosConfiguration: IosConfiguration(), + androidConfiguration: AndroidConfiguration( + onStart: onStart, + isForegroundMode: false, + autoStart: true, + autoStartOnBoot: true + ) ); - Workmanager().registerPeriodicTask("check_nots", "nots_check", frequency: Duration(minutes: 15)); - // Workmanager().registerOneOffTask("test", "test1"); + 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()); } diff --git a/lib/pages/Home.dart b/lib/pages/Home.dart index bff29bf..7b95602 100644 --- a/lib/pages/Home.dart +++ b/lib/pages/Home.dart @@ -6,7 +6,6 @@ import 'package:todoapp/modules/drawer.dart'; import 'package:provider/provider.dart'; import 'package:todoapp/providers/todoprovider.dart'; import 'package:awesome_notifications/awesome_notifications.dart'; -import 'package:workmanager/workmanager.dart'; import 'package:disable_battery_optimization/disable_battery_optimization.dart'; @@ -19,12 +18,12 @@ class Home extends StatefulWidget { class _HomeState extends State { void getPermissions() async { - await DisableBatteryOptimization.showEnableAutoStartSettings("Enable Auto Start", "Follow the steps and enable the auto start of this app"); - await DisableBatteryOptimization.showDisableBatteryOptimizationSettings(); - - AwesomeNotifications().isNotificationAllowed().then((isAllowed) { + await AwesomeNotifications().isNotificationAllowed().then((isAllowed) { AwesomeNotifications().requestPermissionToSendNotifications(); }); + + await DisableBatteryOptimization.showEnableAutoStartSettings("Enable Auto Start", "Follow the steps and enable the auto start of this app"); + await DisableBatteryOptimization.showDisableBatteryOptimizationSettings(); } @override diff --git a/pubspec.lock b/pubspec.lock index 853f5fe..9792f0e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -89,11 +89,51 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.3" + file: + dependency: transitive + description: + name: file + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + url: "https://pub.dev" + source: hosted + version: "7.0.0" flutter: dependency: "direct main" description: flutter source: sdk version: "0.0.0" + flutter_background_service: + dependency: "direct dev" + description: + name: flutter_background_service + sha256: d32f078ec57647c9cfd6e1a8da9297f7d8f021d4dcc204a35aaad2cdbfe255f0 + url: "https://pub.dev" + source: hosted + version: "5.0.10" + flutter_background_service_android: + dependency: transitive + description: + name: flutter_background_service_android + sha256: "39da42dddf877beeef82bc2583130d8bedb4d0765e99ca9e7b4a32e8c6abd239" + url: "https://pub.dev" + source: hosted + version: "6.2.7" + flutter_background_service_ios: + dependency: transitive + description: + name: flutter_background_service_ios + sha256: "6037ffd45c4d019dab0975c7feb1d31012dd697e25edc05505a4a9b0c7dc9fba" + url: "https://pub.dev" + source: hosted + version: "5.0.3" + flutter_background_service_platform_interface: + dependency: transitive + description: + name: flutter_background_service_platform_interface + sha256: ca74aa95789a8304f4d3f57f07ba404faa86bed6e415f83e8edea6ad8b904a41 + url: "https://pub.dev" + source: hosted + version: "5.1.2" flutter_lints: dependency: "direct dev" description: @@ -264,6 +304,62 @@ packages: url: "https://pub.dev" source: hosted version: "6.1.2" + shared_preferences: + dependency: "direct dev" + description: + name: shared_preferences + sha256: "746e5369a43170c25816cc472ee016d3a66bc13fcf430c0bc41ad7b4b2922051" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "480ba4345773f56acda9abf5f50bd966f581dac5d514e5fc4a18c62976bbba7e" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: c4b35f6cb8f63c147312c054ce7c2254c8066745125264f0c88739c417fc9d9f + url: "https://pub.dev" + source: hosted + version: "2.5.2" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "57cbf196c486bc2cf1f02b85784932c6094376284b3ad5779d1b1c6c6a816b80" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: d2ca4132d3946fec2184261726b355836a82c33d7d5b67af32692aff18a4684e + url: "https://pub.dev" + source: hosted + version: "2.4.2" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1" + url: "https://pub.dev" + source: hosted + version: "2.4.1" sky_engine: dependency: transitive description: flutter @@ -333,14 +429,14 @@ packages: url: "https://pub.dev" source: hosted version: "14.2.5" - workmanager: - dependency: "direct dev" + web: + dependency: transitive description: - name: workmanager - sha256: ed13530cccd28c5c9959ad42d657cd0666274ca74c56dea0ca183ddd527d3a00 + name: web + sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062 url: "https://pub.dev" source: hosted - version: "0.5.2" + version: "1.0.0" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 2d2c0c6..2f2d042 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,9 +42,10 @@ dev_dependencies: provider: path_provider: awesome_notifications: - workmanager: + flutter_background_service: android_intent_plus: disable_battery_optimization: + shared_preferences: # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is