新增重要警告音量参数

close: #152
This commit is contained in:
Fin 2024-11-12 14:54:00 +08:00
parent 089b33caa3
commit 8c9834d25f
2 changed files with 45 additions and 21 deletions

View File

@ -43,7 +43,7 @@ class CallProcessor: NotificationContentProcessor {
guard let content = content.mutableCopy() as? UNMutableNotificationContent else {
return
}
if !content.isCritical { //
if !content.isCritical { //
content.sound = nil
}
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
@ -152,12 +152,3 @@ class CallProcessor: NotificationContentProcessor {
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, name, nil)
}
}
extension UNMutableNotificationContent {
var isCritical: Bool {
if #available(iOS 15, *) {
return self.interruptionLevel == .critical
}
return false
}
}

View File

@ -11,18 +11,51 @@ import Foundation
///
class LevelProcessor: NotificationContentProcessor {
func process(identifier: String, content bestAttemptContent: UNMutableNotificationContent) async throws -> UNMutableNotificationContent {
if #available(iOSApplicationExtension 15.0, *) {
if let level = bestAttemptContent.userInfo["level"] as? String {
let interruptionLevels: [String: UNNotificationInterruptionLevel] = [
"passive": UNNotificationInterruptionLevel.passive,
"active": UNNotificationInterruptionLevel.active,
"timeSensitive": UNNotificationInterruptionLevel.timeSensitive,
"timesensitive": UNNotificationInterruptionLevel.timeSensitive,
"critical": UNNotificationInterruptionLevel.critical
]
bestAttemptContent.interruptionLevel = interruptionLevels[level] ?? .active
}
guard let level = bestAttemptContent.userInfo["level"] as? String else {
return bestAttemptContent
}
//
if level == "critical" {
//
var audioVolume: Float = 0.5
// 1 - 10 , 0.1 - 1
if let volume = bestAttemptContent.userInfo["volume"] as? String, let volume = Float(volume) {
audioVolume = max(0.1, min(1, volume / 10.0))
}
// sound
if let sound = bestAttemptContent.soundName {
bestAttemptContent.sound = UNNotificationSound.criticalSoundNamed(UNNotificationSoundName(rawValue: sound), withAudioVolume: audioVolume)
} else {
bestAttemptContent.sound = UNNotificationSound.defaultCriticalSound(withAudioVolume: audioVolume)
}
return bestAttemptContent
}
//
guard #available(iOSApplicationExtension 15.0, *) else {
return bestAttemptContent
}
let interruptionLevels: [String: UNNotificationInterruptionLevel] = [
"passive": UNNotificationInterruptionLevel.passive,
"active": UNNotificationInterruptionLevel.active,
"timeSensitive": UNNotificationInterruptionLevel.timeSensitive,
"timesensitive": UNNotificationInterruptionLevel.timeSensitive
]
bestAttemptContent.interruptionLevel = interruptionLevels[level] ?? .active
return bestAttemptContent
}
}
extension UNMutableNotificationContent {
///
var isCritical: Bool {
self.userInfo["level"] as? String == "critical"
}
///
var soundName: String? {
(self.userInfo["aps"] as? [AnyHashable: Any])?["sound"] as? String
}
}