-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
161 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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,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); | ||
} | ||
} |
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