diff --git a/android/app/build.gradle b/android/app/build.gradle index 92f14a8..0b5b0f8 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -64,4 +64,13 @@ flutter { source '../..' } -dependencies {} +dependencies { + constraints { + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") { + because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib") + } + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") { + because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib") + } + } +} diff --git a/lib/main.dart b/lib/main.dart index 881a8de..9673440 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter/material.dart'; import 'package:hrt/notification.dart'; +import 'package:hrt/tasks.dart'; import 'package:permission_handler/permission_handler.dart'; void main() { @@ -12,6 +13,7 @@ class App extends StatelessWidget { @override Widget build(BuildContext context) { + setupBackgroundTasks(); return DynamicColorBuilder( builder: ((lightDynamic, darkDynamic) => MaterialApp( title: "HRT", diff --git a/lib/tasks.dart b/lib/tasks.dart new file mode 100644 index 0000000..7628be0 --- /dev/null +++ b/lib/tasks.dart @@ -0,0 +1,58 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:hrt/notification.dart'; +import 'package:permission_handler/permission_handler.dart'; +import 'package:workmanager/workmanager.dart'; + +@pragma('vm:entry-point') +void callbackDispatcher() { + Workmanager().executeTask((taskName, inputData) async { + print("Native called background task: $taskName"); + + if (taskName == 'scheduleNotifications') { + sendNotification(); + } + + return true; + }); +} + +void setupBackgroundTasks() async { + WidgetsFlutterBinding.ensureInitialized(); + await Workmanager().initialize( + callbackDispatcher, + isInDebugMode: kDebugMode, + ); + + await Permission.notification.request(); + + await FlutterLocalNotificationsPlugin().initialize( + const InitializationSettings( + android: AndroidInitializationSettings('estrogen'), + ), + // onSelectNotification: handleNotificationClick, + ); + + var launchDetails = + await FlutterLocalNotificationsPlugin().getNotificationAppLaunchDetails(); + + // if (launchDetails?.payload != null && launchDetails!.payload!.isNotEmpty) { + // handleNotificationClick(launchDetails.payload); + // } + + // Runs every 15 minutes + await Workmanager().registerPeriodicTask( + 'schedule_notifications', + 'scheduleNotifications', + constraints: Constraints( + networkType: NetworkType.not_required, + requiresCharging: false, + requiresBatteryNotLow: false, + requiresDeviceIdle: false, + requiresStorageNotLow: false, + ), + initialDelay: const Duration(seconds: 15), + frequency: const Duration(minutes: 15), + ); +} diff --git a/pubspec.lock b/pubspec.lock index 3e5192d..ce36d80 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -106,10 +106,10 @@ packages: dependency: "direct main" description: name: flutter_local_notifications - sha256: "3002092e5b8ce2f86c3361422e52e6db6776c23ee21e0b2f71b892bf4259ef04" + sha256: "501ed9d54f1c8c0535b7991bade36f9e7e3b45a2346401f03775c1ec7a3c06ae" url: "https://pub.dev" source: hosted - version: "15.1.1" + version: "15.1.2" flutter_local_notifications_linux: dependency: transitive description: @@ -281,7 +281,7 @@ packages: source: hosted version: "0.6.1" timezone: - dependency: transitive + dependency: "direct main" description: name: timezone sha256: "1cfd8ddc2d1cfd836bc93e67b9be88c3adaeca6f40a00ca999104c30693cdca0" @@ -304,6 +304,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.1.4-beta" + workmanager: + dependency: "direct main" + description: + name: workmanager + sha256: ed13530cccd28c5c9959ad42d657cd0666274ca74c56dea0ca183ddd527d3a00 + url: "https://pub.dev" + source: hosted + version: "0.5.2" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 33278ce..2cca27f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,8 @@ dependencies: dynamic_color: ^1.6.7 flutter_local_notifications: ^15.1.1 permission_handler: ^11.0.1 + workmanager: ^0.5.2 + timezone: ^0.9.2 dev_dependencies: flutter_test: