Files
openchamber-android-native/third_party/mygo/plugins/push/ios/PushPlugin.swift
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

83 lines
3.1 KiB
Swift

import UIKit
import UserNotifications
final class PushPlugin: MyGoPlugin {
private var waiting: PluginReply?
private var lastMessage = ""
private var lastMessageAt = Date.distantPast
/// A tap that launched the app comes before Go listens.
private var opened: [[String: Any]] = []
private var listening = false
private func plain(_ info: [AnyHashable: Any]) -> [String: Any] {
var out: [String: Any] = [:]
for (k, v) in info where JSONSerialization.isValidJSONObject([v]) { out["\(k)"] = v }
return out
}
func call(_ method: String, _ args: [String: Any], _ reply: @escaping PluginReply) {
switch method {
case "configure":
reply(.success(nil))
case "register":
waiting = reply
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
DispatchQueue.main.async {
guard granted else {
self.waiting?(.failure(PluginFailure("Denied", error?.localizedDescription ?? "notifications are not allowed")))
self.waiting = nil
return
}
UIApplication.shared.registerForRemoteNotifications()
}
}
case "listenOpened":
listening = true
for o in opened { PluginHost.emit("push", "opened", o) }
opened = []
reply(.success(nil))
default:
reply(.failure(PluginFailure("NoMethod", method)))
}
}
func applicationEvent(_ event: ApplicationEvent) {
switch event {
case .pushToken(let d):
let token = d.map { String(format: "%02x", $0) }.joined()
if let r = waiting {
r(.success(["token": token]))
waiting = nil
}
PluginHost.emit("push", "token", token)
case .pushError(let e):
waiting?(.failure(PluginFailure("Failed", e.localizedDescription)))
waiting = nil
case .pushMessage(let info):
// UIKit tells some twice: as it arrives, and as it would show.
let key = String(describing: info)
let now = Date()
if key == lastMessage && now.timeIntervalSince(lastMessageAt) < 2 { return }
lastMessage = key
lastMessageAt = now
PluginHost.emit("push", "message", plain(info))
case .pushOpened(let info):
if listening {
PluginHost.emit("push", "opened", plain(info))
} else {
opened.append(plain(info))
}
}
}
}
/// Only apps with this plugin have it: UIKit warns of a delegate with it and no
/// remote-notification background mode.
extension AppDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification info: [AnyHashable: Any],
fetchCompletionHandler done: @escaping (UIBackgroundFetchResult) -> Void) {
PluginHost.applicationEvent(.pushMessage(info))
done(.newData)
}
}