Skip to content

Commit

Permalink
#20 test scheduled notification
Browse files Browse the repository at this point in the history
  • Loading branch information
floodoo committed Jan 22, 2022
1 parent 989dac6 commit 3f2a4a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/core/service/notification.service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

class NotificationService {
static final NotificationService _notificationService = NotificationService._internal();
Expand Down Expand Up @@ -28,6 +30,8 @@ class NotificationService {
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
);

tz.initializeTimeZones();
}

void sendAndoidNotification({required String title, required String body}) async {
Expand All @@ -39,4 +43,20 @@ class NotificationService {
payload: 'data',
);
}

Future<void> scheduleAndroidNotification({required String title, required String body}) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
body,
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
platformChannelSpecifics,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
);
}

void cancelNotification() async {
await flutterLocalNotificationsPlugin.cancel(0);
}
}
8 changes: 7 additions & 1 deletion lib/ui/screens/time_table/time_table.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,16 @@ class TimeTableScreen extends ConsumerWidget {
),
IconButton(
onPressed: () {
NotificationService().sendAndoidNotification(title: "Test", body: "lol");
NotificationService().scheduleAndroidNotification(title: "Test", body: "lol");
},
icon: const Icon(Icons.notification_add),
),
IconButton(
onPressed: () {
NotificationService().cancelNotification();
},
icon: const Icon(Icons.delete),
),
],
),
drawer: const CustomDrawer(),
Expand Down

0 comments on commit 3f2a4a6

Please sign in to comment.