简体   繁体   English

Android 通知不弹出

[英]Android notification doesn't pop up

I'm trying to create a simple Android program (in Kotlin) which should display a notification after 10 seconds.我正在尝试创建一个简单的 Android 程序(在 Kotlin 中),它应该在 10 秒后显示通知。 I can see the notification after opening the quick settings and scrolling down, but it doesn't pop up at the top automatically.打开快速设置并向下滚动后我可以看到通知,但它不会自动弹出在顶部。

This is the relevant code:这是相关代码:

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())
    }
}

I've solved the problem by turning "Floating notifications" on我通过打开“浮动通知”解决了这个问题

 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)

    }

If it helps you, accept the answer please.如果对你有帮助,请采纳答案。

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

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