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 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 bool? initialized = await flutterLocalNotificationsPlugin .initialize(initializationSettings); print(initialized); } Future 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, ), ), ); } 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: DarwinNotificationDetails( 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 ); } }