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

标签 java android kotlin push-notification android-notifications

我正在从 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/

相关文章:

java - Android - 如何使屏幕覆盖上的按钮可点击

java - 更好的性能,SQLite 查询与内存中的 ArrayList<Object>

Android 到 ASP.NET 上传保存路径

android - Kotlin JaCoCo,无覆盖 -> IllegalClassFormatException ...请提供原始的非仪器类

java - dataSnapshot.getValue 的 Android Studio Kotlin 错误

java - 如何从 char 中减去字符 '0' 将其更改为 int?

java - 在 eclipse 中离线安装 google app engine 插件

java - 在长轮询的情况下, Atmosphere 框架会在服务器上抛出错误

java - 使用Java的File类以及如何获得正确的访问权限来创建文件

websocket - Kotlin 和 WebSocket