持续响铃支持自定义铃声

This commit is contained in:
Fin 2024-08-07 16:31:23 +08:00
parent 779e2375f8
commit 99bb686be1
2 changed files with 34 additions and 3 deletions

View File

@ -178,22 +178,53 @@ class SoundFileStorage: SoundFileStorageProtocol {
/// Library/Sound /// Library/Sound
func saveSound(url: URL) { func saveSound(url: URL) {
// Sounds
let soundsDirectoryUrl = getSoundsDirectory() let soundsDirectoryUrl = getSoundsDirectory()
let soundUrl = soundsDirectoryUrl.appendingPathComponent(url.lastPathComponent) let soundUrl = soundsDirectoryUrl.appendingPathComponent(url.lastPathComponent)
try? fileManager.copyItem(at: url, to: soundUrl) try? fileManager.copyItem(at: url, to: soundUrl)
//
saveSoundToGroupDirectory(url: url)
} }
func deleteSound(url: URL) { func deleteSound(url: URL) {
// sounds
try? fileManager.removeItem(at: url) try? fileManager.removeItem(at: url)
//
if let groupSoundUrl = getSoundsGroupDirectory()?.appendingPathComponent(url.lastPathComponent) {
try? fileManager.removeItem(at: groupSoundUrl)
}
} }
/// Library Sounds /// Library Sounds
/// ///
func getSoundsDirectory() -> URL { private func getSoundsDirectory() -> URL {
let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!.appending("/Sounds") let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!.appending("/Sounds")
if !fileManager.fileExists(atPath: soundsDirectoryUrl) { if !fileManager.fileExists(atPath: soundsDirectoryUrl) {
try? fileManager.createDirectory(atPath: soundsDirectoryUrl, withIntermediateDirectories: true, attributes: nil) try? fileManager.createDirectory(atPath: soundsDirectoryUrl, withIntermediateDirectories: true, attributes: nil)
} }
return URL(fileURLWithPath: soundsDirectoryUrl) return URL(fileURLWithPath: soundsDirectoryUrl)
} }
/// NotificationServiceExtension 使
private func saveSoundToGroupDirectory(url: URL) {
guard let groupUrl = getSoundsGroupDirectory() else {
return
}
let soundUrl = groupUrl.appendingPathComponent(url.lastPathComponent)
try? fileManager.copyItem(at: url, to: soundUrl)
}
/// Sounds
///
private func getSoundsGroupDirectory() -> URL? {
if let directoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Sounds") {
if !fileManager.fileExists(atPath: directoryUrl.absoluteString) {
try? fileManager.createDirectory(atPath: directoryUrl.absoluteString, withIntermediateDirectories: true, attributes: nil)
}
return directoryUrl
}
return nil
}
} }

View File

@ -136,10 +136,10 @@ class CallProcessor: NotificationContentProcessor {
func getSoundInCustomSoundsDirectory(soundName: String) -> String? { func getSoundInCustomSoundsDirectory(soundName: String) -> String? {
// 访APP // 访APP
guard let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first?.appending("/Sounds") else { guard let soundsDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Sounds") else {
return nil return nil
} }
let path = soundsDirectoryUrl.appending("/\(soundName)") let path = soundsDirectoryUrl.appendingPathComponent(soundName).absoluteString
if FileManager.default.fileExists(atPath: path) { if FileManager.default.fileExists(atPath: path) {
return path return path
} }