Skip to content

Commit

Permalink
新增重要警告音量参数
Browse files Browse the repository at this point in the history
close: #152
  • Loading branch information
Finb committed Nov 12, 2024
1 parent 089b33c commit 8c9834d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
11 changes: 1 addition & 10 deletions NotificationServiceExtension/Processor/CallProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
53 changes: 43 additions & 10 deletions NotificationServiceExtension/Processor/LevelProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 8c9834d

Please sign in to comment.