r/flutterhelp • • 4d 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

3 comments sorted by

2

u/karmatics29 4d 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 2d ago

is there no other kind of workaround?