-->

非主束文件作为提示音(non-main bundle file as alert sound)

2019-06-26 15:02发布

我希望我是错了,但我不认为这是可能的。 在本地和推送通知编程指南,在“准备自定义提示音就会响起”它说,“这声音文件必须在客户端应用程序的主束”。

如果你不能写入主束,那么你怎么能得到所生成的用户记录(使用,比如说,AVAudioRecorder)发挥其作为警报声?

一方面,这似乎是不可能的,但另一方面,我认为有一些做(我会寻找那些)的应用程序在那里。

Answer 1:

我解决了这个复制的系统声音文件到〜/库/声音目录,并将它命名为notification.caf。 服务器警报有效载荷将其指定为要播放的声音文件的名称。 当用户选择了另一种声音,那声音将被复制到同一个文件夹,并覆盖旧的声音。

有效载荷:

{
"aps": {
    "sound": "notification.caf"
}

}

// get the list of system sounds, there are other sounds in folders beside /New
let soundPath = "/System/Library/Audio/UISounds/New"
func getSoundList() -> [String] {
    var result:[String] = []
    let fileManager = NSFileManager.defaultManager()
    let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(soundPath)!
    for url in enumerator.allObjects {
        let string = url as! String
        let newString = string.stringByReplacingOccurrencesOfString(".caf", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
        result.append(newString)
    }
    return result
}

// copy sound file to /Library/Sounds directory, it will be auto detect and played when a push notification arrive
class func copyFileToDirectory(fromPath:String, fileName:String) {
    let fileManager = NSFileManager.defaultManager()

    do {
        let libraryDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let directoryPath = "\(libraryDir.first!)/Sounds"
        try fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)

        let systemSoundPath = "\(fromPath)/\(fileName)"
        let notificationSoundPath = "\(directoryPath)/notification.caf"

        let fileExist = fileManager.fileExistsAtPath(notificationSoundPath)
        if (fileExist) {
            try fileManager.removeItemAtPath(notificationSoundPath)
        }
        try fileManager.copyItemAtPath(systemSoundPath, toPath: notificationSoundPath)
    }
    catch let error as NSError {
        print("Error: \(error)")
    }
}

推送通知声音可以是越野车,虽然,我必须重新启动我的手机之前,声音会可靠地发挥每一个通知。



文章来源: non-main bundle file as alert sound