Omer
Omer

Reputation: 77

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

Upvotes: 2

Views: 1055

Answers (2)

Netanel Izhak
Netanel Izhak

Reputation: 171

 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.

Upvotes: 0

Omer
Omer

Reputation: 77

I've solved the problem by turning "Floating notifications" on

Upvotes: 1

Related Questions