diff --git a/lib/main.dart b/lib/main.dart index 6e5d7a5..36efb6c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,15 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/pages/main_page.dart'; +import 'package:smoke_cess_app/service/notification_service.dart'; -void main() => runApp(const MyApp()); +void main() { + // to ensure all the widgets are initialized. + WidgetsFlutterBinding.ensureInitialized(); + + // to initialize the notificationservice. + NotificationService().initNotification(); + runApp(const MyApp()); +} class MyApp extends StatelessWidget { const MyApp({super.key}); diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index fcd2c30..b21b523 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -3,6 +3,7 @@ import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/service/json_service.dart'; import 'package:smoke_cess_app/service/settings_service.dart'; +import 'package:smoke_cess_app/service/notification_service.dart'; import '../widgets/missing_config_popup.dart'; @@ -67,6 +68,7 @@ class ScannerPageState extends State { textStyle: const TextStyle(fontSize: 20)), onPressed: () { loadSettingsFromLocalJSON(); + NotificationService().setAllNotifications(); }, child: const Text('Read JSON'), ) diff --git a/lib/service/notification_service.dart b/lib/service/notification_service.dart new file mode 100644 index 0000000..daff249 --- /dev/null +++ b/lib/service/notification_service.dart @@ -0,0 +1,81 @@ +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:smoke_cess_app/service/date_service.dart'; +import 'package:timezone/timezone.dart'; + +class NotificationService { + static final NotificationService _notificationService = + NotificationService._internal(); + + factory NotificationService() { + return _notificationService; + } + + final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = + FlutterLocalNotificationsPlugin(); + + NotificationService._internal(); + + Future initNotification() async { + // Android initialization + const AndroidInitializationSettings initializationSettingsAndroid = + AndroidInitializationSettings('@mipmap/ic_launcher'); + + // ios initialization + const IOSInitializationSettings initializationSettingsIOS = + IOSInitializationSettings( + requestAlertPermission: false, + requestBadgePermission: false, + requestSoundPermission: false, + ); + + const InitializationSettings initializationSettings = + InitializationSettings( + android: initializationSettingsAndroid, + iOS: initializationSettingsIOS); + // the initialization settings are initialized after they are setted + await flutterLocalNotificationsPlugin.initialize(initializationSettings); + } + + Future setAllNotifications() async { + List moodDates = await getDatesforMood(); + List sleepDates = await getDatesforSleep(); + for (var date in moodDates) { + setNotification(1, "Mood", "Evaluate your mood", date); + print("mood"); + } + for (var date in sleepDates) { + setNotification(1, "Sleep", "Evaluate your sleep", date); + print("sleep"); + } + } + + Future setNotification( + int id, String title, String body, TZDateTime tzDateTime) async { + await flutterLocalNotificationsPlugin.zonedSchedule( + id, + title, + body, + tzDateTime, //schedule the notification to show after 2 seconds. + const NotificationDetails( + // Android details + android: AndroidNotificationDetails('main_channel', 'Main Channel', + channelDescription: "ashwin", + importance: Importance.max, + priority: Priority.max), + // iOS details + iOS: IOSNotificationDetails( + sound: 'default.wav', + presentAlert: true, + presentBadge: true, + presentSound: true, + ), + ), + + // Type of time interpretation + uiLocalNotificationDateInterpretation: + UILocalNotificationDateInterpretation.absoluteTime, + androidAllowWhileIdle: + true, // To show notification even when the app is closed + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index e039943..57b3f29 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.0" async: dependency: transitive description: @@ -99,6 +106,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.5" + dbus: + dependency: transitive + description: + name: dbus + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.8" fake_async: dependency: transitive description: @@ -132,6 +146,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_local_notifications: + dependency: "direct main" + description: + name: flutter_local_notifications + url: "https://pub.dartlang.org" + source: hosted + version: "9.9.1" + flutter_local_notifications_linux: + dependency: transitive + description: + name: flutter_local_notifications_linux + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.1" + flutter_local_notifications_platform_interface: + dependency: transitive + description: + name: flutter_local_notifications_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "5.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -247,6 +282,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.3" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.0" platform: dependency: transitive description: @@ -364,6 +406,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.12" + timezone: + dependency: "direct main" + description: + name: timezone + url: "https://pub.dartlang.org" + source: hosted + version: "0.8.0" typed_data: dependency: transitive description: @@ -398,7 +447,14 @@ packages: name: xdg_directories url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "0.2.0+3" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.0" sdks: dart: ">=2.18.2 <3.0.0" flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 58445b3..e0c215d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,8 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + flutter_local_notifications: ^9.3.1 + timezone: ^0.8.0 shared_preferences: ^2.0.17 audioplayers: ^3.0.1 mobile_scanner: ^3.0.0