gpt4 book ai didi

java - setSmallIcon() 和 setLargeIcon() 在 Android 通知中不起作用

转载 作者:行者123 更新时间:2023-11-29 23:07:31 25 4
gpt4 key购买 nike

我正在从 Firebase 控制台推送通知。我能够轻松接收通知数据并且通知也会出现,但我想做的是更改通知的小图标和大图标。

我正在使用这两种方法,但它们似乎都不起作用。我还尝试通过 res>New>Vector>Clip Art 使用 Vector 选项制作小图标。既不显示小图标也不显示大图标,通知也不可展开。

MessagingService.kt

class MessagingService(): FirebaseMessagingService() {

override fun onMessageReceived(p0: RemoteMessage ? ) {
super.onMessageReceived(p0)
showNotification(p0!!.notification!!.title!!, p0!!.notification!!.body!!)
}

fun showNotification(title: String, body: String) {
val icon = BitmapFactory.decodeResource(resources,
R.drawable.iphn)
NotificationCompat.Builder(this, "MyNotifications")
.setLargeIcon(icon)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle(title)
.setContentText(body)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null))
.build()

}
}

ic_notif 是我使用 Vector

创建的可绘制对象

最佳答案

您可以尝试使用 ic_notif.png 而不是 vector 。

除此之外,在最新的Android版本中,推荐使用channelId。你可以添加这个 block 来添加 channelId

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "yourChannelId"
val channel = NotificationChannel(channelId, "your channel Name" ,
NotificationManager.IMPORTANCE_DEFAULT)
mNotificationManager.createNotificationChannel(channel)
mBuilder.setChannelId(channelId)
}

关于java - setSmallIcon() 和 setLargeIcon() 在 Android 通知中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com