r/flutterhelp • • 10d ago

OPEN Can a Flutter app using NotificationListenerService reliably play custom notification sounds for incoming notifications on modern Android?

Can a Flutter app using NotificationListenerService reliably play custom notification sounds for incoming notifications on modern Android?

1 Upvotes

4 comments sorted by

2

u/karmatics29 10d ago

You can't reliably override the system sound for another app's notification, but you can play your own. In onNotificationPosted you have two workable options. First, play your own audio from the service with MediaPlayer or SoundPool using AudioAttributes.USAGE_NOTIFICATION. Do it in native Kotlin, not a Dart isolate, because the service can be alive while no Flutter engine is. Second, call cancelNotification on the original and post your own notification on a channel with your sound. Channel sound is locked after creation, so a changed sound needs a new channel ID, and Android 13+ needs POST_NOTIFICATIONS granted. Reliability is the weak spot: Xiaomi, Oppo and Samsung often kill or unbind listener services, so ask for a battery optimization exemption and call requestRebind when it disconnects. The original app's sound may also still play before you cancel it.

1

u/nation198 8d ago

is there no other kind of workaround?

1

u/DickBrainTime 3d ago

One extra API nuance: `updateNotificationChannel(...)` exists from API 26, but Android's docs say it is only for reflecting a user change made through the listener's UI; the caller must be an associated device or the notification-assistant app. It isn't a general way for an ordinary listener to rewrite another app's sound settings. `getNotificationChannels()` can inspect channels, but doesn't provide an unrestricted write path. API reference: https://developer.android.com/reference/android/service/notification/NotificationListenerService

If your app doesn't qualify for that role, the non-root path is still to ask the user to silence the source channel (if available) and play your replacement, or accept the race when cancelling/reposting. That's an Android platform limitation, not Flutter-specific.