如何使用 jetpack compose 制作自定义通知视图?

发布于 2025-01-10 14:23:53 字数 904 浏览 5 评论 0原文

我想制作一个自定义通知视图,其中的内容会定期更新。自定义视图可能包含操作按钮。在android文档中,我们可以使用XML布局文档创建自定义视图: https://developer .android.com/training/notify-user/custom-notification

// Get the layouts to use in the custom notification
val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)

// Apply the layouts to the notification
val customNotification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setCustomBigContentView(notificationLayoutExpanded)
        .build()

那么我们可以使用 jetpack compose 制作自己的自定义通知视图吗?

I wanted to make a custom notification view with content being updated in interval of time. The custom view might contain the action button. In android documentation, we can create custom view using XML layout docs: https://developer.android.com/training/notify-user/custom-notification

// Get the layouts to use in the custom notification
val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)

// Apply the layouts to the notification
val customNotification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setCustomBigContentView(notificationLayoutExpanded)
        .build()

So can we make our own custom notification view using jetpack compose ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2025-01-17 14:23:53

截至目前,我们无法在自定义远程视图布局文件中使用androidx.compose.ui.platform.ComposeView。目前,也没有其他选择

我们实现自定义通知的唯一方法是通过远程视图,但目前,远程视图不支持 ComposeView ...即使它支持它也不值得,因为它不支持所有 API 级别

有关详细信息,请参阅远程视图中支持的小部件/视图

https://developer.android.com/reference/android/widget/RemoteViews

As of now, we can't use androidx.compose.ui.platform.ComposeView in custom remote view layout file. And currently, there is no alternative to it also

The only way we achieve custom notification is via remote views but currently, remote view doesn't support ComposeView ... even if it supports it will not be worth since it wont support all API levels

For more info here are the widgets/views supported in remote views

https://developer.android.com/reference/android/widget/RemoteViews

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文