Binding to Android Foreground Service with JNI and jnigen from Flutter
A short note about accessing foreground service directly from Dart. I'm still here.
The more I work with jni and native APIs from Flutter, the more I think it’s time to move away from methods channels entirely. In this short post I’m going to show how to bind to Android’s foreground service directly from Dart, and send messages back and forth without additional plugin class.
Here’s what I built:
starting and binding to a foreground service from Flutter
requesting and checking for notification permissions directly from Dart
sending messages from Flutter to foreground service
receiving messages from foreground service notification as if it was a messaging app
I first started with more traditional approach of having a plugin class that proxied calls to the foreground service, but after some chats with my friend we realized we can skip this step and bind directly to the service from Flutter.
Let’s recap some useful helpers included in jni:
Jni.androidApplicationContext- needed to start the service and send messages to itjni.Jni.androidActivity(engineId)andPlatformDispatcher.instance.engineId- needed to request notification permissions

