【问题标题】:How to show large icon in notification如何在通知中显示大图标
【发布时间】:2022-08-09 15:02:03
【问题描述】:

通知的附件图像中有一个小图标和一个大图标。我想创建一个类似的通知。我可以使用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
                )
            )

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

    标签: android push-notification notifications android-notifications


    【解决方案1】:

    第一种方式(这就是你想要的)

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

    第二种方式

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

    【讨论】:

    • 我已经用我的代码更新了这个问题。我尝试使用您的两种方法,但不知何故它们不起作用。
    • 问题出在我使用的图标上,我不确定为什么 svg 不起作用。将其更改为 png 对我有用。
    • 我有同样的问题,svg 对我也不起作用。我还没有尝试过制作png。 png 有用吗
    【解决方案2】:

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

    有趣的 getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap >{ val drawable = ContextCompat.getDrawable(context!!, drawableId) val 位图 = Bitmap.createBitmap( 可绘制!!.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888 ) val 画布 = 画布(位图) drawable.setBounds(0, 0, canvas.width, canvas.height) drawable.draw(画布) 返回位图 }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多