我想使用Flutter小部件在Android上显示自定义通知?我想知道这是否可行。
我所做的:
我尝试使用显示Flutter widget in an Android Fragment和显示片段使用RemoteViews for custom Android notifications。
会显示一个通知,但它不包括Flutter小部件。参见下面的屏幕截图:
代码:
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())
}
1条答案
按热度按时间zzoitvuj1#
为了让Flutter小部件工作,应该有一个Flutter框架工作。它仅适用于常规视图。在远程视图中,不存在这种可能性,因为它是由Android系统本身渲染的,而不是应用程序,它不知道Flutter,它封装在应用程序内部,而不是系统内部。此外,即使是not all Android原生小部件也可以呈现为远程视图。
不过,您可以在这里做一些选择: