0

When I schedule a UILocalNotification, I know that I can specify a sound to play when the notification is delivered to the user. According to the UILocalNotification API document, I can schedule a local notification with a sound attached to it:

let notification = UILocalNotification()
notification.fireDate = date
notification.timeZone = NSTimeZone.localTimeZone()
notification.alertTitle = "My Alarm"
notification.alertBody = "my text message"
notification.soundName = "my-sound.aif"

let app = UIApplication.sharedApplication()
app.scheduleLocalNotification(notification)

This works well, but there's a limitation: it seems that you can specify only the sound files you already shipped in the application when you submitted the app to the app store.

What I'm trying to do is, I'd like to record users' own voice message into a sound file and specify it in the local notification so that , when the OS delivers the notification to the user, the user can hear his or her own recorded messages. I saw other questions and answers in SO about how to change the sound, but they only talk about changing the sound among the resources that are shipped with the app at the submit time, and not about using dynamically generated sound. Does anyone know if I can attach a generated sound file to a local notification?, and if yes, how to do it?

Jake
  • 1,518
  • 2
  • 14
  • 20
  • Possible duplicate of [Choose custom sound for local notifications](http://stackoverflow.com/questions/7170475/choose-custom-sound-for-local-notifications) – Carter Aug 14 '16 at 18:25
  • @Carter, no, my question is not a duplicate of the question you mentioned. I edited my question to explain it. Basically I'm talking about attaching user-generated sound with a notification. – Jake Aug 15 '16 at 16:16
  • How are you saving the sound? iOS documentation states that "You can assign the filename of a nonlocalized custom sound in the app’s main bundle (or data container)" so if you store the sound in the data container and give the path to it, you should be fine. – Carter Aug 15 '16 at 16:25
  • As far as I know, an app can't save a file into the main bundle. There's another designated place to save files the app dynamically generates. @Carter if you're saying that it's possible to save a file into the main bundle, I'll appreciate if you show me how to. – Jake Aug 15 '16 at 17:22
  • You're not saving to the main bundle, the documentation says you can also load sounds from the data container. This shows you how to write a file https://thatthinginswift.com/writing-documents-directory-swift/ Then just use the location of the file for the notification sound. – Carter Aug 15 '16 at 17:27
  • I see. I didn't think the documents directory would be allowed. I'll try that and see if it works. If it works, I'll post it here. @Carter thanks for the info. – Jake Aug 15 '16 at 18:01
  • @Carter, I tested it today and noticed that the data container idea didn't work. I placed the sample audio file in the main document directory and specified the file name in the notification, but when the notification was delivered the OS played the default sound as if I had specified the default sound. – Jake Aug 17 '16 at 20:45
  • 1
    [This StackOverflow post](http://stackoverflow.com/questions/6514423/how-can-i-play-a-recorded-sound-using-a-local-notification) has this working successfully. – Carter Aug 18 '16 at 08:33
  • @Carter the solution in that post works for me. Thanks for the link. – Jake Aug 18 '16 at 17:57
  • As Carter pointed out, the solution described [here](http://stackoverflow.com/questions/6514423/how-can-i-play-a-recorded-sound-using-a-local-notification) worked for me. – Jake Aug 20 '16 at 20:06
  • you can take this sound file length maximum 30 sec.. more than 30 sec length file not taken by notification. Sounds that last longer than 30 seconds are not supported...By apple doc.. just take a look at this Apple document https://developer.apple.com/documentation/uikit/uilocalnotification – Azharhussain Shaikh Mar 11 '18 at 07:36

0 Answers0