我正在尝试创建一个简单的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())
}
}