0

I set up a notification with a custom sound like so:

var soundNames = ["Default", "Alert1", "Alert2", "Alert3", "Alert4", "Alert5", "Alert6", "Alert7", "Alert8", "Alert9", "Alert10"]

func addNotification(title: String, timeInt: TimeInterval, image: Data?, soundName: String?) {

let content = UNMutableNotificationContent()
content.title = title

    content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "\(soundName!).caf"))

    var trigger: UNTimeIntervalNotificationTrigger

    trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInt, repeats: true)

// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

// add our notification request
UNUserNotificationCenter.current().add(request)

saveConceptCard(id: request.identifier, title: title, image: image)

}

Now I want to get the name of the custom sound I used when I retrieve the specific notification request from its id

 var notification: UNNotificationRequest

I try to get the sound name like so:

 let sound = notification.content.sound!.debugDescription

But this returns:

 <UNNotificationSound: 0x2822419d0>

Is there a way to get the filename from that code?

Peter Ruppert
  • 1,067
  • 9
  • 24
  • As far as I know there is no API. How about passing the name in the `userInfo` dictionary. – vadian Oct 28 '20 at 20:10
  • @vadian thought so, I didn't have too many variables so I didn't feel the need to make a struct to store in user defaults but I have now and solved it that way, thanks. – Peter Ruppert Oct 28 '20 at 23:50

1 Answers1

-2

you have to convert your text into speech by using AVFoundation framework

when you push your notification you also have to push AVSpeechSynthesizer

here is how you set it:

let utterance = AVSpeechUtterance(string: "Hello world")
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
utterance.rate = 0.1 

let synthesizer = AVSpeechSynthesizer()

Here is the voice action:

synthesizer.speak(utterance)