gpt4 book ai didi

ios - 自定义本地通知声音不起作用

转载 作者:行者123 更新时间:2023-11-28 06:08:37 28 4
gpt4 key购买 nike

我正在尝试为本地通知提供自定义通知声音。但它仍然以默认声音播放

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
"Test", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey:
"Test message", arguments: nil)
content.sound = UNNotificationSound.init(named: "marbles-daniel_simon.mp3")
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,
repeats: true)
let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)

我在下面的项目中添加了声音文件

enter image description here

最佳答案

我在有关本地和远程通知的 Apple 文档中检查了您的问题,这里有一些有趣的内容:

Local and remote notifications can specify custom alert sounds to be played when the notification is delivered. You can package the audio data in an aiff, wav, or caf file. Because they are played by the system-sound facility, custom sounds must be in one of the following audio data formats:

  • 线性PCM
  • MA4(IMA/ADPCM)
  • 微定律
  • 法律

Place custom sound files in your app bundle or in the Library/Sounds folder of your app’s container directory. Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead. When specifying custom sounds, specify only the filename of the sound file that you want played. If the system finds a suitable sound file with the name you provided, it plays that sound when delivering the notification. If the system does not find a suitable sound file, it plays the default sound.

content.sound = UNNotificationSound(named: "MySound.aiff")

更新:将文件添加到 Xcode 项目根目录。添加文件时确保选择添加到目标,以便它们自动添加到包资源

Add custom sound

关于ios - 自定义本地通知声音不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47408464/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com