简体   繁体   English

正在创建通知,但未在Android中弹出(在Pie中)

[英]Notification is being created but not popping up in android (in Pie)

I have a problem in android O+ (i tested it on P) that i can see my notification being created but it doesn't pop up so if i don't physically open the status bar i would never know that i got any notification. 我在android O +中有一个问题(我在P上进行了测试),我可以看到正在创建通知,但是它没有弹出,所以如果我不亲自打开状态栏,我将永远不知道我收到了任何通知。 Here's my code: 这是我的代码:

@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
}

and i call it from other class like this: 我从其他班级这样称呼它:

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
    )

Can someone please tell me what do i need to do. 有人可以告诉我我需要做什么。 I've searched about it but couldn't find any solution. 我已经搜索过了,但是找不到任何解决方案。 I read somewhere that it has something to do with priority but i am setting that so i am stuck. 我在某处读到它与优先级有关,但是我正在设置它,因此我陷入了困境。

The issue is when you make your notification channel 问题是当您创建通知频道时

val chan = NotificationChannel(channelId,
        channelName, NotificationManager.IMPORTANCE_NONE)

You set importance to IMPORTANCE_NONE, meaning that the notification doesn't pop up. 您将重要性设置为IMPORTANCE_NONE,这意味着不会弹出通知。 See https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_NONE 参见https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_NONE

It sounds like you want IMPORTANCE_HIGH, to have the notification 'peek' 听起来您想让IMPORTANCE_HIGH收到通知“窥视”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM