Can't silence notification on Android API29

Viewed 92

Need to silence a notification that is shown by a foreground service. To silence the notification I have used many suggested solution from SO. But nothing seems to silence the annoying notification sound. It is silenced on SDK25. But on SDK 29 emulator it is not silenced. What can be the solution?

Code:

//service

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

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    createNotificationChannel()

    val notificationIntent = Intent(this, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        this,
        0, notificationIntent, 0
    )
    val notification: Notification =
        NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("app_name")
            .setContentText("Updating database.")
            .setSmallIcon(R.drawable.logo)
            .setContentIntent(pendingIntent)
            .build()
    startForeground(1, notification)

    thread() {
        Thread.sleep(3000)
        stopSelf()
    }

    return START_NOT_STICKY
}


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

private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = SettingsHelper.KEY_NOTIFICATION_channel_name_service
        val description = SettingsHelper.KEY_NOTIFICATION_CH_ID_SERVICE
        val channel = NotificationChannel(
            SettingsHelper.KEY_NOTIFICATION_CH_ID,
            name,
            NotificationManager.IMPORTANCE_NONE
        )
        channel.description = description
        channel.setSound(null, null)

        val manager = getSystemService(
            NotificationManager::class.java
        )

        manager.createNotificationChannel(channel)
    }
}
1 Answers

Could you use setSilent() from NotificationCompat.Builder

If true, silences this instance of the notification, regardless of the sounds or vibrations set on the notification or notification channel. If false, then the normal sound and vibration logic applies. Defaults to false.

Note, that if you are changing notification settings, I recommend you to uninstall and re-install the app everytime since it might not be updated the settings you set in the new run.

Related