What is the best way to execute a function in the future in Flutter on Android only

Viewed 22

I am using the firebase cloud messaging function onBackgroundMessage in my flutter app.

https://pub.dev/documentation/firebase_messaging/latest/firebase_messaging/FirebaseMessaging/onBackgroundMessage.html

Whenever this function is triggered, I want to queue code to execute 3 minutes in the future. This all is running in the background with my app terminated, so I am wondering what is the best way.

Can I just call...

Future.delayed(Duration(minutes: 3));

Or is it possible that this will not reliably execute.

Should I use a library like workmanager for this?

1 Answers

if you want to use in background try Workmanager can help you in android/ios both

Workmanager().registerOneOffTask(
  "task-identifier",
  simpleTaskKey, // Ignored on iOS
  initialDelay: Duration(minutes: 30),
  constraints: Constraints(
    // connected or metered mark the task as requiring internet
    networkType: NetworkType.connected,
    // require external power
    requiresCharging: true,
  ),
  inputData: ... // fully supported
);

After Trigger initialDelay: Duration(minutes: 30),

Related