From 8c9834d25f71db9a916e64358a8405b0453805e2 Mon Sep 17 00:00:00 2001 From: Fin Date: Tue, 12 Nov 2024 14:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=87=8D=E8=A6=81=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=E9=9F=B3=E9=87=8F=E5=8F=82=E6=95=B0=20close:=20#152?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Processor/CallProcessor.swift | 11 +--- .../Processor/LevelProcessor.swift | 53 +++++++++++++++---- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/NotificationServiceExtension/Processor/CallProcessor.swift b/NotificationServiceExtension/Processor/CallProcessor.swift index 92f5b159..8fd54660 100644 --- a/NotificationServiceExtension/Processor/CallProcessor.swift +++ b/NotificationServiceExtension/Processor/CallProcessor.swift @@ -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 - } -} diff --git a/NotificationServiceExtension/Processor/LevelProcessor.swift b/NotificationServiceExtension/Processor/LevelProcessor.swift index e08c1caf..2cb5250e 100644 --- a/NotificationServiceExtension/Processor/LevelProcessor.swift +++ b/NotificationServiceExtension/Processor/LevelProcessor.swift @@ -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 + } +}