Disable the popup notification using Koltin on Android with NotificationListenerService

453 Views Asked by At

I want to Disable the notifications similar to DND mode, but without using it, I am using NotificationListenerService and it works great the notification not show up in the notification list on my phone and also no sound using this code

override fun onNotificationPosted(sbn: StatusBarNotification) {
        // Intercept the notification
        if (MainActivity.BLOCK_ALL) {
            val notification = sbn.notification

            // Cancel the notification visual display
            cancelNotification(sbn.key)

            // Check if the notification has sound
            if (notification.flags and Notification.FLAG_INSISTENT == Notification.FLAG_INSISTENT) {
                // If the notification has sound, mute it
                notification.sound = null
                notification.defaults = 0
                notification.flags = notification.flags and Notification.FLAG_INSISTENT.inv()

                // Re-notify the modified notification
                val notificationManager =
                    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                val uniqueId = sbn.key.hashCode() // Convert the String key to an integer
                notificationManager.notify(uniqueId, notification)
            }
        }

    }

but i have and issue i don't want the notification popup for few seconds to also not show, so what i should edit in my code if something like this is possible to make it more clear here is a screenshot of what i want to block.

popup notification

so how can I block this so it did not popup like this and just make the mobile never show anything while my service is running?

Thank you.

1

There are 1 best solutions below

6
Venus On

To disable popup notifications using Kotlin on Android with NotificationListenerService, you can follow these steps:

#1. Create a new class that extends NotificationListenerService.

class MyNotificationListener : NotificationListenerService() {
    // ...
}

#2. Override the onNotificationPosted method to intercept the notifications when they are posted.

override fun onNotificationPosted(sbn: StatusBarNotification?) {
    // Check if the notification is a popup notification
    if (sbn?.notification?.priority ?: 0 >= NotificationCompat.PRIORITY_HIGH) {
        // Cancel the notification
        cancelNotification(sbn?.key)
    } else {
        // Pass the notification to the super class
        super.onNotificationPosted(sbn)
    }
}

You check if the priority of the notification is higher than or equal to PRIORITY_HIGH, which is the priority level used by popup notifications. If it is, we cancel the notification using the cancelNotification method. If the priority level is lower than PRIORITY_HIGH, you pass the notification to the super class using super.onNotificationPosted.

#3. You need to declare the BIND_NOTIFICATION_LISTENER_SERVICE permission, which is required to use NotificationListenerService. you also declare our MyNotificationListener service, and specify that it requires the BIND_NOTIFICATION_LISTENER_SERVICE permission. You also add an intent-filter with the NotificationListenerService action.

#4. Start the MyNotificationListener service by calling startService from your app's main activity or another appropriate location.

startService(Intent(this, MyNotificationListener::class.java))

Please try these steps and let me know. I think it will help you. Thank you.