From 5d67b7ea8af1c852d5be6f0bfe71b90745f3530f Mon Sep 17 00:00:00 2001 From: Willian <47341070+wjrcode@users.noreply.github.com> Date: Tue, 18 Oct 2022 17:48:43 -0300 Subject: [PATCH] =?UTF-8?q?notifica=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/src/main/AndroidManifest.xml | 9 +++ lib/Controller/TarefaController.dart | 16 ++++ lib/Model/API/ResponseAPIModel.dart | 7 +- lib/SplashView.dart | 14 ++++ lib/components/selectData.dart | 4 +- lib/main.dart | 4 +- .../notificacao/notification_api.dart | 74 +++++++++++++++++++ pubspec.lock | 40 ++++++++-- pubspec.yaml | 2 + 9 files changed, 161 insertions(+), 9 deletions(-) create mode 100644 lib/services/notificacao/notification_api.dart diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 5cb4816..e9fa3eb 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -33,6 +33,12 @@ + + + + + + + + + diff --git a/lib/Controller/TarefaController.dart b/lib/Controller/TarefaController.dart index 6474472..1bc4f35 100644 --- a/lib/Controller/TarefaController.dart +++ b/lib/Controller/TarefaController.dart @@ -1,10 +1,14 @@ import 'dart:ffi'; +import 'dart:io'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart'; +import 'package:intl/intl.dart'; import 'package:organizei/Model/Tarefa/TarefaModel.dart'; import 'package:organizei/Controller/Base/Base.dart'; import 'package:organizei/Repository/TarefaRepository.dart'; +import 'package:organizei/services/notificacao/notification_api.dart'; import 'package:organizei/services/persistencia/login.configuracoes.dart'; class TarefaController extends Base { @@ -62,6 +66,11 @@ class TarefaController extends Base { await Future.delayed(const Duration(milliseconds: 500)); if (value.valido!) { + await NotificationApi.showScheduledNotification( + id: value.id!, + title: 'Oiê! tá na hora tá na hora de ' + model.nome!, + body: '', + scheduledDate: model.data); return value.valido!; } else { return false; @@ -93,6 +102,11 @@ class TarefaController extends Base { await Future.delayed(const Duration(milliseconds: 500)); if (value.valido!) { + await NotificationApi.showScheduledNotification( + id: model.id ?? 0, + title: 'Oiê! tá na hora tá na hora de ' + model.nome!, + body: '', + scheduledDate: model.data); return value.valido!; } else { return false; @@ -142,6 +156,7 @@ class TarefaController extends Base { await Future.delayed(const Duration(milliseconds: 500)); if (value.valido!) { + NotificationApi.cancelScheduleNotification(model.id ?? 0); return value.valido!; } else { return false; @@ -180,6 +195,7 @@ class TarefaController extends Base { await Future.delayed(const Duration(milliseconds: 500)); if (value.valido!) { + NotificationApi.cancelScheduleNotification(model.id ?? 0); return value.valido!; } else { return false; diff --git a/lib/Model/API/ResponseAPIModel.dart b/lib/Model/API/ResponseAPIModel.dart index 73b977d..5b8bf4c 100644 --- a/lib/Model/API/ResponseAPIModel.dart +++ b/lib/Model/API/ResponseAPIModel.dart @@ -1,11 +1,15 @@ class ResponseAPIModel { bool? valido; String? msg; + int? id; int? statusCode; - ResponseAPIModel({this.valido, this.msg, this.statusCode}); + ResponseAPIModel({this.id, this.valido, this.msg, this.statusCode}); ResponseAPIModel.fromJson(Map json) { + if (json['id'] != null) { + id = json['id'] != null ? int.parse(json['id']) : null; + } valido = json['valido']; msg = json['msg']; } @@ -15,6 +19,7 @@ class ResponseAPIModel { data['valido'] = valido; data['msg'] = msg; data['statusCode'] = statusCode; + data['id'] = id; return data; } diff --git a/lib/SplashView.dart b/lib/SplashView.dart index abdbe26..a5f3f3b 100644 --- a/lib/SplashView.dart +++ b/lib/SplashView.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:organizei/Controller/SplashController.dart'; +import 'package:organizei/home_page.dart'; +import 'package:organizei/services/notificacao/notification_api.dart'; class SplashView extends StatefulWidget { const SplashView({Key? key}) : super(key: key); @@ -11,6 +13,18 @@ class SplashView extends StatefulWidget { class _SplashViewState extends State { var controller = SplashController(); + @override + void initState() { + //super.initState(); + NotificationApi.init(); + listenNotifications(); + } + + void listenNotifications() => + NotificationApi.onNotifications.stream.listen(onClickedNotification); + void onClickedNotification(String? payload) => Navigator.of(context) + .push(MaterialPageRoute(builder: (context) => HomePage())); + @override Widget build(BuildContext context) { return FutureBuilder( diff --git a/lib/components/selectData.dart b/lib/components/selectData.dart index 42ebae3..4092aec 100644 --- a/lib/components/selectData.dart +++ b/lib/components/selectData.dart @@ -86,11 +86,13 @@ class _SelectDataState extends State { widget.onSaved(newDate.toString()); + DateTime hora = new DateTime.now(); + if (widget.tipo != 'data') { final time = await showTimePicker( context: context, initialTime: - TimeOfDay(hour: date.hour, minute: date.minute), + TimeOfDay(hour: hora.hour, minute: hora.minute), builder: (BuildContext context, child) { return Theme( data: ThemeData.light().copyWith( diff --git a/lib/main.dart b/lib/main.dart index 14dea7f..0ec01b6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -6,10 +6,12 @@ import 'package:intl/intl.dart'; import 'package:organizei/start_page.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:timezone/data/latest.dart' as tz; //import 'home_page.dart'; -void main() { +void main() async { + tz.initializeTimeZones(); runApp(MyApp()); } diff --git a/lib/services/notificacao/notification_api.dart b/lib/services/notificacao/notification_api.dart new file mode 100644 index 0000000..cd04544 --- /dev/null +++ b/lib/services/notificacao/notification_api.dart @@ -0,0 +1,74 @@ +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:intl/intl.dart'; +import 'package:rxdart/rxdart.dart'; +import 'package:timezone/timezone.dart' as tz; + +class NotificationApi { + static final _notifications = FlutterLocalNotificationsPlugin(); + static final onNotifications = BehaviorSubject(); + + static Future _notificationDetails() async { + return const NotificationDetails( + android: AndroidNotificationDetails( + 'channel id', 'channel name', 'channel description', + icon: 'logo', importance: Importance.max), + iOS: IOSNotificationDetails(), + ); + } + + static Future showNotification( + {int id = 0, String? title, String? body, String? payload}) async => + _notifications.show( + id, + title, + body, + await _notificationDetails(), + payload: payload, + ); + + static Future init({bool initScheduled = false}) async { + final android = AndroidInitializationSettings('logo'); + final iOS = IOSInitializationSettings(); + final settings = InitializationSettings(android: android, iOS: iOS); + + await _notifications.initialize(settings, + onSelectNotification: (payload) async { + onNotifications.add(payload); + }); + } + + static Future showScheduledNotification( + {int id = 0, + String? title, + String? body, + String? payload, + required String? scheduledDate}) async => + _notifications.zonedSchedule( + id, + title, + body, + tz.TZDateTime.from( + new DateFormat('dd/MM/yyyy HH:mm').parse(scheduledDate ?? ''), + tz.local), + // _scheduleDaily(Time(8)), + await _notificationDetails(), + payload: payload, + androidAllowWhileIdle: true, + uiLocalNotificationDateInterpretation: + UILocalNotificationDateInterpretation.absoluteTime, + matchDateTimeComponents: DateTimeComponents.time); + + static tz.TZDateTime _scheduleDaily(Time time) { + final now = tz.TZDateTime.now(tz.local); + final scheduleDate = tz.TZDateTime(tz.local, now.year, now.month, now.day, + time.hour, time.minute, time.second); + + return scheduleDate.isBefore(now) + ? scheduleDate.add(Duration(days: 1)) + : scheduleDate; + } + + static void cancelScheduleNotification(int id) async { + await _notifications.cancel(id); + } +} diff --git a/pubspec.lock b/pubspec.lock index 104916e..cf21cf0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -56,7 +56,7 @@ packages: name: device_info_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.0" fake_async: dependency: transitive description: @@ -90,6 +90,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + flutter_local_notifications: + dependency: "direct main" + description: + name: flutter_local_notifications + url: "https://pub.dartlang.org" + source: hosted + version: "7.0.0" + flutter_local_notifications_platform_interface: + dependency: transitive + description: + name: flutter_local_notifications_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.1" flutter_localizations: dependency: "direct main" description: flutter @@ -188,7 +202,7 @@ packages: name: path_provider_android url: "https://pub.dartlang.org" source: hosted - version: "2.0.20" + version: "2.0.9" path_provider_ios: dependency: transitive description: @@ -209,14 +223,14 @@ packages: name: path_provider_macos url: "https://pub.dartlang.org" source: hosted - version: "2.0.6" + version: "2.0.3" path_provider_platform_interface: dependency: transitive description: name: path_provider_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "2.0.4" + version: "2.0.0" path_provider_windows: dependency: transitive description: @@ -237,7 +251,7 @@ packages: name: plugin_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "2.1.2" + version: "2.1.3" process: dependency: transitive description: @@ -245,6 +259,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.2.4" + rxdart: + dependency: "direct main" + description: + name: rxdart + url: "https://pub.dartlang.org" + source: hosted + version: "0.27.5" shared_preferences: dependency: "direct main" description: @@ -348,6 +369,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.12" + timezone: + dependency: transitive + description: + name: timezone + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.0" typed_data: dependency: transitive description: @@ -378,4 +406,4 @@ packages: version: "0.2.0+2" sdks: dart: ">=2.17.0-0 <3.0.0" - flutter: ">=2.8.1" + flutter: ">=2.8.0" diff --git a/pubspec.yaml b/pubspec.yaml index c162e0c..c397b06 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -32,6 +32,8 @@ dependencies: flutter_localizations: sdk: flutter intl: + flutter_local_notifications: ^7.0.0 + rxdart: ^0.27.1 # The following adds the Cupertino Icons font to your application.