Skip to content

Commit

Permalink
notificação
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 18, 2022
1 parent 881f821 commit 5d67b7e
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 9 deletions.
9 changes: 9 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand All @@ -42,4 +48,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
16 changes: 16 additions & 0 deletions lib/Controller/TarefaController.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion lib/Model/API/ResponseAPIModel.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json) {
if (json['id'] != null) {
id = json['id'] != null ? int.parse(json['id']) : null;
}
valido = json['valido'];
msg = json['msg'];
}
Expand All @@ -15,6 +19,7 @@ class ResponseAPIModel {
data['valido'] = valido;
data['msg'] = msg;
data['statusCode'] = statusCode;
data['id'] = id;

return data;
}
Expand Down
14 changes: 14 additions & 0 deletions lib/SplashView.dart
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -11,6 +13,18 @@ class SplashView extends StatefulWidget {
class _SplashViewState extends State<SplashView> {
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(
Expand Down
4 changes: 3 additions & 1 deletion lib/components/selectData.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ class _SelectDataState extends State<SelectData> {

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(
Expand Down
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
74 changes: 74 additions & 0 deletions lib/services/notificacao/notification_api.dart
Original file line number Diff line number Diff line change
@@ -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<String?>();

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);
}
}
40 changes: 34 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -237,14 +251,21 @@ packages:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
version: "2.1.3"
process:
dependency: transitive
description:
name: process
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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5d67b7e

Please sign in to comment.