Choose custom sound for local notifications

后端 未结 6 851
囚心锁ツ
囚心锁ツ 2020-11-27 15:31

How do you change the sound that plays for local notifications? I use the code below to play the default sound:

notif.soundName = UILocalNotificationDefaultS         


        
6条回答
  •  春和景丽
    2020-11-27 15:51

    For Swift 3 use UNNotificationSound.init for the notif object in notification scheduling function.

    Here's a Swift 3 example:

    func scheduleNotifications(inSeconds: TimeInterval, completion: @escaping (_ Success: Bool) ->()){
            ...
            notif.sound = UNNotificationSound.init(named: "CustomSound.mp3") 
            ...
    }
    

    Final note, according to Apple: "The sound file must be contained in the app’s bundle or in the Library/Sounds folder of the app's data container. If files exist in both locations then the file in ~/Library/Sounds will be preferred."

提交回复
热议问题