'Notifications are not shown when the app is closed, how can I fix it?

It successfully shows notifications when the app is open. It shows even when the app is in the background. When I close the app it doesn't show up anymore. I don't want to use FCM. because I want to remember this when doing other applications. In a video, she said that the service above Android 8 does not work permanently, and that startForeground () should be used as a solution for this.But I don't want to use it because it creates a permanent notification on top

class MyFirebaseMessagingService : Service() {

private val channelId = "notification_channel"
private val channelName = "com.dombikpanda.doktarasor.service"
private val notificationTitle = "Sorunuz Cevaplanmıştır"
private val notificationMessage =
    "Doktor tarafından sorunuz cevaplanmıştır.Görmek için tıklayınız"
private var importance = 0
private var notifManagerId = 0

override fun onBind(intent: Intent): IBinder? {
    return null
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    questionNotification()
    return START_STICKY
}

override fun onDestroy() {
    super.onDestroy()
}

override fun onCreate() {
    createNotifChannel()
    super.onCreate()
}

private val crudRepository = CrudRepository()
private fun questionNotification() {
    val shared = getSharedPreferences("kontrol", MODE_PRIVATE)
    val control = shared.getLong("date", 0)
    val collection = Firebase.firestore.collection("questions")
    collection
        .addSnapshotListener { value, error ->
            error.let {

            }
            value?.let { result ->
                for (document in result) {
                    if (document["userid"] == crudRepository.getFirebaseAuth().uid) {
                        if (document["cevapdurum"] == true && document["messageDurum"] == true && document["date"] == control) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                createNotification()
                            }
                            break
                        }
                    }
                }
            }
        }
}


private fun createNotifChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        importance = NotificationManager.IMPORTANCE_HIGH //normal high
        val notificationChannel =
            NotificationChannel(channelId, channelName, importance).apply {
                description = notificationMessage
            }
        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager.createNotificationChannel(notificationChannel)
    }
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotification() {
    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent =
        PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
    val notification = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.drawable.ic_stat_name)
        .setContentTitle(notificationTitle)
        .setContentText(notificationMessage)
        .setAutoCancel(true)
        .setOnlyAlertOnce(true)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setVibrate(longArrayOf(1000, 1000, 1000, 1000))
        .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
        .setContentIntent(pendingIntent)
    with(NotificationManagerCompat.from(this)) {
        notify(notifManagerId, notification.build())
        notifManagerId++
    }
}
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source