Skip to content
DeveloperMemos

Setting Custom Notification Sounds with UNNotificationSound

iOS, Swift, UNNotificationSound2 min read

When it comes to building engaging and user-friendly applications, notifications play a crucial role in keeping users informed and engaged with your app's content. Customizing notification sounds adds a personal touch to your app and can make it stand out from the rest. In iOS, you can achieve this by using the UNNotificationSound API. In this article, we will explore how to set custom notification sounds in iOS using UNNotificationSound with Swift.

Prerequisites

To follow along with the examples in this article, you'll need:

  • Xcode 12 or later
  • Basic knowledge of Swift and iOS development

Overview of UNNotificationSound

The UNNotificationSound class in the UserNotifications framework allows you to specify the sound to be played when a notification is delivered to the user's device. It provides various options to control the notification sound, including system-defined sounds, custom sounds bundled with your app, or even no sound at all.

Setting a Custom Notification Sound

To set a custom notification sound, you need to provide a sound file in your app's bundle and specify the sound using the UNNotificationSound API. Let's look at an example where we set a custom sound named "CustomSound.wav" for a notification:

1import UserNotifications
2
3let soundName = "CustomSound"
4let soundExtension = "wav"
5
6if let soundURL = Bundle.main.url(forResource: soundName, withExtension: soundExtension) {
7 let sound = UNNotificationSound(named: UNNotificationSoundName(soundURL.absoluteString))
8 // Use the sound when creating a UNNotificationRequest
9 // ...
10} else {
11 // Sound file not found
12}

In the example above, we first retrieve the URL of the sound file from the app's bundle. Make sure to include the sound file in your Xcode project and ensure it is added to the app's target. Then, we create an instance of UNNotificationSound using the named: initializer with the URL of the sound file.

Once you have the UNNotificationSound instance, you can use it when creating a UNNotificationRequest to specify the sound for your notification. The exact steps for creating and scheduling notifications are beyond the scope of this article, but you can refer to the official Apple documentation for more details on that process.

System-Defined Notification Sounds

In addition to custom sounds, iOS provides a set of system-defined sounds that you can use for your notifications. These sounds are available through the UNNotificationSoundName enum, which includes various options like default, critical, apns, and more. Here's an example of setting the default system sound for a notification:

1import UserNotifications
2
3let defaultSound = UNNotificationSound.default
4
5// Use the default sound when creating a UNNotificationRequest
6// ...

By using UNNotificationSound.default, you can easily set the default system sound for your notification.

Bonus Round: Registering Custom Sounds

To use custom sounds for notifications, you need to make sure they are properly registered in your app's Xcode project. Here are the steps to register a custom sound:

  1. Add the sound file to your Xcode project by dragging and dropping it into the project navigator.
  2. Select your app target, go to the "Build Phases" tab, and make sure the sound file is added under the "Copy Bundle Resources" section.
  3. Build your project to ensure the sound file is included in the app's bundle.

By following these steps, you ensure that the custom sound file is accessible and can be used when setting the UNNotificationSound for your notifications.