Kotlin Android - Notification not appearing as heads up

Viewed 454

(My device is running Android 5.1.)

Hello,

For an app I've making, I've created a type of notification class, with methods, etc., which make it easy to create notifications.

I've tried to configure it to use a 'heads-up' notification, although it has not been working for me. It just appears as a regular notification: maybe I have gotten something wrong?

Notification helper class and interface:

interface NotificationHelper {
        fun startNotification(title: String, text: String, priority: Int, smallIcon: Int)
    }
    
    class DefaultNotificationHelper(private val context: Context) : NotificationHelper {
        private val channelID = "channel_01"

        private fun registerNotificationManagerWithSystem(channel: NotificationChannel) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
            }
        }

        private fun createNotificationChannel() {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                val notificationChannelInstance = NotificationChannel(channelID, "Channel 1", NotificationManager.IMPORTANCE_HIGH).let {
                    it.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
                    it.setShowBadge(true)

                    registerNotificationManagerWithSystem(it)
                }
            }
        }

        private fun createNotificationInstance(title: String, text: String, priority: Int, smallIcon: Int): NotificationCompat.Builder {
            return NotificationCompat.Builder(context, channelID)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setPriority(priority)
                    .setDefaults(DEFAULT_VIBRATE)
                    .setDefaults(DEFAULT_SOUND)
                    .setSmallIcon(smallIcon)
        }

        private fun notify(notificationCompat: NotificationCompat.Builder) {
            (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(1, notificationCompat.build())
        }

        override fun startNotification(title: String, text: String, priority: Int, smallIcon: Int) {
            createNotificationChannel()
            notify(createNotificationInstance(title, text, priority, smallIcon))
        }
    }

Implementation:

val notificationHelper = DefaultNotificationHelper(this)
        notificationHelper.startNotification("Hello", "Hello", NotificationCompat.PRIORITY_HIGH, R.drawable.fire_gradient)

Appreciate your help - it could be something related to my phone though.

Cheers,

Tom Joney

Edit: I've determined that it's just an issue with my phone, it doesn't matter how hard I try - it will never work. Don't get a Huawei Y6 Elite if you want to test heads-up notifications, it WON'T work no matter how hard you try.

1 Answers
Related