'Flutter: How to upload data to firestore database in background when app is closed at a particular time daily
I am trying to upload data to my firestore database at 10:30 am and 6 pm daily whether the app is open or closed in Android, in the open state it can be done in the regular method but how to do it when the app is closed
Solution 1:[1]
You need to utilize Android's WorkManager to do periodic tasks in the background. Here's an existing plugin that does this.
Here is an example of scheduling two recurring jobs that have an initial delay and then repeat every day at the same time. You will need to consult the plugin documentation for how to implement the tasks.
final now = DateTime.now();
final tenThirtyAmDelay = DateTime(now.year, now.month, now.day, 10, 30).difference(now);
final sixPmDelay = DateTime(now.year, now.month, now.day, 18).difference(now);
if (tenThirtyAmDelay.isNegative) {
tenThirtyAmDelay.add(days: 1);
}
if (sixPmDelay.isNegative) {
sixPmDelay.add(days: 1);
}
WorkManager().registerPeriodicTask(
"my1030amTask",
"my1030amTask",
initialDelay: tenThirtyAmDelay,
frequency: Duration(days: 1),
);
WorkManager().registerPeriodicTask(
"my6pmTask",
"my6pmTask",
initialDelay: sixPmDelay,
frequency: Duration(days: 1),
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
