cpd_2022_zi/lib/service/notification_service.dart

82 lines
2.7 KiB
Dart
Raw Normal View History

2023-02-24 20:30:28 +01:00
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
);
}
}