cpd_2022_zi/lib/service/notification_service.dart

111 lines
3.4 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 =
2023-02-25 14:58:10 +01:00
AndroidInitializationSettings('mipmap/ic_launcher');
2023-02-24 20:30:28 +01:00
// ios initialization
2023-02-25 14:19:55 +01:00
const DarwinInitializationSettings initializationSettingsIOS =
DarwinInitializationSettings(
2023-02-24 20:30:28 +01:00
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
);
const InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS);
// the initialization settings are initialized after they are setted
2023-02-25 14:19:55 +01:00
bool? initialized = await flutterLocalNotificationsPlugin
.initialize(initializationSettings);
print(initialized);
2023-02-24 20:30:28 +01:00
}
2023-02-25 14:58:10 +01:00
Future<void> showNotification() async {
await flutterLocalNotificationsPlugin.show(
0,
'test',
'test',
//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-24 20:30:28 +01:00
Future<void> setAllNotifications() async {
List<TZDateTime> moodDates = await getDatesforMood();
List<TZDateTime> sleepDates = await getDatesforSleep();
2023-02-25 15:52:07 +01:00
int index = 0;
2023-02-24 20:30:28 +01:00
for (var date in moodDates) {
2023-02-25 15:52:07 +01:00
setNotification(index, "Mood", "Evaluate your mood", date);
2023-02-24 20:30:28 +01:00
print("mood");
2023-02-25 15:52:07 +01:00
index++;
2023-02-24 20:30:28 +01:00
}
for (var date in sleepDates) {
2023-02-25 15:52:07 +01:00
setNotification(index, "Sleep", "Evaluate your sleep", date);
2023-02-24 20:30:28 +01:00
print("sleep");
2023-02-25 15:52:07 +01:00
print(date);
index++;
2023-02-24 20:30:28 +01:00
}
}
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
2023-02-25 14:19:55 +01:00
iOS: DarwinNotificationDetails(
2023-02-24 20:30:28 +01:00
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
);
}
}