如何在通知中显示大图标

发布于 2025-01-27 03:01:33 字数 1538 浏览 3 评论 0原文

通知的附件图像中有一个小图标和一个大图标。我想创建类似的通知。我可以使用notificationcompat.builder(context).setsmallicon()来设置小图标。但是我无法设置大图标。任何人都可以帮我吗?

val builder = NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_notification_icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_notification_icon))
            .setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(BitmapFactory.decodeResource(context.resources, R.drawable.ic_notification_icon))
                .bigLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_notification_icon)))
            .setColor(context.getColor(R.color.colorPrimary))
            .setContentTitle(notificationTitle)
            .setContentText(notificationText)
            .setLights(Color.RED, 1000, 1000)
            .setVibrate(longArrayOf(0, 400, 250, 400))
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(
                PendingIntent.getActivity(
                    context,
                    0,
                    Intent(context, SplashActivity::class.java),
                    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
                )
            )

我尝试使用上述代码添加大图标,但它不起作用。

enter image description here

There is a small icon and a large icon in the attached image of a notification. I would like to create a similar notification. I am able to set the small icon using NotificationCompat.Builder(context).setSmallIcon(). But I am not able to set the large icon. Can anyone please help me with this.

val builder = NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_notification_icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_notification_icon))
            .setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(BitmapFactory.decodeResource(context.resources, R.drawable.ic_notification_icon))
                .bigLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_notification_icon)))
            .setColor(context.getColor(R.color.colorPrimary))
            .setContentTitle(notificationTitle)
            .setContentText(notificationText)
            .setLights(Color.RED, 1000, 1000)
            .setVibrate(longArrayOf(0, 400, 250, 400))
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(
                PendingIntent.getActivity(
                    context,
                    0,
                    Intent(context, SplashActivity::class.java),
                    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
                )
            )

I tried adding the large icon using the above code, but it didn't work.

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

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

发布评论

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

评论(2

混吃等死 2025-02-03 03:01:33

第一路(这是您想要的)

.setLargeIcon(BitmapFactory. decodeResource (resources , R.drawable. ic_launcher_foreground ))

第2条

.setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(aBitmap)
                .bigLargeIcon(null))

1st way(this is what you want)

.setLargeIcon(BitmapFactory. decodeResource (resources , R.drawable. ic_launcher_foreground ))

2st way

.setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(aBitmap)
                .bigLargeIcon(null))
泪之魂 2025-02-03 03:01:33

我们可以将SVG转换为位图图像并将其设置为通知图标。

fun getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap {
            val drawable = ContextCompat.getDrawable(context!!, drawableId)
            val bitmap = Bitmap.createBitmap(
                drawable!!.intrinsicWidth,
                drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(bitmap)
            drawable.setBounds(0, 0, canvas.width, canvas.height)
            drawable.draw(canvas)
            return bitmap
        }

We can convert svg to bitmap images and set it to notification icons.

fun getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap {
            val drawable = ContextCompat.getDrawable(context!!, drawableId)
            val bitmap = Bitmap.createBitmap(
                drawable!!.intrinsicWidth,
                drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(bitmap)
            drawable.setBounds(0, 0, canvas.width, canvas.height)
            drawable.draw(canvas)
            return bitmap
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文