在我的应用中,当使用以下代码接收到FCM时,我会显示一条通知:
val intent = Intent(this, SplashActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = "CHANNEL_ID"
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification) //THis is an XML Drawable
.setContentTitle("Title")
.setContentText("Some notification body.")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"Human Readable Channel Name",
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
我注意到一个小问题。当应用程序在前台运行或在后台运行时,setSmallIcon(R.drawable.ic_stat_ic_notification)
会在通知的左上方显示可绘制对象。
但是,当应用程序未运行时(我将其从正在运行的应用程序中删除),将显示通知,但图标仅显示为白色圆圈。
有人可以帮助我解决这个问题-为什么会发生,如何解决?