-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcbef74
commit 6ba1640
Showing
10 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CustomNotification{ | ||
final int id; | ||
final String? title; | ||
final String? body; | ||
final DateTime? dateTime; | ||
|
||
|
||
CustomNotification({ | ||
required this.id, | ||
this.title, | ||
this.body, | ||
this.dateTime | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | ||
import 'package:flutter_native_timezone/flutter_native_timezone.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
import 'package:sigalogin/src/controllers/student_controller.dart'; | ||
import 'package:sigalogin/src/models/custom_notification.dart'; | ||
import 'package:sigalogin/src/models/schedule.dart'; | ||
import 'package:timezone/timezone.dart' as tz; | ||
import 'package:timezone/data/latest.dart' as tz; | ||
|
||
|
||
class NotificationService{ | ||
late FlutterLocalNotificationsPlugin localNotificationsPlugin; | ||
late AndroidNotificationDetails androidDetails; | ||
|
||
NotificationService(){ | ||
localNotificationsPlugin = FlutterLocalNotificationsPlugin(); | ||
_setupNotifications(); | ||
} | ||
|
||
_setupNotifications()async{ | ||
await _setupTimeZone(); | ||
await _initializaNotifications(); | ||
} | ||
|
||
_setupTimeZone()async{ | ||
tz.initializeTimeZones(); | ||
final String timeZoneName = await FlutterNativeTimezone.getLocalTimezone(); | ||
tz.setLocalLocation(tz.getLocation(timeZoneName)); | ||
} | ||
|
||
_initializaNotifications() async{ | ||
const android = AndroidInitializationSettings('@mipmap/ic_launcher'); | ||
await localNotificationsPlugin.initialize( | ||
const InitializationSettings( | ||
android: android | ||
), | ||
); | ||
} | ||
|
||
showNotification()async{ | ||
await Permission.notification.request(); | ||
List<Schedule> schedules = await _loadSchedules(); | ||
String period = await _getStudentPeriod(); | ||
if(schedules.isEmpty)return; | ||
androidDetails = const AndroidNotificationDetails( | ||
"Lembretes", | ||
"Lembretes de Horários", | ||
importance: Importance.max, | ||
priority: Priority.max, | ||
enableVibration: true, | ||
styleInformation: BigTextStyleInformation(''), | ||
); | ||
|
||
DateTime now = DateTime.now(); | ||
int today = DateTime.now().day; | ||
int dayInMonth = _daysInMonth(DateTime.now()); | ||
|
||
for(int i = today; i<dayInMonth; i++){ | ||
DateTime scheduleDate = DateTime( | ||
now.year, | ||
now.month, | ||
i, | ||
period=="Noite"?17:7, | ||
); | ||
|
||
if(scheduleDate.weekday != 6 && scheduleDate.weekday != 7 && scheduleDate.millisecondsSinceEpoch>now.millisecondsSinceEpoch){ | ||
print(scheduleDate); | ||
localNotificationsPlugin.zonedSchedule( | ||
i, | ||
'Aulas de hoje, ${schedules[scheduleDate.weekday-1].weekDay}', | ||
schedules[scheduleDate.weekday-1].schedule.toString().replaceAll(', ','\n').replaceAll("{", '').replaceAll("}", ''), | ||
tz.TZDateTime.from(scheduleDate, tz.local), | ||
NotificationDetails( | ||
android: androidDetails | ||
), | ||
androidAllowWhileIdle: true, | ||
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime | ||
); | ||
} | ||
} | ||
|
||
} | ||
|
||
Future<List<Schedule>> _loadSchedules()async{ | ||
StudentController controller = StudentController(); | ||
return await controller.querySchedule(); | ||
} | ||
|
||
Future<String> _getStudentPeriod()async{ | ||
StudentController controller = StudentController(); | ||
return (await controller.queryStudent()).period.trim(); | ||
} | ||
|
||
int _daysInMonth(DateTime date){ | ||
var firstDayThisMonth = DateTime(date.year, date.month, date.day); | ||
var firstDayNextMonth = DateTime(firstDayThisMonth.year, firstDayThisMonth.month + 1, firstDayThisMonth.day); | ||
return firstDayNextMonth.difference(firstDayThisMonth).inDays; | ||
} | ||
|
||
// scheduleNotification(){ | ||
// androidDetails = const AndroidNotificationDetails( | ||
// "Lembretes", | ||
// "Lembretes de Horários", | ||
// importance: Importance.max, | ||
// priority: Priority.max, | ||
// enableVibration: true | ||
// ); | ||
// | ||
// | ||
// | ||
// localNotificationsPlugin.zonedSchedule( | ||
// notification.id, | ||
// notification.title, | ||
// notification.body, | ||
// tz.TZDateTime.from(notification.dateTime!, tz.local), | ||
// NotificationDetails( | ||
// android: androidDetails | ||
// ), | ||
// androidAllowWhileIdle: true, | ||
// uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime | ||
// ); | ||
// } | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters