Implementing a Sticky Service in android/flutter
Asked Answered
I

3

13

I need to add a native sticky background service in a flutter application, in order to achieve 2 things:

  1. Starting at boot time and running in background indefinitely
  2. Exchange data with the main Dart activity, in a message passing fashion

However, I cannot find any kind of useful documentation. It seems that for now, you have to choose to go completely native or giving up using low level features and focus only on the UI (until someone pulls a specific plugin out of the hat).

Thus, my question is the following: what is the easiest way to achieve this sort of integration, starting with a basic flutter project ?

Thank you

Isallobar answered 8/2, 2021 at 20:24 Comment(7)
Maybe create a background process that saves to SharedPrefs which will be the medium for sharing the data from the service to the UI? Or you could use your own network API for sharing the data if that makes more sense for your use case => medium.com/vrt-digital-studio/flutter-workmanager-81e0cfbd6f6eButene
Well, yes but actually no. This service will keep a permanent websocket connection. The message passing systems should be used to pass messages from the UI to the websocket and viceversaIsallobar
iOS likely won't guarantee an indefinite process. You've got a window of opportunity to do things. Android, you might be able to get away with it. You could possibly execute a Future from a Background process that returns only once the socket is closed. If you never close the socket, the Future never returns and maybe the Flutter code keeps on executing "indefinitely." I've never tried this myself to prove it out.Butene
Yes, the idea is definitely something like that. However, let's consider the example of an IM app like Telegram or Whatsapp. How can they manage the online status and the message delivery system ? There must be somewhere a background process that handles the network communication/notification feature.Isallobar
I imagine IMs use background processes to check-in with a server. The server sends out a notification. When the device activates the app's listener, it will pop the notification.Butene
Yes, but in order to receive the notification, the client needs a permanent connection or a similar concept. I want to implement something similar from scratch without using firebase or similarIsallobar
Look into Services. The problem is that you still need to do some native configuring/coding to make it work. This article focuses on Android. Search more like it for iOS (which may or may not have an interface with flutter idk) => medium.com/codechai/flutter-and-services-638ebfbbe47fButene
A
1
  1. Make a Sticky Service using native Android service.
  2. The easiest way to exchange data with the main Dart activity is to use deep links or intents.

Note: if you explain more why do you need that, I think I may be able to give you a better solution.

Apeak answered 30/9, 2022 at 23:38 Comment(0)
L
1
  1. While you can register a BroadcastReceiver to be activated at BOOT your idea of having a service "running in background indefinitely" is highly discouraged in recent version of Android. Therefore what can you do is have a Broadcast receiver registered in Manifest that will be activate when BOOT completes (See https://developer.android.com/guide/components/broadcasts for a sample) and from here you can use the available API to schedule work. Check out this link to see what from available API best suite you needs.

  2. While there are more complicated solutions you will find that simply sending Intents between components will do the job.

Leodora answered 2/10, 2022 at 17:17 Comment(0)
T
0

You can use the method channel Docs : https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-swift-tab

Twice answered 28/12, 2022 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.