Foreground service example in Flutter
Asked Answered
R

4

7

I am new to Flutter. Can anyone post the link or code for "android foreground service example with a notification"? I googled but didn't get any good example of foreground service.

I'm working on a project that includes an SMS package. This App has to be active on the foreground.

Rapp answered 3/7, 2019 at 4:33 Comment(2)
simply use Context.startForegroundServicePaneling
Thank you, could you give me an example about that. I'm looking for an example of flutter.Rapp
R
5

Update:

I have created my own implementation of android foreground service for Flutter called android_long_task. check it out it solved my problem. let me know if it solves your problem by raising an issue.I created my own library because the tutorial I mentioned just shows how to create a foreground service in java and that's all and as you have found out by now the other foregorund_service plugins have some problems.

Old answer (didn't help me):

if you're having a problem with this and none of the libraries mentioned in other answers worked for you. you might wanna give this tutorial from RetroPortal Studio a try. it's a total of 26 minutes which teaches you how to write a working foreground service for flutter android

Reconnoitre answered 29/1, 2021 at 19:0 Comment(6)
What was the problem with the RetroPortal tutorial? Is it worth watching?Negus
@Negus I wanted my dart code to run in foreground-service. basically that video just tells you how to create a foreground service in java and that's it. I created a plugin myself and you can simply run your dart code. read the docs. it'll worth your timeReconnoitre
Many thanks for your response. I need a way of guaranteeing that some code is run on a periodic basis. I'm currently using Workmanager and disabling battery optimisation and, whilst it seems to work for me, I gather that isn't always reliable, and neither is AlarmManager. Is your pub.dev foreground service package a suitable basis to start some kind of timer-driven task from?Negus
foreground service is either for running long running tasks. that is why i named it android long task. or running a task non stop. now you can run foreground service non stop. and have your code run on a periodic basis. if that's the case, android_long_task will work for you. or maybe you could use alarm manager to launch foreground_service to run a long running task.Reconnoitre
I started playing around with your project last night. I don't have enough knowledge of Android internals to quite figure out how it works under the hood, but externally it seems quite nice, though I haven't tested it against Doze mode irl yet. I've also opened an issue on it so we can discuss over there, if you would like, some ideas on how it might be made unstoppable?Negus
for bypassing doze mode you must ask user to exclude your app from battery optimization in settings. there is a plugin for thatReconnoitre
F
4

I tried flutter_foreground_service but it has a big problem. When app closes this plugin throwing error. This bug is reported in here: https://github.com/pauldemarco/foreground_service/issues/1

foreground_service has problem too: https://github.com/pauldemarco/foreground_service/issues/1

Probably there isn't a good solution for now (Jan 2020). If someone develops a good solution may be I add it to here.

I think Google should find solution for this kind of problems. We're using flutter becouse we're thinking that flutter is more effective. If we lost our time kind of this absurd errors why we using flutter? Using kotlin for android, swift for ios this is more good probably.

Furtherance answered 23/1, 2021 at 19:56 Comment(0)
C
3

Pauldemarco has created some excellent flutter packages for flutter. He has developed one for Foreground service too. As of now, it is only supported in android. It is not supported for iOS. I hope this helps.

https://github.com/pauldemarco/foreground_service

Circumpolar answered 16/7, 2019 at 8:22 Comment(0)
C
2

You can use library flutter_foreground_task for long or always running task.

Corkscrew answered 24/3, 2022 at 7:54 Comment(2)
Hi, My requirement is send gps and bluetooth scanning periodically every one minute . Does this plugin help me to run in background for more than 6 hours. Please helpOverseer
Yes, It will for Flutter and code should be in Flutter only. If your service is in Android / iOS native code then you might need to find another workaroundCorkscrew

© 2022 - 2024 — McMap. All rights reserved.