Уведомление создается, но не появляется в Android (в Pie) - PullRequest
0 голосов
/ 06 января 2019

У меня есть проблема в 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
    )

Может кто-нибудь сказать, что мне нужно сделать? Я искал об этом, но не мог найти никакого решения. Я где-то читал, что это как-то связано с приоритетом, но я настраиваю его, поэтому я застрял.

1 Ответ

0 голосов
/ 10 января 2019

Проблема в том, когда вы делаете свой канал уведомлений

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

Вы указали важность IMPORTANCE_NONE, то есть уведомление не появляется. Смотри https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_NONE

Звучит так, как будто вы хотите, чтобы у IMPORTANCE_HIGH было уведомление "peek"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...