2

My app is downloading a .wav file and moving it to Library/Sounds, then scheduling a local notification via UNNotificationRequest with that sound's file name. This request sends notifications as expected, but the custom sound I have added only plays aloud when the phone is unlocked, not when the notification is getting delivered to the phone's lock screen. However, if I use UNNotificationSound.default() instead of my custom sound, the default push sound will play both on the lock screen and when the device is unlocked. Anyone have any ideas what could be causing this issue? My code is simple:

let soundName = "demoSound.wav" // (or nil to use default)

let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()

let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body";
content.sound = soundName.flatMap { UNNotificationSound(named: $0) } ??  UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let identifier = "PushNotifierLocalNotification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)

EDIT: this isn't a problem for bundled sounds (dragged & dropped into Xcode), but I see the above issue only when I use sounds downloaded by my app and moved into Library/Sounds.

flatpickles
  • 2,198
  • 2
  • 15
  • 21
  • 1
    Which OS version and device are you running? – shallowThought Mar 10 '18 at 12:40
  • If you create the `UNNotificationSound` directly and assign it to the `content.sound` does it work? I was able to play a custom sound without issue. `let sound = UNNotificationSound(named: "carhorn.wav") content.sound = sound` – Dennis W. Mar 11 '18 at 22:58
  • hopefully you're not using iOS10 as your test device... Because there was a bug on their releases before 10.2. See [here](https://stackoverflow.com/a/39434256/5175709) – mfaani Mar 11 '18 at 23:22
  • man1: Can you make sure downloaded file(.wav) is audible or not ? – Bucket Mar 12 '18 at 11:27
  • This actually does work as expected with bundled sounds used to create UNNotificationSounds directly, but NOT with sounds downloaded by the app and added to Library/Sounds. I'm on iOS 11.2.6. The downloaded sounds are indeed audible: they play as expected when the phone is unlocked. Perhaps this is an unwritten Apple limitation? – flatpickles Mar 12 '18 at 18:48
  • I think it's related to format mismatch while transmitting the file from the server. – navroz Mar 13 '18 at 11:38
  • @man1, Sounds that last longer than 30 seconds are not supported for localNotification. Just check the duration of the sound file which is downloaded. – Rohi Mar 14 '18 at 11:06
  • 1
    This sounds like an issue with file protection. What level of protection did you assign to the downloaded sound file? Check out `FileProtectionType` for more information. – floschliep Mar 15 '18 at 17:36
  • Did you fix this? – bandejapaisa Mar 06 '19 at 19:52

1 Answers1

0

From here, you will notice that :

Waveform Audio File Format (WAVE, or more commonly known as WAV due to its filename extension - both pronounced "wave"[6])3[7][8][9] (rarely, Audio for Windows)[10] is a Microsoft and IBM audio file format standard for storing an audio bitstream on PCs.

I assume that Apple prefer native Apple sound format, like AIFF or CAF, which are both developped by Apple.

May you retry with an AIFF or or CAF sound file format and confirm use if the issue is still here ?

You may also take a look here .

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
  • If not work with AIFF or CAF, the issue will probably be relative to a file access permission issue. – A. STEFANI Mar 16 '18 at 14:36
  • Thanks for your thought, but it seems like this works the same with both WAV and CAF files; by my understanding both can wrap uncompressed PCM files when used correctly. Sadly, using a CAF file instead does not resolve my issue. – flatpickles Mar 20 '18 at 00:12