Flutter background task when app is being used - flutter

This question is about the flutter framework.
I am looking for information about how to schedule background tasks in the app while the app is being used by the user. It is not necessary to run the task also when the whole app is in the background / closed.
To explain, an example:
User starts the app and goes to a news article. The page is finished rendering and the user starts reading. While the user is reading I want the app to do tasks in the background such as checking whether there is new data that can be written to the database or maybe that avatar's from other user have update and it should be updated on the user's phone.
Does anybody know where to look for this?

If it's a simple task, you may use Timer:
https://api.flutter.dev/flutter/dart-async/Timer-class.html
But it can't broadcast messages, so you should either use a provider or if it's time-critical and it's a heavy task you should use isolates:
https://api.flutter.dev/flutter/dart-isolate/Isolate-class.html
If you are unfamiliar with isolates you can use ready-made packages, like this one : https://pub.dev/packages/flutter_isolate

Related

what is a simple way to create an app that runs in both background and foreground in flutter?

I am trying to create a flutter app that sends notification every time there is new entry in stream builder, it works fine when the app is on or opened but I want it to work and receive notification even when it is not opened , I have used workmanager to make the task work in background but it is not working ...the code for it can be found here code
Flutter apps by nature are supposed to run on a UI thread or foreground. The background tasks you are asking for are features disclosed by each individual platforms.
In case of Android, you can create a a Background Service to accomplish the tasks you need to perform when application is closed. The link to the bacground service is as follows: Creating a Background Service
In case of iOS, see this link for creating a background task. Other platform implementation will be as like above, so please have a look into each platform specific background process.
UPDATE:
Please have a look into the package Flutter Background Service. This package helps to deal with background tasks setup from flutter side even when app is closed.
NOTE:
Q: Why the service not started automatically?
A: Some android device manufacturers have a custom android os for example MIUI from Xiaomi. You have to deal with that policy.
Q: Service killed by system and not respawn?
A: Try to disable battery optimization for your app.

Using Isolates in Flutter

I am trying to understand how I can create a background process in a Flutter Application. Basically, My understanding is that once a user has started the application, the Isolate can be created. This will allow my code to run on the background.
In the background, I want to get a list of Bluetooth enabled devices and their RSSI values within a certain radius, every 30 seconds or so.
I am aware that there are some Bluetooth libraries available for Flutter, but Time is really limited and It would be best to seek advice from experienced flutter developers.
Specifically, I would like to know the following -
Is using Isolates the best way to go about doing this? From my research, I understand that the user has to start the Isolate through some UI Activity?
Could the Isolate(Background Service) repeat every periodically to perform certain tasks?
I need to be able to write the data received from the Bluetooth scan to a database. I understand that the Isolate will not be the best place for this? Based on this blog post by Ben Konyi, it is not good practice to do such tasks within an Isolate.

iphone: backgrounded app sync webservice

I know this question may be a little bit common and over asked but I cannot find any precise information... so :
Is it possible to have some kind of thread running when the app is in background so I can perform basic sync with my webservice ?
App is in background : I mean the user clicked the Home button, or switched to another app
basic sync : photo upload and download with AFNetworking. I know it has method to continue an HTTPRequest while app is in background, but this is not my point.
My goal would be to make some kind of sync manager, reading a list of photos to update created while the user was on the app, and perform those changes.
I know that the manager could be killed by the OS, but since my server uses atomic transfers it is not a problem. I just need a way to relaunch it... Push ?
I think apps like Google Latitude or Mail and those kind of apps uses what I am looking for but I cannot find any relevant details on it. And using iOS5 is not a problem but waiting for iOS6 would not be a solution.
Thank you for your replies !
PS : well I almost forgot. the app is designed for an enterprise program, so maybe rules are different ? I don't think there is any check for in-house deployment so it might lead to new possibilities...
Apple's Mail client has a background daemon which keeps it running but you can't have that with your own applications. Once an app enters a background state, it must halt it's operations. You can request for a little more time when backgrounded to finish off any transfers or writes to disk (see the Executing a Finite-Length Task in the Background section on Apple's Multitasking Guide)
Google Latitude has events generated based on location. This is a special type of backgrounding introduced by Apple for certain types of applications (see Implementing Long-Running Background Tasks section on Apple's Multitasking Guide) but this can't be used for HTTP syncing. It can only be used for audio, location, voip, newstand content, bluetooth and external hardware attachments.
Push doesn't seem like a solution because it only generates an alert. It doesn't trigger any action until the user triggers the opening of the notification.
You'll want to read Tech Note 2277 Networking and Multitasking.
Basically you have a couple of options:
If you can convince Apple that your app is a VoIP app then you can register a VoIP socket and the OS will resume your background app whenever there is activity on that socket.
Your main option though is to register a background task for any outstanding activity that you have to do when your app is put in the background. You typically get 10 minutes to finish up that work.
Mail is a special app with privileges you don't get.
Apps like Latitude typically register themselves for location updates, specifically to be woken up when there are major geo-position changes. Apps that record GPS tracks do similar things.
Found it !
Using Suhail Patel 's link on Apple's Multitasking Guide I added the voip tag to UIBackgroundModes in Info.plist and use setKeepAliveTimeout:handler: method of UIApplication to relaunch it if needed once the app is going to sleep.
I hope this will help a lot of you !
Of course this app won't be allowed to be on the App Store but for in house development this is in my opinion the best way to do so.
Thanks everyone for showing me the right direction !

iPhone Application Permission

I want to know if it is possible to have two separate applications in iOS Domain where
1) One application is the background application
2) Other acts like Foreground application
The foreground application will use background application for fetching data, data processing and maintaining its database. The user will need to install both applications on his device and for this it should be like this that if user installs one app (say foreground or UI app) it will automatically guide user to install another app. Have heard that something like of this sort is possible in android and applications like voice recognization etc does that.
Also the background application will start running as soon as we boot our device and foreground will launch only after user launches it by clicking on the icon.
Possible advantages in my mind
scenario where one can come with more than one foreground apps. So both of foreground applications can use same background applicatons for maintaining database. This way there will be only one app maintaining database for two different foreground apps on a single device
The background will be the main app will be doing all heavy lifting such as fetching data from server, checking update at regular time intervals and maintaining data base.
Not sure if something of that sort is possible in iOS and is permissible on app store?
Thanks
I've never heard of something like this, especially not on an iOS device.
I don't think it will be accepted for one reason: two different testing teams will test each app. Because the apps require each other, neither app would function. If prompted to install the "other" app, testers would be unable to do so, because it would not be published, because the other team was unable to test it, because the original app was not published. (run on sentence).
It would be like a circle...
My thoughts...
What you are saying could be down with one app and multiple classes and in fact is a fairly common design pattern. You set up one class as the data model, and use background threads and process to keep the data up to date. The other class handles the user interaction on the main thread. Do a few searchs on Model View Controller design pattern and check the link below
https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

Update my app when it is in background

I want to update some data to my application. Consider the application is in the background state, it is neither Voip or Music or GPS. Is it possible to update/send data to the application which is in background?
NOTE: I dont want to notify the user so that the application becomes active.
Can anyone help me ??
The answer is yes and no.
Apple does allow you app to complete a lengthy process in the background. But if you does not fall in the Voip, music or GPS category then you can't run in background.
If for example you want to send some data to a server, which could take some time, then you can mark that process to back executed until it is finished (or 10min. have passed).
You will find some about Executing a Finite-Length Task in the Background
There is not way to run timers or any thing like that in the backgroud, you can only finish a task you started before the app is backgrounded.
The alert which is displayed is an inbuilt functionality. You can't do anything for that. If a notification is fired from the server and application is in background then the alert will be displayed.
I have done a lot of search in past for this stuff.
I have done this in one of my work. this is what i did.
when application enter : - (void)applicationDidEnterBackground:(UIApplication *)application
I send data to server using ASIHTTPRequest with property :
[request setShouldContinueWhenAppEntersBackground:YES];
But after finished, i didn't do anymore connection or data manipulation. So, only the connection is running at background and not your app. you can't do much after the connection finish.
As #rckoenes was mentioned, you may not execute task too long.
If you would like to update server data while your app is running in the background mode, the application should be active at that time. It can only be active if it uses "music, or voip, or location tracking", otherwise the app will be paused in background mode.
One way to avoid this is to develop your application, and to set it to use, for example, «location tracking». This will allow it to meet the requirements for active background process and you will be able to update server data.
Unfortunately, I do not know whether the app can pass app store approval with this set-up.
However, if you are interested in this solution, you can find an example here.