38

I am developing a Flutter app for academic purposes in university. Now, I need to run a sort of "always on" background service in order to use Bluetooth and most importantly to get periodic updates from external REST APIs.

I've already checked those two libraries from pub.dev (this and this) without finding the perfect solution that works out-of-the-box (as expected...). I also saw something native with Kotlin (here). What I would to know is what is the best option in order to achieve my goals (in terms of best practices, completeness and with simplicity in mind).

Have a look at the following image for a more schematic view:

enter image description here

Amir
  • 2,225
  • 2
  • 18
  • 27
docdev
  • 943
  • 1
  • 7
  • 17

4 Answers4

9

You might want to have a look at flutter_background_service package. It helps executing Dart code in the background.

Jago
  • 2,751
  • 4
  • 27
  • 41
8

Can I run Dart code in the background of an Flutter app?

Yes, you can run Dart code in a background process on both iOS and Android. For more information, see the Medium article Executing Dart in the Background with Flutter Plugins and Geofencing.

Credit to Raouf Rahiche

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • 23
    Thank you for your reply. I've already seen that article on Medium. It is interesting, however I was wondering if it wasn't too complicated compared to what I need to do. – docdev Jun 04 '20 at 19:07
  • Is this can survive how the native handle the process? what if Android kills the background application process? – Jongz Puangput Oct 26 '20 at 13:27
  • @JongzPuangput the process is not running all the time and will be killed after a short period. There are two options on Android how a process can be launched: either it is a periodic task which is initialised by the Android OS or it is triggered by an Android Intent. – niceman Jan 06 '21 at 08:45
6

For this kind of things you have to rely on native mechanisms.

The most efficient way to deal with APIs in an "always existent" background component is to implement a push notification service. Using that kind of tech a remote server is capable of starting communication with your app, awaken it and then perform whatever task needs to be performed.

Also, in Android, you have foreground services which will run even if the app is closed.

svprdga
  • 2,171
  • 1
  • 28
  • 51
  • 1
    Thank you very much for your reply. I was quite afraid that to get what I want it was necessary to implement a native solution. So now the problem is the further complexity in doing that kind of stuff... Where should I look to get an idea without running into overly complicated things? – docdev Jun 04 '20 at 19:05
  • Maybe the fastest way is to implement the push notifications mechanism with a tool like Firebase, so you don't have to manage all the server infrastructure by yourself. Then look at pub.dev if there is any package to register to push notifications...if it's not you will have to implement the native code by yourself in both iOS & Android... – svprdga Jun 05 '20 at 08:03
  • Ok thanks. The problem is that we already have a Spring REST backend to use for our project. So firebase is not an option. – docdev Jun 05 '20 at 14:43
  • I was just suggesting Firebase for the ease and speed, but if you already have a backend then you can use it for the notifications as well. – svprdga Jun 06 '20 at 08:43
  • 1
    You can also use this package https://pub.dev/packages/background_fetch – Md omer arafat Feb 24 '21 at 05:41
  • @docdev you can use the firebase admin sdk to send the push notifications via the server, while using the flutter firebase messaging sdk you can capture those messages. – Ankur Datta Jul 26 '21 at 05:25
2

I recommend using android_long_task plugin which has a good documentation

if you want to run a long running task using an Android Foreground Service and start to listen to the update and progress of your task running in the that service. also you can update the foreground service notification however you want.

alireza easazade
  • 3,324
  • 4
  • 27
  • 35