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,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
}
}