cpd_2022_zi/lib/services/notification_service.dart

85 lines
2.8 KiB
Dart
Raw Permalink Normal View History

2023-02-26 14:10:06 +01:00
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:smoke_cess_app/services/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 DarwinInitializationSettings initializationSettingsIOS =
DarwinInitializationSettings(
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();
int index = 0;
for (var date in moodDates) {
setNotification(index, "Mood", "Evaluate your mood", date);
index++;
}
for (var date in sleepDates) {
setNotification(index, "Sleep", "Evaluate your sleep", date);
index++;
}
}
Future<void> setNotification(
int id, String title, String body, TZDateTime tzDateTime) async {
2023-03-07 09:45:54 +01:00
if (tzDateTime.isAfter(DateTime.now())) {
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: DarwinNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
2023-02-26 14:10:06 +01:00
),
2023-03-07 09:45:54 +01:00
// Type of time interpretation
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle:
true, // To show notification even when the app is closed
);
}
2023-02-26 14:10:06 +01:00
}
}