Я пишу приложение Android в Kotlin, которое имеет текущее уведомление. Проблема в том, что он исчезает, когда приложение убивается. Мне пока не удалось найти рабочее решение этой проблемы, поэтому вся помощь приветствуется!
Вот мой код:
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val builder = NotificationCompat.Builder(this, "all_notifications")
.setContentTitle("remember your intention")
.setContentText("\"" + intentionTxt.text + "\"")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_notification)
.setOngoing(true)
.setContentIntent(pendingIntent)
.setAutoCancel(false)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "all_notifications"
val mChannel = NotificationChannel(
channelId,
"General Notifications",
NotificationManager.IMPORTANCE_DEFAULT
)
mChannel.description = "Default Notification Channel"
val mContext = this.applicationContext
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
with(NotificationManagerCompat.from(this)) {
notify(1, builder.build())
}
Версия SDK: 28, тестирование в Android 9 (P ie) эмулятор