2016-07-30 96 views
2

我正在使用CloudKit中的推送通知。以下是我斯威夫特3之前使用:来自UNNotificationSound的声音名称

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,但我需要的声音名称。我怎样才能从声音中获得声音名称?谢谢!

回答

1

使用新UserNotifications框架:

import UserNotifications 

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

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

这不是我正在寻找的东西...我不相信我需要'UNNotificationRequest'。另外,content.sound不幸的是只能得到。 – penatheboss

+2

.sound对于UNNotificationContent只能获取,但如果使用UNMutableNotificationContent,则可以将其设置为您要使用的声音。 – gohnjanotis

2

可悲的是我认为这里的答案是,即使我们已经得到了很多伟大的东西与新UserNotifications框架,CloudKit和CKNotificationInfo特别是没有得到为它更新。

现在最好的选择,就是使用UILocalNotificationDefaultSoundName已经正常返回,并使用"default"