I have an app where I send notifications at specified times to help users remember things. I'm running into a problem when implementing custom sounds in my notifications. I have the sound playing when the phone's ringer is on, however I want to have it play a haptic feedback when it delivers. The iOS default sound plays a haptic when it delivers whether or not the ringer is on or not.
My code looks like this:
var sounds = ["default", "definite", "quite-impressed", "rush", "serious-strike", "what-friends-are-for"]
var sound = 0
let soundNumber = defaults.integer(forKey: "alarmSound")
let notificationContent = UNMutableNotificationContent()
notificationContent.title = defaults.string(forKey: "notificationBody") ?? "Time to eat!"
notificationContent.body = defaults.string(forKey: "notificationText") ?? "Your timers have gone off!"
if sound == 0 {
notificationContent.sound = UNNotificationSound.default
} else {
notificationContent.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "\(sounds[sound]).m4a"))
}
notificationContent.categoryIdentifier = NotificationActions.Category.snooze
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: differenceInDates, repeats: false)
let request = UNNotificationRequest(identifier: "\(i)", content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
The sounds array is an array of the sound file names, it gets the index of the sound from UserDefaults and sets the notification sound with that file.
Appreciate any help!