— iOS, Swift, UNNotificationSound — 2 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.
To follow along with the examples in this article, you'll need:
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.
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 UserNotifications2
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 UNNotificationRequest9 // ...10} else {11 // Sound file not found12}
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.
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 UserNotifications2
3let defaultSound = UNNotificationSound.default4
5// Use the default sound when creating a UNNotificationRequest6// ...
By using UNNotificationSound.default
, you can easily set the default system sound for your notification.
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:
By following these steps, you ensure that the custom sound file is accessible and can be used when setting the UNNotificationSound
for your notifications.