Skip to content

Commit

Permalink
Notification Implements
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusrmatias committed Nov 16, 2023
1 parent fcbef74 commit 6ba1640
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 1 deletion.
Binary file modified android/app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:sigalogin/src/repositories/student_card_repository.dart';
import 'package:sigalogin/src/repositories/student_repository.dart';
import 'package:sigalogin/src/repositories/settings_repository.dart';
import 'package:sigalogin/src/repositories/update_repository.dart';
import 'package:sigalogin/src/services/notification_service.dart';
import 'package:sigalogin/src/services/update_service.dart';

void main() async{
Expand Down Expand Up @@ -55,6 +56,7 @@ void main() async{
ChangeNotifierProvider<StudentRepository>(create: (context)=>StudentRepository(student, [], [], [])),
ChangeNotifierProvider<StudentCardRepository>(create: (context)=>StudentCardRepository(card)),
ChangeNotifierProvider<UpdateRepository>(create: (context)=>UpdateRepository(update: update)),
Provider<NotificationService>(create: (context)=>NotificationService())
],
child: MyApp(page: const LoginPage()),
)
Expand All @@ -68,6 +70,7 @@ void main() async{
ChangeNotifierProvider<StudentRepository>(create: (context)=>StudentRepository(student,historic,assessment,schedule)),
ChangeNotifierProvider<StudentCardRepository>(create: (context)=>StudentCardRepository(card)),
ChangeNotifierProvider<UpdateRepository>(create: (context)=>UpdateRepository(update: update)),
Provider<NotificationService>(create: (context)=>NotificationService())
],
child: MyApp(page: const AuthPage()),
)
Expand All @@ -80,6 +83,7 @@ void main() async{
ChangeNotifierProvider<StudentRepository>(create: (context)=>StudentRepository(student,historic,assessment,schedule)),
ChangeNotifierProvider<StudentCardRepository>(create: (context)=>StudentCardRepository(card)),
ChangeNotifierProvider<UpdateRepository>(create: (context)=>UpdateRepository(update: update)),
Provider<NotificationService>(create: (context)=>NotificationService())
],
child: MyApp(page: HomePage(afterLogin: !(prefs.getBool('updateOnOpen')??true), update: update)),
)
Expand Down
14 changes: 14 additions & 0 deletions lib/src/models/custom_notification.dart
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
});
}
3 changes: 3 additions & 0 deletions lib/src/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:intl/intl.dart';
import 'package:page_transition/page_transition.dart';
import 'package:provider/provider.dart';
import 'package:sigalogin/src/models/assessment.dart';
import 'package:sigalogin/src/models/custom_notification.dart';
import 'package:sigalogin/src/models/historic.dart';
import 'package:sigalogin/src/models/schedule.dart';
import 'package:sigalogin/src/models/update.dart';
Expand All @@ -14,6 +15,7 @@ import 'package:sigalogin/src/pages/tabs/schedule_tab.dart';
import 'package:sigalogin/src/pages/update_page.dart';
import 'package:sigalogin/src/repositories/settings_repository.dart';
import 'package:sigalogin/src/repositories/update_repository.dart';
import 'package:sigalogin/src/services/notification_service.dart';
import 'package:sigalogin/src/services/update_service.dart';
import 'package:sigalogin/src/themes/main_theme.dart';
import 'package:sigalogin/src/controllers/student_controller.dart';
Expand Down Expand Up @@ -54,6 +56,7 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
});

if (!widget.afterLogin) _updateStudentDate(init: true);
Provider.of<NotificationService>(context,listen: false).showNotification();
}

@override
Expand Down
125 changes: 125 additions & 0 deletions lib/src/services/notification_service.dart
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
// );
// }


}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 2.3.1+33
version: 2.4.1+35

environment:
sdk: '>=3.0.5 <4.0.0'
Expand Down

0 comments on commit 6ba1640

Please sign in to comment.