Enhancing Your SwiftUI App with Custom Notification Sounds

One of the standout features of modern apps is their ability to engage users through notifications. While visual elements are essential, sound can significantly enhance the user experience. Custom notification sounds add a personal touch to your app, making it more engaging and memorable. In this post, we’ll explore how to implement custom notification sounds in your SwiftUI app.

Setting Up Your SwiftUI Project

First things first, ensure you have a SwiftUI project ready. If you're starting from scratch, create a new SwiftUI project in Xcode. Name your project and set up your development environment. Once you have your project set up, we can dive into the specifics of adding custom notification sounds.

import SwiftUI
import UserNotifications

@main
struct CustomNotificationSoundApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onAppear(perform: requestNotificationPermission)
    }
    
    func requestNotificationPermission() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            if granted {
                print("Notification permission granted.")
            } else if let error = error {
                print("Notification permission denied: \(error.localizedDescription)")
            }
        }
    }
}

Adding Your Custom Sound File

To use a custom sound, you need to add the sound file to your project. Ensure the file is in .wav, .aiff, or .caf format for iOS compatibility. Drag and drop your sound file into the Xcode project navigator. Remember to check the box to copy the file into your project’s resources.

Configuring the Notification

Next, we’ll configure a notification that uses the custom sound. This involves creating a UNNotificationRequest and specifying the custom sound in the UNNotificationContent. Here’s how you can set it up:

import SwiftUI
import UserNotifications

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Tap to Schedule Notification")
                .padding()
                .onTapGesture {
                    scheduleNotification()
                }
        }
    }
    
    func scheduleNotification() {
        let content = UNMutableNotificationContent()
        content.title = "Custom Sound Notification"
        content.body = "This notification uses a custom sound."
        content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "custom_sound.wav"))
        
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        
        UNUserNotificationCenter.current().add(request) { error in
            if let error = error {
                print("Error adding notification: \(error.localizedDescription)")
            } else {
                print("Notification scheduled with custom sound.")
            }
        }
    }
}

In this example, tapping the text schedules a notification that triggers after 5 seconds, playing the custom sound you added to your project.

Testing Your Notification

To test your notification, run your app on a real iOS device. Simulators may not always support sound notifications correctly. After granting notification permissions and tapping the text in your app, lock your device and wait for the notification to appear. You should hear your custom sound when the notification is delivered.

Conclusion

Custom notification sounds are a great way to personalize your app and enhance user engagement. With SwiftUI and the UserNotifications framework, adding these sounds is straightforward. Experiment with different sounds to find what best suits your app’s personality. By making your notifications unique, you can create a more memorable and enjoyable user experience.

Incorporating custom notification sounds in your SwiftUI app is just one of the many ways you can leverage the power of SwiftUI to build engaging, user-friendly applications. Happy coding!

Our mission

Effect UI for
your next project

We are a team of talented designers making iOS components to help developers build outstanding apps faster with less effort and best design.