Java SetMallIcon()和setLargeIcon()在Android通知中不工作
我正在从Firebase控制台推送通知。我能够轻松地接收通知数据,通知也会出现,但我正在尝试更改通知的小图标和大图标 我正在使用这两种方法,但它们似乎都不起作用。我还尝试通过Java SetMallIcon()和setLargeIcon()在Android通知中不工作,java,android,kotlin,push-notification,android-notifications,Java,Android,Kotlin,Push Notification,Android Notifications,我正在从Firebase控制台推送通知。我能够轻松地接收通知数据,通知也会出现,但我正在尝试更改通知的小图标和大图标 我正在使用这两种方法,但它们似乎都不起作用。我还尝试通过res>New>Vector>Clip-Art使用Vector选项制作小图标。 小图标和大图标均不显示,通知也不可扩展 MessagingService.kt class MessagingService():FirebaseMessagingService(){ 覆盖收到的消息(p0:RemoteMessage?){ su
res>New>Vector>Clip-Art
使用Vector
选项制作小图标。
小图标和大图标均不显示,通知也不可扩展
MessagingService.kt
class MessagingService():FirebaseMessagingService(){
覆盖收到的消息(p0:RemoteMessage?){
super.onMessageReceived(p0)
showNotification(p0!!.notification!!.title!!,p0!!.notification!!.body!!)
}
娱乐节目通知(标题:字符串,正文:字符串){
val icon=BitmapFactory.decodeResource(资源、,
R.drawable.iphn)
NotificationCompat.Builder(此“MyNotifications”)
.setLargeIcon(图标)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle(标题)
.setContentText(正文)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(图标)
.bigLargeIcon(空))
.build()
}
}
ic\u notif
是我使用Vector
创建的可绘制图形。您可以尝试使用ic\u notif.png而不是Vector
private var mBuilder: NotificationCompat.Builder? = null
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val intent = Intent(activity, MainActivity::class.java)
val pi = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val icon = BitmapFactory.decodeResource(resources, shapemyapp.admin.R.drawable.iphn)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val notificationChannel = NotificationChannel("ID", "Name", importance)
mNotificationManager.createNotificationChannel(notificationChannel)
mBuilder = NotificationCompat.Builder(this, "MyNotifications")
.setLargeIcon(icon)
.setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
.setContentTitle(title)
.setContentText(body)
.build()
} else {
mBuilder = NotificationCompat.Builder(activity)
.setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
}
mBuilder!!.setContentIntent(pi)
mNotificationManager.notify(NotificationID.id, mBuilder!!.build())
除此之外,在最新的Android版本中,建议使用channelId。可以添加此块以添加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)
}
下面是一个工作示例: 调用
sendNotification(yourResponse)
在onMessageReceived(remoteMessage:remoteMessage?)
中,这样可以静态或动态设置大图标
private fun sendNotification(response: NotifyResponse) {
var bmp: Bitmap? = null
try {
//loading the image from server
// if (!TextUtils.isEmpty(response.image)) {
// val futureTarget = GlideApp.with(this).asBitmap().load(response.image).submit()
// try {
// bmp = futureTarget.get()
// GlideApp.with(this).clear(futureTarget)
//
// } catch (e: ExecutionException) {
// e.printStackTrace()
// } catch (e: InterruptedException) {
// e.printStackTrace()
// }
//
// }
val uniqueInt = (System.currentTimeMillis() and 0xfffffff).toInt()
val pendingIntent = PendingIntent.getActivity(this, uniqueInt, getIntent(response.type), PendingIntent.FLAG_ONE_SHOT)
val channelId = BuildConfig.FLAVOR
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(notificationIcon)
.setContentTitle(response.title)
.setContentText(response.message)
.setAutoCancel(true)
.setSound(soundUri)
.setPriority(NotificationCompat.PRIORITY_HIGH)
if (bmp != null) {
notificationBuilder.setLargeIcon(bmp)
notificationBuilder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bmp).bigLargeIcon(null))
}
notificationBuilder.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(BuildConfig.FLAVOR,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0, notificationBuilder.build())
} catch (o: Exception) {
o.printStackTrace()
}
}
我正在
Pie
中运行此代码。它不应该工作吗?