How to Add Custom Sound Notifications in Flutter for iOS
Ever wanted your app to stand out with a distinctive alert tone? On iOS, the system only plays the sound you bundle with the notification, so a custom audio file can make a huge difference. This guide walks you through the whole process—from preparing the sound file to wiring it up in Flutter—without losing sight of the little quirks that often trip developers.
Why Custom Sounds Matter on iOS
Apple restricts notifications to the sounds shipped with your app, which means a generic “ding” can feel bland. A well‑chosen tone reinforces branding, signals urgency, or simply adds a splash of personality. Users tend to notice and remember alerts that sound different, boosting engagement in subtle ways.
Prerequisites
- Flutter ≥ 2.5 installed
- Xcode 12 or newer
- A valid iOS development certificate
- The
flutter_local_notificationsplugin added topubspec.yaml - A short (< 30 seconds) audio file in
.cafor.wavformat
Setting Up the Sound File
First things first: you need a sound that iOS will actually accept. Not every codec works, and the file size matters—you’ll want something crisp but lightweight.
Choosing the Right Format
Apple prefers .caf because it handles a variety of sample rates gracefully. If you start with an .mp3 or .aac, convert it using afconvert or a GUI tool like Audacity. A quick terminal command looks like this:
afconvert -f caff -d LEI16@44100 -c 1 original.wav custom_sound.cafIf you’re not comfortable with command‑line tools, the same conversion can be done in iTunes by exporting the track as “Apple Lossless”.
Adding to Xcode Project
Open the ios folder of your Flutter project in Xcode. Drag the .caf file into the Runner target, making sure Copy items if needed is checked. Xcode will prompt you to add the file to the appropriate build phases—just hit “Finish”.
Once inside Xcode, verify that the file appears under Build Phases → Copy Bundle Resources. This step guarantees the sound ships with the final app binary.
Configuring Flutter Code
Now that the audio lives in the iOS bundle, it’s time to tell Flutter to use it when a notification fires. The flutter_local_notifications plugin abstracts most of the boilerplate, but you’ll still need a few iOS‑specific tweaks.
Initialize Plugin for iOS
In your main.dart, set up the plugin with the appropriate initialization settings. Note the requestSoundPermission flag—without it, iOS will silently ignore your custom tone.
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =FlutterLocalNotificationsPlugin();
const IOSInitializationSettings iosSettings = IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
await flutterLocalNotificationsPlugin.initialize(
InitializationSettings(iOS: iosSettings),
onSelectNotification: (payload) async {
// handle tap
},
);
That snippet ensures the app asks the user for permission to play sounds right up front.
Define Notification Details with Custom Sound
When you schedule a notification, specify the sound name exactly as it appears in the Xcode bundle (including the extension). Here’s a minimal example:
const IOSNotificationDetails iosDetails = IOSNotificationDetails(sound: 'custom_sound.caf',
);
const NotificationDetails platformDetails = NotificationDetails(
iOS: iosDetails,
);
await flutterLocalNotificationsPlugin.show(
0,
'Reminder',
'Don’t forget your meeting at 3 PM',
platformDetails,
);
If the file isn’t found, iOS falls back to the default tone—so double‑check spelling and case sensitivity.
Testing the Notification
Run the app on a real iOS device (simulators sometimes skip custom sounds). Trigger the notification via a button or a scheduled timer. You should hear the custom audio play instantly, even if the app is in the background.
If you don’t hear anything, pause the app, close Xcode, and clean the build folder (Shift + Cmd + K). Re‑build; this often resolves hidden caching issues.
Troubleshooting Common Issues
- Sound not playing on locked screen: Ensure the audio file is short (under 30 seconds) and marked as “public” in the Xcode file inspector.
- Wrong file name: iOS is case‑sensitive. “Custom_Sound.caf” won’t match “custom_sound.caf”.
- App crashes after adding the file: Verify the file is correctly added to
Copy Bundle Resourcesand not duplicated in the project hierarchy. - Notification delivered but silent: Check that
requestSoundPermissionwas granted; you can prompt the user again if needed.
With the steps above, you now have a fully functional custom‑sound notification system in your Flutter iOS app. A little extra polish can go a long way, and the effort is surprisingly manageable once you’ve got the basics down.