Added Notifications

main
Kai Mannweiler 2023-02-24 20:30:28 +01:00
parent d2773cad54
commit de7361f9ad
5 changed files with 151 additions and 2 deletions

View File

@ -1,7 +1,15 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/pages/main_page.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 { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});

View File

@ -3,6 +3,7 @@ import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/models/settings.dart';
import 'package:smoke_cess_app/service/json_service.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/settings_service.dart';
import 'package:smoke_cess_app/service/notification_service.dart';
import '../widgets/missing_config_popup.dart'; import '../widgets/missing_config_popup.dart';
@ -67,6 +68,7 @@ class ScannerPageState extends State<ScannerPage> {
textStyle: const TextStyle(fontSize: 20)), textStyle: const TextStyle(fontSize: 20)),
onPressed: () { onPressed: () {
loadSettingsFromLocalJSON(); loadSettingsFromLocalJSON();
NotificationService().setAllNotifications();
}, },
child: const Text('Read JSON'), child: const Text('Read JSON'),
) )

View File

@ -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<void> 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<void> setAllNotifications() async {
List<TZDateTime> moodDates = await getDatesforMood();
List<TZDateTime> 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<void> 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
);
}
}

View File

@ -1,6 +1,13 @@
# Generated by pub # Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
async: async:
dependency: transitive dependency: transitive
description: description:
@ -99,6 +106,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.5" version: "1.0.5"
dbus:
dependency: transitive
description:
name: dbus
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.8"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@ -132,6 +146,27 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.1" 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: flutter_test:
dependency: "direct dev" dependency: "direct dev"
description: flutter description: flutter
@ -247,6 +282,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.3" version: "2.1.3"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.0"
platform: platform:
dependency: transitive dependency: transitive
description: description:
@ -364,6 +406,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.4.12" version: "0.4.12"
timezone:
dependency: "direct main"
description:
name: timezone
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.0"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
@ -398,7 +447,14 @@ packages:
name: xdg_directories name: xdg_directories
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted 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: sdks:
dart: ">=2.18.2 <3.0.0" dart: ">=2.18.2 <3.0.0"
flutter: ">=3.0.0" flutter: ">=3.0.0"

View File

@ -35,6 +35,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
flutter_local_notifications: ^9.3.1
timezone: ^0.8.0
shared_preferences: ^2.0.17 shared_preferences: ^2.0.17
audioplayers: ^3.0.1 audioplayers: ^3.0.1
mobile_scanner: ^3.0.0 mobile_scanner: ^3.0.0