Requirement
I would like to implement a Flutter app, which is:
- Triggered by a broadcast action
ACL_CONNECTED
on Android and then it - Processes information about the connected device in Dart (Flutter) and based on the result it
- Starts a foreground service with a notification, which continues processing and can open the UI if tapped on the notification
Idea
- Add
application/receiver
tag toAndroidManifest.xml
- Implement
BroadcastReceiver/onReceive
, which gives over processing to Dart/Flutter - Continue processing in Dart
Note: using static BroadcastReveiver will allow to skip starting the app and keeping it running in background.
Challenge
It is not clear how to give the processing from Android to Dart/Flutter. If the app is working it is possible using platform channels: MethodChannel.invokeMethod
from Android side and catch it by MethodChannel.setMethodCallHandler
from Flutter side.
In case of static BroadcastReviever no other part of Android or Flutter code is initialized, so it looks like MethodChannel.invokeMethod
is ending up in the void.
Question
How to start Flutter processing from Android BroadcastReceiver?
Similar questions
Which unfortunately are left without clear answer:
- Flutter plugin: Send data from Android BroadcastReceiver to Flutter code
- How to stream data from Broadcast Receiver to Flutter?
- https://stackoverflow.com/a/69232241/10475643
Possible alternative solutions
- Dynamic BroadcastReceiver
Maybe the app can be started after Android starts and registerBroadcastReviever
dynamically and the callback in Dart will be initialized, but also we should be sure, that the app is not killed by the system. - Start the main Flutter app from static BroadcastReceiver
Special parameter can be transferred. If this special parameter is received on app start - use alternative logic - make the required processing and minimize or close the app without even displaying the UI to avoid user distraction.