Skip to content

Commit 2d60496

Browse files
committed
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.
1 parent 94e1cc9 commit 2d60496

14 files changed

Lines changed: 646 additions & 155 deletions
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Foundation
2+
import UserNotifications
3+
import WebKit
4+
5+
enum DashboardNotificationsRequest: String {
6+
case status
7+
case requestPermission = "request-permission"
8+
case sendTest = "send-test"
9+
}
10+
11+
@MainActor
12+
final class DashboardNotificationsMessageHandler: NSObject, WKScriptMessageHandler {
13+
weak var owner: DashboardWindowController?
14+
15+
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
16+
self.owner?.receiveNotificationsMessage(message)
17+
}
18+
}
19+
20+
extension DashboardWindowController {
21+
static let notificationsMessageHandlerName = "openclawNotifications"
22+
23+
static func notificationsRequest(from body: Any) -> DashboardNotificationsRequest? {
24+
guard let payload = body as? [String: Any],
25+
let type = payload["type"] as? String
26+
else {
27+
return nil
28+
}
29+
return DashboardNotificationsRequest(rawValue: type)
30+
}
31+
32+
static func notificationsPermissionLabel(for status: UNAuthorizationStatus) -> String {
33+
switch status {
34+
case .authorized, .provisional:
35+
"granted"
36+
case .denied:
37+
"denied"
38+
case .notDetermined:
39+
"notDetermined"
40+
default:
41+
// .ephemeral is unavailable by name on macOS and cannot occur here;
42+
// map it and future cases to notDetermined so the UI offers the
43+
// permission request instead of claiming access.
44+
"notDetermined"
45+
}
46+
}
47+
48+
func receiveNotificationsMessage(_ message: WKScriptMessage) {
49+
guard message.name == Self.notificationsMessageHandlerName,
50+
message.webView === self.webView,
51+
message.frameInfo.isMainFrame,
52+
Self.isTrustedLinkSource(message.frameInfo.request.url, dashboardURL: self.currentURL)
53+
else {
54+
return
55+
}
56+
guard let request = Self.notificationsRequest(from: message.body) else { return }
57+
58+
switch request {
59+
case .status:
60+
Task { await self.publishNotificationsStatus() }
61+
case .requestPermission:
62+
Task {
63+
_ = await PermissionManager.ensure([.notifications], interactive: true)
64+
await self.publishNotificationsStatus()
65+
}
66+
case .sendTest:
67+
Task {
68+
_ = await NotificationManager().send(
69+
title: "OpenClaw",
70+
body: "Test notification",
71+
sound: nil)
72+
await self.publishNotificationsStatus()
73+
}
74+
}
75+
}
76+
77+
private func publishNotificationsStatus() async {
78+
let settings = await UNUserNotificationCenter.current().notificationSettings()
79+
let permission = Self.notificationsPermissionLabel(for: settings.authorizationStatus)
80+
// Keep a global snapshot so late subscribers can read status without a bridge round-trip.
81+
_ = try? await self.webView.evaluateJavaScript(
82+
"""
83+
window.__OPENCLAW_NATIVE_NOTIFICATIONS__ = {permission:"\(permission)"};
84+
window.dispatchEvent(new CustomEvent('openclaw:native-notifications-status', \
85+
{detail:window.__OPENCLAW_NATIVE_NOTIFICATIONS__}));
86+
""")
87+
}
88+
}

‎apps/macos/Sources/OpenClaw/DashboardWindowController.swift‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
8787
private static let windowDragMessageHandlerName = "openclawWindowDrag"
8888
private static let updateMessageHandlerName = "openclawUpdate"
8989

90-
private let webView: DashboardWebView
90+
let webView: DashboardWebView
9191
private let linkBrowser: DashboardLinkBrowserView
9292
private let linkBrowserItem: NSSplitViewItem
9393
private let linkBrowserSplitView: DashboardLinkSplitView
@@ -137,6 +137,8 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
137137
config.userContentController.add(linkMessageHandler, name: Self.linkMessageHandlerName)
138138
let windowDragMessageHandler = DashboardWindowDragMessageHandler()
139139
config.userContentController.add(windowDragMessageHandler, name: Self.windowDragMessageHandlerName)
140+
let notificationsMessageHandler = DashboardNotificationsMessageHandler()
141+
config.userContentController.add(notificationsMessageHandler, name: Self.notificationsMessageHandlerName)
140142
let updateMessageHandler = DashboardUpdateMessageHandler()
141143
self.updateMessageHandler = updateMessageHandler
142144
if shouldEnableUpdateBridge {
@@ -200,6 +202,7 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
200202
self.linkBrowserItem.isCollapsed = true
201203
linkMessageHandler.owner = self
202204
windowDragMessageHandler.owner = self
205+
notificationsMessageHandler.owner = self
203206
updateMessageHandler.owner = self
204207
self.webView.navigationDelegate = self
205208
self.webView.uiDelegate = self
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Testing
2+
import UserNotifications
3+
@testable import OpenClaw
4+
5+
@MainActor
6+
struct DashboardNotificationsBridgeTests {
7+
@Test func `parses notification requests`() {
8+
#expect(DashboardWindowController.notificationsRequest(from: ["type": "status"]) == .status)
9+
#expect(DashboardWindowController.notificationsRequest(
10+
from: ["type": "request-permission"]) == .requestPermission)
11+
#expect(DashboardWindowController.notificationsRequest(
12+
from: ["type": "send-test"]) == .sendTest)
13+
}
14+
15+
@Test func `rejects invalid notification requests`() {
16+
#expect(DashboardWindowController.notificationsRequest(from: ["type": "unknown"]) == nil)
17+
#expect(DashboardWindowController.notificationsRequest(from: "status") == nil)
18+
}
19+
20+
@Test func `maps notification permission labels`() throws {
21+
#expect(DashboardWindowController.notificationsPermissionLabel(for: .authorized) == "granted")
22+
#expect(DashboardWindowController.notificationsPermissionLabel(for: .provisional) == "granted")
23+
// Ephemeral (unavailable by name on macOS, raw value 4) cannot occur here
24+
// and maps to notDetermined with the rest of the default branch.
25+
let ephemeral = try #require(UNAuthorizationStatus(rawValue: 4))
26+
#expect(DashboardWindowController.notificationsPermissionLabel(
27+
for: ephemeral) == "notDetermined")
28+
#expect(DashboardWindowController.notificationsPermissionLabel(for: .denied) == "denied")
29+
#expect(DashboardWindowController.notificationsPermissionLabel(
30+
for: .notDetermined) == "notDetermined")
31+
}
32+
}

‎docs/platforms/macos.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ See [Gateway on macOS](/platforms/mac/bundled-gateway) for manual recovery.
110110
permission attribution while the CLI runtime owns shared node policy.
111111
- Remote-mode SSH tunnels or direct Gateway connections.
112112

113+
In the embedded Control UI, **Settings → Notifications** shows the app's native
114+
notification permission instead of browser push because the app delivers notifications natively.
115+
113116
The app does **not** replace the Gateway or general CLI docs. Gateway
114117
configuration, providers, plugins, channels, tools, and security live in their
115118
own docs.

‎docs/web/control-ui.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ states that need your input rather than waiting.
454454

455455
The Control UI ships a `manifest.webmanifest` and a service worker, so modern browsers can install it as a standalone PWA. Web Push lets the Gateway wake the installed PWA with notifications even when the tab or browser window is not open.
456456

457+
Inside the macOS app, the Notifications settings page shows the app's native notification permission instead of browser push because the app delivers notifications natively.
458+
457459
If the page shows **Protocol mismatch** right after an OpenClaw update, first reopen the dashboard with `openclaw dashboard` and hard-refresh. If it still fails, clear site data for the dashboard origin or test in a private browser window; an old tab or browser service-worker cache can keep running a pre-update Control UI bundle against the newer Gateway.
458460

459461
| Surface | What it does |

‎ui/src/app/bootstrap.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { syncCustomThemeStyleTag } from "./custom-theme.ts";
3636
import { createApplicationGateway } from "./gateway-store.ts";
3737
import { createNativeChatDrafts } from "./native-bridge.ts";
3838
import { startNativeLinkRouting } from "./native-link-routing.ts";
39+
import { createNativeNotificationsCapability } from "./native-notifications.ts";
3940
import { createApplicationOverlays } from "./overlays.ts";
4041
import {
4142
loadSettings,
@@ -319,6 +320,7 @@ export function bootstrapApplication(): ApplicationRuntime {
319320
const theme = createApplicationTheme(settings);
320321
const nativeChatDrafts = createNativeChatDrafts();
321322
const nativeLinkRouting = startNativeLinkRouting();
323+
const nativeNotifications = createNativeNotificationsCapability();
322324
const webPush = createWebPushCapability(gateway);
323325
const skillWorkshopRevision = createSkillWorkshopRevisionHandoff();
324326
applyStartupPresentation(settings);
@@ -390,6 +392,7 @@ export function bootstrapApplication(): ApplicationRuntime {
390392
navigation,
391393
theme,
392394
nativeChatDrafts,
395+
nativeNotifications,
393396
webPush,
394397
skillWorkshopRevision,
395398
navigate: (routeId, options) => {
@@ -447,6 +450,7 @@ export function bootstrapApplication(): ApplicationRuntime {
447450
theme.dispose();
448451
nativeChatDrafts.dispose();
449452
nativeLinkRouting.dispose();
453+
nativeNotifications?.dispose();
450454
webPush.dispose();
451455
skillWorkshopRevision.clear();
452456
},

‎ui/src/app/context.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { AgentSelectionCapability } from "./agent-selection.ts";
1212
import type { ApplicationConfigCapability } from "./config.ts";
1313
import type { ApplicationGateway } from "./gateway.ts";
1414
import type { NativeChatDrafts } from "./native-bridge.ts";
15+
import type { NativeNotificationsCapability } from "./native-notifications.ts";
1516
import type { ApplicationOverlays } from "./overlays.ts";
1617
import type { ThemeMode } from "./theme.ts";
1718
import type { WebPushCapability } from "./web-push.ts";
@@ -73,6 +74,7 @@ export type ApplicationContext<TRouteId extends string = string> = {
7374
readonly navigation: ApplicationNavigationPreferences;
7475
readonly theme: ApplicationTheme;
7576
readonly nativeChatDrafts: NativeChatDrafts;
77+
readonly nativeNotifications: NativeNotificationsCapability | null;
7678
readonly webPush: WebPushCapability;
7779
readonly skillWorkshopRevision: ApplicationSkillWorkshopRevisionHandoff;
7880
readonly navigate: (routeId: TRouteId, options?: ApplicationNavigationOptions) => void;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* @vitest-environment jsdom */
2+
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import {
5+
createNativeNotificationsCapability,
6+
NATIVE_NOTIFICATIONS_STATUS_EVENT,
7+
type NativeNotificationsCapability,
8+
} from "./native-notifications.ts";
9+
10+
type NativeNotificationsMessage = {
11+
type: "status" | "request-permission" | "send-test";
12+
};
13+
14+
type NativeNotificationsTestWindow = Window & {
15+
__OPENCLAW_NATIVE_NOTIFICATIONS__?: unknown;
16+
};
17+
18+
let capability: NativeNotificationsCapability | null = null;
19+
20+
afterEach(() => {
21+
capability?.dispose();
22+
capability = null;
23+
Reflect.deleteProperty(
24+
window as NativeNotificationsTestWindow,
25+
"__OPENCLAW_NATIVE_NOTIFICATIONS__",
26+
);
27+
vi.unstubAllGlobals();
28+
});
29+
30+
function installBridge() {
31+
const postMessage = vi.fn<(message: NativeNotificationsMessage) => void>();
32+
vi.stubGlobal("webkit", {
33+
messageHandlers: { openclawNotifications: { postMessage } },
34+
});
35+
return postMessage;
36+
}
37+
38+
describe("native notifications", () => {
39+
it("returns null without the WebKit bridge", () => {
40+
expect(createNativeNotificationsCapability()).toBeNull();
41+
});
42+
43+
it("posts status on create", () => {
44+
const postMessage = installBridge();
45+
46+
capability = createNativeNotificationsCapability();
47+
48+
expect(capability?.snapshot).toEqual({ permission: "unknown" });
49+
expect(postMessage).toHaveBeenCalledWith({ type: "status" });
50+
});
51+
52+
it("seeds status from the native snapshot", () => {
53+
installBridge();
54+
(window as NativeNotificationsTestWindow).__OPENCLAW_NATIVE_NOTIFICATIONS__ = {
55+
permission: "granted",
56+
};
57+
58+
capability = createNativeNotificationsCapability();
59+
60+
expect(capability?.snapshot).toEqual({ permission: "granted" });
61+
});
62+
63+
it("publishes valid status events", () => {
64+
installBridge();
65+
capability = createNativeNotificationsCapability();
66+
const listener = vi.fn();
67+
capability?.subscribe(listener);
68+
69+
window.dispatchEvent(
70+
new CustomEvent(NATIVE_NOTIFICATIONS_STATUS_EVENT, {
71+
detail: { permission: "denied" },
72+
}),
73+
);
74+
75+
expect(capability?.snapshot).toEqual({ permission: "denied" });
76+
expect(listener).toHaveBeenCalledWith({ permission: "denied" });
77+
});
78+
79+
it("ignores invalid status event details", () => {
80+
installBridge();
81+
capability = createNativeNotificationsCapability();
82+
const listener = vi.fn();
83+
capability?.subscribe(listener);
84+
85+
window.dispatchEvent(
86+
new CustomEvent(NATIVE_NOTIFICATIONS_STATUS_EVENT, {
87+
detail: { permission: "authorized" },
88+
}),
89+
);
90+
91+
expect(capability?.snapshot).toEqual({ permission: "unknown" });
92+
expect(listener).not.toHaveBeenCalled();
93+
});
94+
95+
it("reposts status when the window focuses", () => {
96+
const postMessage = installBridge();
97+
capability = createNativeNotificationsCapability();
98+
postMessage.mockClear();
99+
100+
window.dispatchEvent(new Event("focus"));
101+
102+
expect(postMessage).toHaveBeenCalledWith({ type: "status" });
103+
});
104+
105+
it("posts permission and test actions", () => {
106+
const postMessage = installBridge();
107+
capability = createNativeNotificationsCapability();
108+
postMessage.mockClear();
109+
110+
capability?.requestPermission();
111+
capability?.sendTest();
112+
113+
expect(postMessage.mock.calls).toEqual([
114+
[{ type: "request-permission" }],
115+
[{ type: "send-test" }],
116+
]);
117+
});
118+
119+
it("removes listeners on dispose", () => {
120+
const postMessage = installBridge();
121+
capability = createNativeNotificationsCapability();
122+
const listener = vi.fn();
123+
capability?.subscribe(listener);
124+
capability?.dispose();
125+
capability = null;
126+
postMessage.mockClear();
127+
128+
window.dispatchEvent(new Event("focus"));
129+
window.dispatchEvent(
130+
new CustomEvent(NATIVE_NOTIFICATIONS_STATUS_EVENT, {
131+
detail: { permission: "granted" },
132+
}),
133+
);
134+
135+
expect(postMessage).not.toHaveBeenCalled();
136+
expect(listener).not.toHaveBeenCalled();
137+
});
138+
});

0 commit comments

Comments
 (0)