what is a simple way to create an app that runs in both background and foreground in flutter? - 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.

Related

Flutter - background process - how best to package a process that can started in multiple ways

I have a question about how best to structure a background process in a Flutter mobile app that can be triggered in a number of ways.
This background process has to run a web API call, or series of web API calls. The process will take 1 to 5 seconds to complete.
I want the process to be initiated when any of the following occurs:
When the app first starts
When the user initiates a 'refresh' by clicking on a button in the UI
Every hour
Any suggestions on how to structure this in a Flutter app?
You should take a look at this package: https://pub.dev/packages/flutter_background_service
It can run code in background and/or in foreground either on iOs and Android.
Background and foreground are run in isolate, so you should use mechanism provide by package to exchange data between your application and background/foreground.

Flutter background Api fetch

Actually I was trying to implement background process (fetch data from api) even the app is terminated in my flutter project how can I achieve in both Android & iOS platforms.
Have you ever wanted to execute Dart code in the background—even if your app wasn’t the currently active app?
The mechanism for this feature involves setting up an isolate.
Isolates are Dart’s model for multithreading, though an isolate
differs from a conventional thread in that it doesn’t share memory
with the main program. You’ll set up your isolate for background
execution using callbacks and a callback dispatcher.
Example
but if you want do that when app terminated, you will have to wakeup app through Workmanager or some another approach like sending firebase push notification or wakeup app in scheduled time with flutter alarm manager or something like that

how to use network connectivity plugin in background process in flutter

Is it possible to use connectivity plugin in background process?
I don't know how to call dart code in background there is little about this in docs.
I need this functionality to sync offline data with backend whenever there is a connection wether app is in foreground or background and app is in killed state like the way Whatsapp syncs messages.
You can use existing plugins for background execution (on android & ios usually in a roughly 15 minute interval)
https://pub.dev/packages/background_fetch
There is also a article dedicated to using isolates for background executions
https://flutter.dev/docs/development/packages-and-plugins/background-processes
https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124

Fire Chrome Alarms from background service when using cca Google Chrome apps

Experimenting with Mobile Chrome apps API: ( https://github.com/MobileChromeApps/mobile-chrome-apps )
What I'm trying to achieve is the registering of my chrome alarms without having to crudely start the apps' main activity as this causes a bad user experience.
The challenge:
I have specified a chome.alarm which gets successfully registered when I open the app ('cca run android ' for example). These alarms are specified in a background.js file, which is declared as a background script in the manifest.json file.
I wanted these to be registered automatically on boot, therefore I implemented a background service and thought I could simply call the app to register the alarms specified in the background.js file. However, the only way I can find thus far to achieve this, is via a call to launch the apps main activity via an Intent from within the background service as explained in more detail below.
On boot, my background service simply invokes:
//Launch main activity..
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.company.appname");
startActivity(LaunchIntent);
The above launches the app into its main activity and the aforementioned chrome.alarms are registered as desired. Happy days! A massive downside to this, of course, is that no user wants the full blown main activity window to open on boot.
Please advise if there is a less clunky way to have the chrome.alarm specified in background.js registered at boot by a background thread. I do not wish to open the mainActivity of the app, yet this is currently the only way thus far I can see to get them to be registered. It is preferable not to register these alarms from within the Android SDK as I intend to stay as close to using web APIs and want to use background.js as the hook to do this keeping things in-line with chrome web apps as-well-as the android platform.
What I have done:
A cca (chrome mobile web app) app which registers a background service natively (I extend BackgroundService) as a plugin and register this to run at boot time. This is the only 'native android code' in order to load the app at boot-time.
Upon booting android, this background service then simply issues a startActivity(LaunchIntent) in order to open the app. In doing so, my chrome.alarm specified in my background.js file get registered successfully. If I don't do this, my chrome alarm specified in background.js do not get registered until I manually open the apps main activity. Therefore I use the backgroundService to open the apps main activity.
This is not at all ideal, I would simply like to somehow have my chrome.alarms registered without crudely having to launch the apps main activity window.
Is there a call which I can place in my BackgroundService to have only the chrome.alarms contained in my background.js file read and registered to fire?
Thank you for your time.
First, thanks for using Chrome Apps for Mobile! (Note: I work on the project)
Sorry that our current bootstrap is "crude" and "clunky", and we agree that it is. You can track this issue for progress on our effort to replace our current Background-scripts-embedded-in-main-Activity-webview strategy with a real background service and multiple window support. Basically you've summarized exactly our plan and existing limitations.
This work is actually scheduled for the near term, and will certainly be done before the end of the year.
As well as chrome.alarms, this feature is quite important for chrome.gcm and chrome.tcpServer (among other reasons).

phonegap background service in iOS5

I am writing update checker program in xcode, my program needs background process( it needs to be run in background), so is it possible in phonegap, and is there any phonegap plugin for background services? Thanks
iOS does not allow background task to run more than a specific time limit.
Similar Post:
iPhone background task stops running at some point
Explanation:
http://www.macworld.com/article/1164616/how_ios_multitasking_really_works.html
The reasonable solution would be to implement a push notification and send a notification whenever there is an update on server.
It is definitely possible to run some JS code in the background to be checking this (https://github.com/jocull/phonegap-backgroundjs).
It won't be possible to do it for more than 10 minutes in the background, though (Run app for more than 10 minutes in background)
So you'll need both a combination of the first plugin I mentioned and the PN service that dhaval is suggesting.
Cheers!