我在android O +中有一个问题(我在P上对其进行了测试),我可以看到正在创建我的通知,但是它没有弹出,因此如果我不亲自打开状态栏,我将永远不知道我得到了任何通知。这是我的代码:
@JvmStatic
fun createNotification(context: Context,
iconResource: Int,
title: String,
description: String,
intent: PendingIntent?,
soundUri: Uri?) {
val manager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.cancelAll()
val builder = NotificationCompat.Builder(context)
.setSmallIcon(iconResource)
.setContentTitle(title)
.setContentText(description)
.setAutoCancel(true)
if (intent != null) {
builder.setContentIntent(intent)
}
val color = context.resources.getColor(R.color.primary)
builder.color = color
val r = Random()
val notificationId = r.nextInt(10000) + 1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val name = "hugge"
val channelDescription = "Hugge notification"
val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
channel.description = channelDescription
channel.enableLights(true)
channel.lightColor = Color.GREEN
channel.enableVibration(true)
if (soundUri != null) {
val attributes = AudioAttributes.Builder()
.build()
channel.setSound(soundUri, attributes)
}
builder.setChannelId(NOTIFICATION_CHANNEL_ID)
manager.createNotificationChannel(channel)
} else {
if (soundUri != null) {
builder.setSound(soundUri)
} else {
builder.setDefaults(Notification.DEFAULT_ALL)
}
}
manager.notify(notificationId, builder.build())
}
@RequiresApi(Build.VERSION_CODES.O)
fun createNotificationChannel(context: Context, channelId: String, channelName: String): String {
val chan = NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE)
chan.lightColor = Color.BLUE
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
val service = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(chan)
return channelId
}
我从其他班级这样称呼它:
val intent = Intent(context, intentClass)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
NotificationUtil.createNotification(
context,
android.R.drawable.arrow_down_float,
rideTitle,
rideMessage,
pendingIntent,
null
)
有人可以告诉我我需要做什么。我已经搜索过了,但是找不到任何解决方案。我在某处读到它与优先级有关,但是我正在设置它,所以我被卡住了。
答案 0 :(得分:0)
问题在于您何时创建通知频道
val chan = NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE)
您将重要性设置为IMPORTANCE_NONE,这意味着不会弹出通知。参见https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_NONE
听起来您想让IMPORTANCE_HIGH收到通知“窥视”