Sunday, 29 July 2018

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)



from startForeground() always shows a popup on Samsung 8.0 device

No comments:

Post a Comment