ios - UILocalNotification 自定义 soundName 未播放

标签 ios swift

我已经尝试了所有可以想到的方法来播放自定义声音名称 (在 Mac 上格式化为 aiff、wav,使用在线工具格式化,使用以前可以使用的旧声音,移动文件,重命名文件,确保歌曲在 30 秒以下,清理项目,删除应用程序,重新启动 iPhone,删除衍生数据。

基本上一切都行,但就是行不通。我猜这一定是一个错误。我已经尝试了 2-3 天并阅读了所有 SO 问题和论坛 - 没有运气。

所以我放弃了,只想播放铃声,最好是 29 秒长的铃声。 我怎样才能做到这一点?除了 UILocalNotificationDefaultSoundName 之外还有其他铃声/系统声音吗?

任何帮助将非常感激!

func pushRegistry(registry: PKPushRegistry, didReceiveIncomingPushWithPayload payload: PKPushPayload, forType type: String) {
    print("\n\n##  DID RECEIVE NOTIFICATION ## \n\n")

    let data = payload.dictionaryPayload

    if let type = data["type"] as? String {
        if type == "MESSAGE_NOTE" {
            print("Notification is Message")
            let notification = UILocalNotification()
            notification.alertTitle = "App message"
            notification.soundName =  UILocalNotificationDefaultSoundName
            notification.userInfo = ["type":type]
            notification.alertBody = "New message"
            notification.alertAction = "Accept"
            UIApplication.sharedApplication().applicationIconBadgeNumber += 1
            UIApplication.sharedApplication().presentLocalNotificationNow(notification)

        } else if type == "INCOMING_CALL" {
            print("Notification is incoming call")
            let notification = UILocalNotification()
            notification.alertTitle = "App call"
            notification.soundName =  "Simple_ring_tone_29s.aiff"
            notification.userInfo = ["type":type]
            notification.alertBody = "Incoming call"
            notification.alertAction = "Accept"
            UIApplication.sharedApplication().applicationIconBadgeNumber += 1
            UIApplication.sharedApplication().presentLocalNotificationNow(notification)
        }
    }
}

通知设置已在另一个 VC 中注册

    let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()

    if !notificationSettings.types.contains([.Badge, .Sound, .Alert])  {
        let notificationTypes: UIUserNotificationType = [.Badge, .Sound, .Alert]
        let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

在 didFinishLaunchingWithOptions 中

    voipRegistry = PKPushRegistry(queue: dispatch_get_main_queue())
    voipRegistry.delegate = self
    voipRegistry.desiredPushTypes = Set([PKPushTypeVoIP])

最佳答案

此问题已从 iOS 10.1 beta 2 开始修复。

https://forums.developer.apple.com/thread/49512

关于ios - UILocalNotification 自定义 soundName 未播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39945322/

相关文章:

ios - 在 Objective C 中将与文本内联的图像设置为 UITextView

ios - 未调用 UICollectionView 数据源

ios - 如何在 Swift 的小数位拆分字符串?

ios - 按下时如何使 textView 位于键盘顶部?

swift - 如果在 Swift 中可选为 nil,则初始化非可选 var 的最佳方法

swift - UIActivityViewController 和 facebook/twitter

ios - 在 performBatchUpdates 中重新加载 collectionview 时未调用 UICollectionview cellForItemAtIndexPath

ios - 如何使 UIImagePickerController StatusBar lightContent 样式?

swift - 如何使用 Firestore (SWIFT) 中的 FieldValue 减少一个值?

swift - 避免在 DispatchQueue 中使用 self