【问题标题】:Sound name from UNNotificationSound来自 UNNotificationSound 的声音名称
【发布时间】:2016-07-30 12:59:21
【问题描述】:

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

notification.soundName = UILocalNotificationDefaultSoundName

(通知是CKNotificationInfo

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

“UILocalNotificationDefaultSoundName”在 iOS 10.0 中已弃用:使用 UserNotifications Framework 的 +[UNNotificationSound defaultSound]

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

notification.soundName = UNNotificationSound.default()

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

无法将“UNNotificationSound”类型的值分配给“String”类型的值?

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

【问题讨论】:

    标签: ios swift audio push-notification swift3


    【解决方案1】:

    使用新的UserNotifications 框架:

    import UserNotifications
    
    let content = UNMutableNotificationContent()
    content.sound = UNNotificationSound.default()
    
    let request = UNNotificationRequest(identifier: "id", content: content, trigger: nil)
    

    【讨论】:

    • 这不是我想要的……我认为我不需要UNNotificationRequest。此外,不幸的是,content.sound 是 get-only。
    • .sound 对于 UNNotificationContent 是唯一的,但如果您使用 UNMutableNotificationContent,您可以将其设置为您想要使用的声音。
    【解决方案2】:

    遗憾的是,我认为这里的答案是,尽管我们已经通过新的 UserNotifications 框架获得了很多很棒的东西,但 CloudKit 和 CKNotificationInfo 特别没有得到更新。 ?

    目前最好的选择是使用UILocalNotificationDefaultSoundName 通常返回的内容,然后使用"default"

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多