ios - 在 UNNotificationServiceExtension 中为 iOS 远程通知添加系统声音

标签 ios swift xcode remote-notifications

我正在使用 UINotificationServiceExtension 成功修改我的远程通知负载。

我想根据用户在这里的选择更改提示音。为此,我需要将 UINotificationSound 对象分配给 bestAttemptContent:

bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "alertTone.caf"))

这适用于我创建并导入到我的项目中的音调。上例中的一个名为 alertTone.caf。

但是,我想使用系统提示音而不是我自己的提示音。我知道我可以设置默认音调,但我想访问其他自定义音调。有没有办法做到这一点?

最佳答案

UNNotificationSound 不能引用系统声音,但可以尝试在服务扩展中收到通知时播放声音。

// import
import AVFoundation

// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016

// Play the correct sound when the notification is received in the service extension
AudioServicesPlaySystemSound (systemSoundID)

播放系统声音的来源here .

This是您可以用作引用的所有系统声音 ID 的列表。

关于ios - 在 UNNotificationServiceExtension 中为 iOS 远程通知添加系统声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913791/

相关文章:

ios - 文字换行有困难

iphone - 使iOS block 同步执行

Swift 4 contains 方法对于像 "as"这样的短单词并不起作用

ios - Xcode:有关使用 xcassets 的建议

ios - 如何在 ios UITextfield 中显示完整的占位符文本?

ios - SwiftUI - @Binding :String from DatePickerView

ios - 在 UITableView cellForRowAtIndexPath 方法中访问 NSLayoutConstraint ?

ios - 无法在解析推送通知时注册我的 ios 设备

ios - 无法识别的选择器发送到实例,与 View 无关

ios - Xcode 环境变量在存档期间不存在