Implement send notification

This commit is contained in:
世界
2024-11-07 16:52:14 +08:00
parent c72167b0dc
commit 348f2809a1
6 changed files with 102 additions and 25 deletions
+1 -13
View File
@@ -14,8 +14,6 @@ public class CommandClient: ObservableObject {
private let logMaxLines: Int
private var commandClient: LibboxCommandClient?
private var connectTask: Task<Void, Error>?
private var openURLFunc: ((String) -> Void)?
@Published public var isConnected: Bool
@Published public var status: LibboxStatusMessage?
@Published public var groups: [LibboxOutboundGroup]?
@@ -31,15 +29,13 @@ public class CommandClient: ObservableObject {
public init(_ connectionType: ConnectionType, logMaxLines: Int = 300) {
self.connectionType = connectionType
self.logMaxLines = logMaxLines
openURLFunc = nil
logList = []
clashModeList = []
clashMode = ""
isConnected = false
}
public func connect(_ openURLFunc: ((String) -> Void)? = nil) {
self.openURLFunc = openURLFunc
public func connect() {
if isConnected {
return
}
@@ -52,7 +48,6 @@ public class CommandClient: ObservableObject {
}
public func disconnect() {
openURLFunc = nil
if let connectTask {
connectTask.cancel()
self.connectTask = nil
@@ -227,13 +222,6 @@ public class CommandClient: ObservableObject {
commandClient.connections = connections
}
}
func openURL(_ url: String?) {
guard let url else {
return
}
commandClient.openURLFunc?(url)
}
}
}
@@ -1,6 +1,7 @@
import Foundation
import Libbox
import NetworkExtension
import UserNotifications
#if canImport(CoreWLAN)
import CoreWLAN
#endif
@@ -338,4 +339,28 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
func reset() {
networkSettings = nil
}
public func send(_ notification: LibboxNotification?) throws {
#if !os(tvOS)
guard let notification else {
return
}
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = notification.title
content.subtitle = notification.subtitle
content.body = notification.body
if !notification.openURL.isEmpty {
content.userInfo["OPEN_URL"] = notification.openURL
content.categoryIdentifier = "OPEN_URL"
}
content.interruptionLevel = .active
let request = UNNotificationRequest(identifier: notification.identifier, content: content, trigger: nil)
try runBlocking {
try await center.requestAuthorization(options: [.alert])
try await center.add(request)
}
#endif
}
}