简体   繁体   中英

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. 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. See https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_NONE

It sounds like you want IMPORTANCE_HIGH, to have the notification 'peek'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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