[英]Notification is being created but not popping up in android (in Pie)
我在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
)
有人可以告訴我我需要做什么。 我已經搜索過了,但是找不到任何解決方案。 我在某處讀到它與優先級有關,但是我正在設置它,因此我陷入了困境。
問題是當您創建通知頻道時
val chan = NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE)
您將重要性設置為IMPORTANCE_NONE,這意味着不會彈出通知。 參見https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_NONE
聽起來您想讓IMPORTANCE_HIGH收到通知“窺視”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.