UILocalNotification 始终提供默认声音

UILocalNotification always give default sound

我想在交互式本地通知中添加自定义声音。我在 appdelegate

中写了这段代码
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    scheduleNotification()
}
    func scheduleNotification() {
    //UIApplication.sharedApplication().cancelAllLocalNotifications()

    // Schedule the notification ********************************************
    if UIApplication.sharedApplication().scheduledLocalNotifications!.count == 0 {

        let notification = UILocalNotification()
        notification.alertBody = "Hey! Update your counters ;)"
        // let URLToMyFile = documentsURL.URLByAppendingPathComponent("notes_of_the_optimistic.caf")

        let ring: String = NSBundle.mainBundle().pathForResource("notes_of_the_optimistic", ofType: "caf")!
        print(ring)
        notification.soundName = ring
        notification.fireDate = NSDate()
        notification.category = categoryID
        notification.repeatInterval = NSCalendarUnit.Minute

        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }
}

我已经在我的项目中添加了 notes_of_the_optimistic.caf 文件,但我的通知总是发出默认声音。声音文件的长度为 29 秒。哪位小伙伴能告诉我我缺什么吗

这里的问题是您发送的是声音资产的完整路径。

替换

let ring: String = NSBundle.mainBundle().pathForResource("notes_of_the_optimistic", ofType: "caf")!

let ring: String = "notes_of_the_optimistic"

您不需要获取自定义声音的完整路径,您只需要文件格式为 like notification.soundName = "notes_of_the_optimistic.caf"

的声音名称

.caf and any manually type converted file will not work. use mp3 and aiff (apple define format).