1

Requirement

I would like to implement a Flutter app, which is:

  1. Triggered by a broadcast action ACL_CONNECTED on Android and then it
  2. Processes information about the connected device in Dart (Flutter) and based on the result it
  3. Starts a foreground service with a notification, which continues processing and can open the UI if tapped on the notification

Idea

  1. Add application/receiver tag to AndroidManifest.xml
  2. Implement BroadcastReceiver/onReceive, which gives over processing to Dart/Flutter
  3. 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:

Possible alternative solutions

  • Dynamic BroadcastReceiver
    Maybe the app can be started after Android starts and register BroadcastReviever 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.
mspnr
  • 416
  • 4
  • 12

0 Answers0