Android уведомление не всплывает - PullRequest
2 голосов
/ 03 мая 2020

Я пытаюсь создать простую Android программу (в Kotlin), которая должна отображать уведомление через 10 секунд. Я вижу уведомление после открытия быстрых настроек и прокрутки вниз, но оно не появляется вверху автоматически.

Это соответствующий код:

lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
val channelId = "com.example.vicky.notificationexample"
val description = "Test notification"

class MyIntentService : IntentService("MyIntentService") {
    override fun onHandleIntent(intent: Intent?) {
        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val intent = Intent (this, LauncherActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
        notificationChannel.enableLights(true)
        notificationChannel.lightColor = Color.GREEN
        notificationChannel.enableVibration(true)
        notificationManager.createNotificationChannel(notificationChannel)

        builder = Notification.Builder(this, channelId)
            .setContentTitle("Some Title")
            .setContentText("Some text")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setLargeIcon(BitmapFactory.decodeResource(this.resources, R.mipmap.ic_launcher_round))
            .setContentIntent(pendingIntent)

        Thread.sleep(10000)
        notificationManager.notify(1234, builder.build())
    }
}

Ответы [ 2 ]

1 голос
/ 04 мая 2020

Я решил проблему, включив «Плавающие уведомления» на

0 голосов
/ 04 мая 2020
 lateinit var notificationManager: NotificationManager
    lateinit var notificationChannel: NotificationChannel
    lateinit var builder: NotificationCompat.Builder
    val channelId = "com.example.vicky.notificationexample"
    val description = "Test notification"

 fun displayNotification(){
        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val intent = Intent (this, LauncherActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH).apply {
                enableLights(true)
                lightColor = Color.GREEN
                enableVibration(true)
            }
            notificationManager.createNotificationChannel(notificationChannel)
        }

        builder = NotificationCompat.Builder(this, channelId).apply {
            setContentTitle("Some Title")
            setContentText("Some text")
            setSmallIcon(R.drawable.ic_launcher_foreground)
            setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher_round))
            setContentIntent(pendingIntent)
        }
        val handler = Handler()
        handler.postDelayed(Runnable {
            NotificationManagerCompat.from(this).notify(1234,builder.build())   }, 10000)

    }

Если это поможет вам, примите ответ, пожалуйста.

...