Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startForeground() always shows a popup on Samsung 8.0 device

I encountered a problem with Samsung Galaxy S7 Edge showing a popup notification every time I use startForeground() in my foreground service for status update in its notification.

Firstly, this issue was present on all Android 8.0 devices but was easily fixed with my notification channel priority set to IMPORTANCE_LOW. But the problem remains for the Samsung device.

So, the question is, how do I update my foreground service notification silently on Samsung 8.0+ devices?

My code is as follows.

Application class:

override fun onCreate() {
        super.onCreate()
        //other code
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannels(listOf(
                    //
                    NotificationChannel(MY_NOTIFICATION_CHANNEL, "My channel", NotificationManager.IMPORTANCE_LOW).apply {
                        setSound(null, null)
                    }
                    //
            ))
        }
    }

Service starting foreground:

val notification = NotificationCompat.Builder(this, MY_NOTIFICATION_CHANNEL)
                .setSmallIcon(android.R.drawable.stat_sys_warning)
                .setColor(App.context().resources.getColor(R.color.primary_dark))
                .setContentText(if (/*logic*/) "This" else "That")
                .addAction(0, if (enable) "Disable" else "Enable", firstIntent)
                .addAction(0, "Another action", secondIntent)
                .build()

        startForeground(MY_NOTIFICATION_ID, notification)
like image 578
Roman Samoilenko Avatar asked Jul 26 '18 06:07

Roman Samoilenko


1 Answers

I've been investigating the same. Best you could do is create the notification without a channel. Service starts with nothing in the tray. Seems to work for me.

NotificationCompat.Builder(this)

However, this constructor is deprecated in 26.1.0

like image 64
Bill Avatar answered Sep 23 '22 08:09

Bill