Files
openclaw/apps/macos/Sources/OpenClaw/DashboardWindowController+Notifications.swift
Peter Steinberger 42133d0c3c feat(mac): show native notification permission in dashboard Notifications settings (#110646)
* feat(mac): show native notification permission in dashboard Notifications settings

The Mac app's embedded Control UI showed the web-push section as
Unsupported/Not subscribed even though the app delivers notifications
natively. The dashboard now installs an openclawNotifications WebKit
bridge (status / request-permission / send-test) and the Notifications
settings page renders native permission state with Enable, Open System
Settings, and Send test actions when hosted in the Mac app. Browsers
keep the existing web-push UI. The notifications section renderer moved
out of the oversized config view module.

* fix(ui): break config view import cycle and satisfy lint/deadcode/i18n gates

Move the notifications section props to a leaf contract (no view.ts
type import), unexport the status-event constant, use bracket access
for the injected dunder global, default-case the status switch, and
refresh the native i18n inventory line offset.
2026-07-18 14:13:06 +01:00

89 lines
3.2 KiB
Swift

import Foundation
import UserNotifications
import WebKit
enum DashboardNotificationsRequest: String {
case status
case requestPermission = "request-permission"
case sendTest = "send-test"
}
@MainActor
final class DashboardNotificationsMessageHandler: NSObject, WKScriptMessageHandler {
weak var owner: DashboardWindowController?
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
self.owner?.receiveNotificationsMessage(message)
}
}
extension DashboardWindowController {
static let notificationsMessageHandlerName = "openclawNotifications"
static func notificationsRequest(from body: Any) -> DashboardNotificationsRequest? {
guard let payload = body as? [String: Any],
let type = payload["type"] as? String
else {
return nil
}
return DashboardNotificationsRequest(rawValue: type)
}
static func notificationsPermissionLabel(for status: UNAuthorizationStatus) -> String {
switch status {
case .authorized, .provisional:
"granted"
case .denied:
"denied"
case .notDetermined:
"notDetermined"
default:
// .ephemeral is unavailable by name on macOS and cannot occur here;
// map it and future cases to notDetermined so the UI offers the
// permission request instead of claiming access.
"notDetermined"
}
}
func receiveNotificationsMessage(_ message: WKScriptMessage) {
guard message.name == Self.notificationsMessageHandlerName,
message.webView === self.webView,
message.frameInfo.isMainFrame,
Self.isTrustedLinkSource(message.frameInfo.request.url, dashboardURL: self.currentURL)
else {
return
}
guard let request = Self.notificationsRequest(from: message.body) else { return }
switch request {
case .status:
Task { await self.publishNotificationsStatus() }
case .requestPermission:
Task {
_ = await PermissionManager.ensure([.notifications], interactive: true)
await self.publishNotificationsStatus()
}
case .sendTest:
Task {
_ = await NotificationManager().send(
title: "OpenClaw",
body: "Test notification",
sound: nil)
await self.publishNotificationsStatus()
}
}
}
private func publishNotificationsStatus() async {
let settings = await UNUserNotificationCenter.current().notificationSettings()
let permission = Self.notificationsPermissionLabel(for: settings.authorizationStatus)
// Keep a global snapshot so late subscribers can read status without a bridge round-trip.
_ = try? await self.webView.evaluateJavaScript(
"""
window.__OPENCLAW_NATIVE_NOTIFICATIONS__ = {permission:"\(permission)"};
window.dispatchEvent(new CustomEvent('openclaw:native-notifications-status', \
{detail:window.__OPENCLAW_NATIVE_NOTIFICATIONS__}));
""")
}
}