UILocalNotification始终提供默认声音

时间:2016-06-08 11:09:34

标签: ios swift audio uilocalnotification

我想在交互式本地通知中添加自定义声音。我在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文件,但我的nitification总是给出默认声音。声音文件的长度是29秒。任何budy可以告诉我我错过了什么。

3 个答案:

答案 0 :(得分:1)

这里的问题是您要将整个路径发送到您的声音资产。

替换

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

let ring: String = "notes_of_the_optimistic"

答案 1 :(得分:1)

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

答案 2 :(得分:-1)

  

.caf和任何手动类型转换的文件都不起作用。用mp3和   aiff(苹果定义格式)。