mirror of
https://github.com/Finb/Bark.git
synced 2025-12-08 21:36:01 +00:00
parent
089b33caa3
commit
8c9834d25f
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user