ios - 来自 UNNotificationSound 的声音名称

标签 ios swift audio push-notification swift3

我正在使用 CloudKit 中的推送通知。这是我在 Swift 3 之前使用的:

notification.soundName = UILocalNotificationDefaultSoundName

(通知是一个 CKNotificationInfo)

但是,这给了我一个警告:

'UILocalNotificationDefaultSoundName' was deprecated in iOS 10.0: Use UserNotifications Framework's +[UNNotificationSound defaultSound]

所以我将 UserNotification 框架添加到我的 Xcode 项目中,并将其导入到我的 ViewController 中。然后我尝试了这个:

notification.soundName = UNNotificationSound.default()

但是,这给了我一个错误:

Cannot assign value of type 'UNNotificationSound' to type 'String?'

所以默认声音是 UNNotificationSound,但我需要声音名称。如何从声音中获取声音名称?谢谢!

最佳答案

使用新的 UserNotifications 框架:

import UserNotifications

let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()

let request = UNNotificationRequest(identifier: "id", content: content, trigger: nil)

关于ios - 来自 UNNotificationSound 的声音名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674237/

相关文章:

ios - 如何在方向更改时更新 UIView 子类中的自动布局约束

ios - 快速在导航栏下添加进度条?

ios - 超出范围时,UISearchController 未正确关闭

iphone - 自定义键盘设置光标位置

ios - iOS 实时摄像头馈送的 Swift 直边检测

ios - 我正在用两行单元格实现 UICollectionView,我需要它们可以同时水平滚动

ubuntu - 在 FFmpeg 上合并不等长的音频和视频

javascript - Fluent-ffmpeg : merging video and audio = wrong frames

typescript - 我收到错误信息,polyfills-es2015.js:4001未处理的 promise 拒绝:指定的音频ID不存在引用。 ;区域:<root>;任务

ios - Collection View 与其容器之间的 20px 间距