How to keep my flutter app running in the background when close? - android

I am building an app and I want it to show up in the user's notification bar and run in the background even when it's closed. Similar to "KWGT Kustom Widget Maker".
I've tried looking for libraries that allow this, but I can only find those related to java. Are there any available for Dart/Flutter or any other workaround? I am only targeting android devices.

I used a package called flutter_background , which worked for me perfectly.

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.
For more information and a geofencing example that uses background execution of Dart code, see Executing Dart in the Background with Flutter Plugins and Geofencing, an article in the Flutter Publication on Medium. At the end of this article, you’ll find links to example code, and relevant documentation for Dart, iOS, and Android

After searching, I guess you have two solution:
You can use flutter wrapper for both android and IOS platform, but you won't be able to run jobs often than every 15 minutes. If you want to schedule jobs more often, you'll need to write platform dependent code, using the android_alarm_manager flutter package and background_fetch for IOS.
You can trigger the correct one using the Platform function in dart.

You should use Isolates for this, also you can use android_alarm_manager or background_fetch libraries

You can use the flutter_workmanager plugin.
It's better than the android_alarm_manager plugin since it uses newer API's on Android and also works with iOS.

Related

Adding native Android code to a Flutter app

I'm trying to add native android code to an already existing Flutter app, that does something quite different.
So I'd just like to have all of that functionality on top of my current app.
The native android app is using Bluetooth to connect to an external device and is collecting data periodically.
I know, that there is the possibility to call native code via platform channels, but it seemed to me, that you'd have to call that code every time you want to use it? Is this a reasonable way to implement the app or should it be rewritten in Dart? What's the best practice here?
Hope you can help me, thanks a lot!
The purpose of platform channels is to call platform-specific APIs.
...you'd have to call that code every time you want to use it...
Correct.
...Is this a reasonable way to implement the app or should it be rewritten in dart? What's the best practice here...
Check the source code of any package that relies on platform channels (for example camera, file picker, etc.) and read the official documentation. As a short summary, platform-specific code is written in Kotlin/Java (Android), Swift/Objective-C (iOS) or C/C++. Dart is used for building the UI and communicating with said code via platform channels and messages.

How can I run dart code in the background in flutter?

I want to run a method in my main.dart file from the android(Kotlin) part of my app. For this I am using methodChannels and it works perfectly fine when the app is in foreground. However, the moment the app is killed, the flutterView is destroyed and therefore I am unable to run any methods in my main.dart file.
I read that using FlutterNativeView would allow me to do what I want but I am unable to find small code snippets to help me with it.
It would be great if you could help me with this
Thanks!!
In Flutter, you can execute Dart code in the background.
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.
For more information and a geofencing example that uses background execution of Dart code, see the Medium article by Ben Konyi, Executing Dart in the Background with Flutter Plugins and Geofencing. At the end of this article, you’ll find links to example code, and relevant documentation for Dart, iOS, and Android.
You can write the native code for Firebase realtime database. You can check this.
You should create a custom plugin and put the native code in that plugin as writing the files directly in the Flutter android folder will make the project unmanageable in the future and you can face multiple unexpected issues.

Flutter - Is there anything similar to sync adapter in flutter

I wanted to sync my local DB with my server. In Android we have sync Adapter that does the work and make life easier. I would wonder if we have something similar in Flutter or how can I do that in flutter.
Sync-adapters are core Android components. Flutter cannot directly provide a replacement for them. However, flutter does provide a mechanism to use platform specific APIs called flutter plugin. And although their are a lot of plugins already present, their is still a bit lack of plugins providing such core platform support.
Ideally, in an android application, the sync adapters are background services which can be triggered by the OS itself in a separate process, so the simplest way would be to do the following :
Create a separate sync-adapter inside the android directory of your flutter app. Make sure you correctly follow all the requirements for the sync-adapter to work. The documentation is in here.
Create a simple plugin-mechanism using this to invoke android APIs corresponding to the sync-adapter.
Also note that all this code - Android sync-adapter, flutter-plugin and your flutter-app, can simply reside in current code base. You are NOT REQUIRED to create a separate "plugin" to actually use the plugin.

Pass data to Android Background Service on a Native Android Application using Phonegap?

So here's the background story:The phonegap application is an online e-store has a plugin (which my team has created) to print receipts after the orders has been completed. All is working good but the printing is taking too long on Phonegap. In fact, it is taking 5-6 seconds per print, which is not good -- the client expects a much faster process of printing, somewhere down to 1-2 seconds.However, this is not possible using Phonegap, so I had to resort to using native applications. I created a native app with the help of a sample code. Eureka! The printing took only 1-2 seconds!On a side note, both Phonegap and the Native App are using the same Printer library -- the code for printing is entirely the same, just that the Native App's performance is better than Phonegap's.Here is where the problem lies:1. The third-party native app should ONLY run in the background (Hence Android Background Services)2. Phonegap should be able to pass the order data to this third-party native app's Android Background Service so that it will be able to print the receipt.So the question is: How do I do this?What I've currently tried is that I used a phonegap plugin (Link). It works, however, it opens the third-party app, which isn't good at all. I can close it immediately after it opens, but what the client wants is that it doesn't open up at all, to avoid confusion with their customers.Any help is greatly appreciated! If there are any clarifications on what needs to be done, please add comments. Thanks!
I fear that it is not possible to create a background service in cordova/phonegap but you can implement you supporting (Native)app with BroadcastReceiver or IntentService which(Runs in backgroun without UI) gets triggered by the (explicit)intent which you call from the cordova plugin.
More on < Service (Deep Guide), IntentServices, BroadcastReceiver > (Native) and invoking intent using cordova webIntent (plugin).
I guess this tutorial may help you.
Check this article explaining js to native code calling (or plugin development).
Create one test app, which will implement all steps explained in above article. Once implemented, you will get to know how js to native code call is executed.
Now, add printer library to your code (native wrapper), then add your code for printing (in one function, again in native code). Call this function from js. That's it.

How to run a background process using Worklight?

Can you please guide me how to run a background process/service while using Worklight in Android? I have looked at many places but found nothing helpful regarding this.
I'll assume you're asking about running a background process when the app is in the background. The problem is that JavaScript in PhoneGap does not run when the app is in the background. To provide background process capability you'll need to create a plugin to manage the background process.
http://docs.phonegap.com/en/2.2.0/guide_plugin-development_index.md.html
There used to be some documentation from Worklight about creating PhoneGap plugins to be used in Worklight apps but I can't seem to find it.
There is already an existing plugin to help manage background processes in Android
https://github.com/phonegap/phonegap-plugins/tree/master/Android/BackgroundService
Technically speaking, it sounds like the flow you describe requires running a plugin from the javascript code, and starting a service from within the plugin.
The async issues here could be a bit tricky - as the return "from" the plugin to the js is async, but also the thread itself (service) inside the native code.
So wouldn't it be enough to run your native code synchronously ? (this would be synchronous "relative" to the native but async "relative" to the js)
android service - http://developer.android.com/guide/components/services.html
If you're talking about JavaScript part of the application you can detach a thread using setTimeout(function(){...}, 0);. Thats the closest thing to background thread JavaScript have to provide. You can, of course, look into web workers, but that will make things a bit more complex.

Categories

Resources