I want to show custom notifications on Android using Flutter widgets? I'll like to know if that is possible.
What I've done:
I tried using showing a Flutter widget in an Android Fragment and display that Fragment using RemoteViews for custom Android notifications.
A notification shows but it does not include the Flutter widget. See screenshot below:
Code:
var newFlutterFragment: FlutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
.shouldAttachEngineToActivity(false)
.build()
if (fragmentManager != null) {
fragmentManager
.beginTransaction()
.add(
R.id.fragment_container,
newFlutterFragment,
TAG_FLUTTER_FRAGMENT
)
.commit()
}
val notificationLayout = RemoteViews(packageName, R.layout.activity_layout)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.activity_layout)
var builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_bg_service_small)
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
var notificationId = 1;
with(NotificationManagerCompat.from(this)) {
// notificationId is a unique int for each notification that you must define
notify(notificationId, builder.build())
}
In order for Flutter widgets to work, there should be a Flutter framework working. It works only for regular views. In the remote view, there is no such possibility because it is rendered by the Android system itself but not the app, and it does not know about Flutter, which is packaged inside the app but not the system. Moreover, even not all the Android native widgets can be rendered as remote views.
There are some options you can do here, though: