首页 > 解决方案 > 在 Jetpack Compose 中设置大通知图标不起作用

问题描述

我试图在可组合通知的右侧显示一个大图标,但出现类型不匹配错误,但我使用的是.png图像:

Type mismatch: inferred type is Int but Bitmap? was expected

我究竟做错了什么?

@Composable
fun NotificationApp() {
    val context = LocalContext.current
    val channelId = "MyTestChannel"
    val notificationId = 0

    val builder = NotificationCompat.Builder(context, channelId)
        .setSmallIcon(R.drawable.ic_edit_location) 
        .setContentTitle("My Test Notification")
        .setContentText("This is my test notification in one line...")
        .setLargeIcon(R.drawable.header) // error here
        .setStyle(
            NotificationCompat.BigTextStyle()
                .bigText(
                    "This is my test notification in one line. Made it longer " +
                            "by setting the setStyle property. " +
                            "It should not fit in one line anymore, " +
                            "rather show as a longer notification content."
                )
        )
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

    createNotificationChannel(channelId, context)

    with(NotificationManagerCompat.from(context)) {
        notify(notificationId, builder.build())
    }
}

谢谢你的帮助!

标签: androidkotlinandroid-jetpack-compose

解决方案


该方法setLargeIcon适用于Bitmap.

您可以使用:

.setLargeIcon(BitmapFactory.decodeResource(context.resources,R.drawable.header)) 

推荐阅读