background notifications
This commit is contained in:
+59
-51
@@ -1,72 +1,80 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:todoapp/elems/filehandler.dart';
|
||||
import 'package:todoapp/pages/Home.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:todoapp/providers/todoprovider.dart';
|
||||
import 'package:awesome_notifications/awesome_notifications.dart';
|
||||
import 'package:workmanager/workmanager.dart';
|
||||
import 'package:disable_battery_optimization/disable_battery_optimization.dart';
|
||||
import 'package:flutter_background_service/flutter_background_service.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callbackDispatcher() {
|
||||
Workmanager().executeTask((task, inputData) async {
|
||||
AwesomeNotifications().initialize(
|
||||
null,
|
||||
[
|
||||
NotificationChannel(
|
||||
channelKey: "reminder_channel",
|
||||
channelName: "Reminders",
|
||||
channelDescription: "Reminder for ToDo")
|
||||
]);
|
||||
|
||||
Map content = await FileHandler.fileContent;
|
||||
for (String key in content.keys) {
|
||||
Map list = content[key];
|
||||
for (String title in list.keys) {
|
||||
Map current = list[title];
|
||||
DateTime dueTime = DateTime.parse(current["dueTime"]);
|
||||
bool checked = current["checked"];
|
||||
DateTime currentTime = DateTime.now();
|
||||
int timeDiff = dueTime
|
||||
.difference(currentTime)
|
||||
.inMinutes;
|
||||
if (timeDiff <= 30 && timeDiff >= 0 && !checked) {
|
||||
AwesomeNotifications().createNotification(
|
||||
content: NotificationContent(
|
||||
id: 10,
|
||||
channelKey: "reminder_channel",
|
||||
title: "Task due!",
|
||||
body: "Task $title due in $timeDiff minutes!"
|
||||
)
|
||||
);
|
||||
}
|
||||
void callbackDispatcher() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
SharedPreferences.getInstance();
|
||||
print("test1");
|
||||
Map content = await FileHandler.fileContent;
|
||||
for (String key in content.keys) {
|
||||
Map list = content[key];
|
||||
for (String title in list.keys) {
|
||||
print("test2");
|
||||
Map current = list[title];
|
||||
DateTime dueTime = DateTime.parse(current["dueTime"]);
|
||||
bool checked = current["checked"];
|
||||
DateTime currentTime = DateTime.now();
|
||||
int timeDiff = dueTime
|
||||
.difference(currentTime)
|
||||
.inMinutes;
|
||||
if (timeDiff <= 30 && timeDiff >= 0 && !checked) {
|
||||
AwesomeNotifications().createNotification(
|
||||
content: NotificationContent(
|
||||
id: 10,
|
||||
channelKey: "reminder_channel",
|
||||
title: "Task due!",
|
||||
body: "Task $title due in $timeDiff minutes!"));
|
||||
}
|
||||
}
|
||||
return Future.value(true);
|
||||
}
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void onStart(ServiceInstance service) {
|
||||
Timer.periodic(const Duration(seconds: 10), (timer) {
|
||||
callbackDispatcher();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> initService() async {
|
||||
final service = FlutterBackgroundService();
|
||||
|
||||
void main() async {
|
||||
|
||||
|
||||
AwesomeNotifications().initialize(
|
||||
null,
|
||||
[
|
||||
NotificationChannel(
|
||||
channelKey: "reminder_channel",
|
||||
channelName: "Reminders",
|
||||
channelDescription: "Reminder for ToDo")
|
||||
]);
|
||||
|
||||
Workmanager().initialize(
|
||||
callbackDispatcher,
|
||||
isInDebugMode: true
|
||||
await service.configure(
|
||||
iosConfiguration: IosConfiguration(),
|
||||
androidConfiguration: AndroidConfiguration(
|
||||
onStart: onStart,
|
||||
isForegroundMode: false,
|
||||
autoStart: true,
|
||||
autoStartOnBoot: true
|
||||
)
|
||||
);
|
||||
|
||||
Workmanager().registerPeriodicTask("check_nots", "nots_check", frequency: Duration(minutes: 15));
|
||||
// Workmanager().registerOneOffTask("test", "test1");
|
||||
service.startService();
|
||||
}
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await AwesomeNotifications().initialize(null, [
|
||||
NotificationChannel(
|
||||
channelKey: "reminder_channel",
|
||||
channelName: "Reminders",
|
||||
channelDescription: "Reminder for ToDo",
|
||||
importance: NotificationImportance.High,
|
||||
)
|
||||
]);
|
||||
|
||||
await initService();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user