Thursday, 24 March 2022

How do I show a custom Android notification using Flutter widgets?

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:

Screenshot showing a notification without any content

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())
 }


from How do I show a custom Android notification using Flutter widgets?

No comments:

Post a Comment