1

I'd like to update Firestore document every day at 6 AM automatically.
For instance, there is a quote app and update quote to a new one every morning so that the user can see the different quotes.
I don't mind whether executing code on an iOS device or Android because I have both phones.
But I want an app to update the document even if I'm sleeping.
What is the best way of doing this?

Vishal G. Gohel
  • 1,008
  • 1
  • 16
  • 31
Daibaku
  • 11,416
  • 22
  • 71
  • 108
  • These seems like something you might want to do server-side rather than client-side. – Alex Meuer Dec 05 '18 at 15:01
  • Possible duplicate of [How to schedule background tasks in Flutter?](https://stackoverflow.com/questions/51706265/how-to-schedule-background-tasks-in-flutter) – timr Aug 01 '19 at 11:32

2 Answers2

0

Good news it is somewhat possible.

Background Tasks in a nutshell

On Android you would use WorkManager. You can ask the Android framework to schedule your task somewhere in the future (exact hours are not supported).

On iOS you enable Background Fetch in XCode or manually edit the Info.plist file.

<key>UIBackgroundModes</key>
  <array>
    <string>fetch</string>
  </array>
</key>

enter image description here

WorkManager Plugin

Since there is a lot of ceremony to wire everything together there is a handy Flutter plugin which helps you.

The flutter_workmanager plugin supports both WorkManager and performFetch in one unified Dart API.

void callbackDispatcher() {
  Workmanager.executeTask((backgroundTask) {
    switch(backgroundTask) {
      case Workmanager.iOSBackgroundTask:
      case "firebaseTask":
        print("You are now in a background Isolate");
        print("Do some work with Firebase");
        Firebase.doSomethingHere();
        break;
    }
    return Future.value(true);
  });
}

void main() {
  Workmanager.initialize(callbackDispatcher);
  Workmanager.registerPeriodicTask(
    "1",
    "firebaseTask",
    frequency: Duration(days: 1),
    constraints: WorkManagerConstraintConfig(networkType: NetworkType.connected),
  );
  runApp(MyApp());
}
timr
  • 6,668
  • 7
  • 47
  • 79
-1

Sorry, in this case you will need use WorkManager on Android. This is a way to use background service for that case. In iOS I don't know how.

I think that is not possible to create background services in Flutter because it's run in other context.

Ascension
  • 2,599
  • 2
  • 15
  • 13