SystemAlertWindow icon indicating copy to clipboard operation
SystemAlertWindow copied to clipboard

How to show overlay from native code

Open koncafaruk opened this issue 7 months ago • 1 comments

Hi,

Thanks for the package. I’m able to show the overlay using the Dart code you provided. However, I’m not sure how to do the same from Java/Kotlin.

I’ve implemented a custom NotificationListenerService and need to show the Flutter-based overlay when a specific notification is posted. This event might happen before Flutter app started, so I need to trigger the overlay directly from Kotlin code.

Specifically, I want to invoke MethodCallHandlerImpl.onMethodCall() from Kotlin to start the overlay. Could you please guide me on how to achieve this?

Thanks in advance!

koncafaruk avatar Jul 01 '25 12:07 koncafaruk

Since you already have the overlay code and MethodChannel set up on the Dart side, you don’t need to call MethodCallHandlerImpl directly. Instead, just grab cached FlutterEngine and use a MethodChannel to send the event from your NotificationListenerService:

val engine = FlutterEngineCache.getInstance().get("in.jvapps.flutter_cache_engine") if (engine != null) { MethodChannel(engine.dartExecutor.binaryMessenger,"in.jvapps.flutter_cache_engine") .invokeMethod("showSystemWindow", arguments)) }

This will trigger your Dart overlay handler the same way as if you had invoked it from Dart.

Note: the Flutter engine (and its MethodChannel handlers) only exists after your app has been launched at least once, so the Dart side can register the overlay handler. If the Android process is killed, the cached engine is also destroyed — in that case, you’ll need to spin up a new engine inside your service before making the method call.

aryaveer0710 avatar Sep 08 '25 06:09 aryaveer0710