我正在从Firebase控制台推送通知。我能够轻松接收通知数据,并且通知也会出现,但是我想做的是更改通知的小图标和大图标。
我正在使用这两种方法,但似乎都没有用。我还尝试通过Vector
使用res>New>Vector>Clip Art
选项制作小图标。
小图标既不会出现,大图标也不会出现,并且通知也无法展开。
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
创建的图形对象
答案 0 :(得分:0)
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())
答案 1 :(得分:0)
您可以尝试使用ic_notif.png代替矢量。
除此之外,在最新的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)
}
答案 2 :(得分:0)
这是工作示例:
以这种方式调用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()
}
}
,您可以静态或动态设置大图标。
{{1}}