Notification
This part of the runtime provides access to native system notifications with support for interactive elements like action buttons and text input fields.
InitializeNotificationsโ
Initializes the notification system. It should be called during app startup.
Go: InitializeNotifications(ctx context.Context) error
JavaScript: InitializeNotifications(): Promise<void>
Returns: Error if initialization fails
Example:
err := runtime.InitializeNotifications(ctx)
if err != nil {
log.Fatal(err)
}
await runtime.InitializeNotifications();
IsNotificationAvailableโ
Checks if notifications are supported on the current platform.
Go: IsNotificationAvailable(ctx context.Context) bool
JavaScript: IsNotificationAvailable(): Promise<boolean>
Returns: true if notifications are supported, false otherwise
Example:
if !runtime.IsNotificationAvailable(ctx) {
log.Println("Notifications not available on this platform")
}
const available = await runtime.IsNotificationAvailable();
if (!available) {
console.log("Notifications not available on this platform");
}
RequestNotificationAuthorizationโ
Requests permission to display notifications (macOS only). On Windows and Linux, this always returns true.
Go: RequestNotificationAuthorization(ctx context.Context) (bool, error)
JavaScript: RequestNotificationAuthorization(): Promise<boolean>
Returns: Authorization status and error
Example:
authorized, err := runtime.RequestNotificationAuthorization(ctx)
const authorized = await runtime.RequestNotificationAuthorization();
CheckNotificationAuthorizationโ
Checks the current notification authorization status (macOS only). On Windows and Linux, this always returns true.
Go: CheckNotificationAuthorization(ctx context.Context) (bool, error)
JavaScript: CheckNotificationAuthorization(): Promise<boolean>
Returns: Authorization status and error
Example:
authorized, err := runtime.CheckNotificationAuthorization(ctx)
const authorized = await runtime.CheckNotificationAuthorization();
CleanupNotificationsโ
Cleans up notification resources and releases any held connections. This should be called when shutting down the application, particularly on Linux where it closes the D-Bus connection.
Go: CleanupNotifications(ctx context.Context)
JavaScript: CleanupNotifications(): Promise<void>
Example:
runtime.CleanupNotifications(ctx)
await runtime.CleanupNotifications();
SendNotificationโ
Sends a basic notification to the system.
Go: SendNotification(ctx context.Context, options NotificationOptions) error
JavaScript: SendNotification(options: NotificationOptions): Promise<void>
Returns: Error if the notification fails to send
Example:
err := runtime.SendNotification(ctx, runtime.NotificationOptions{
ID: "notif-1",
Title: "Hello",
Body: "This is a notification",
})
await runtime.SendNotification({
id: "notif-1",
title: "Hello",
body: "This is a notification"
});
SendNotificationWithActionsโ
Sends an interactive notification with predefined actions. Requires a registered notification category. If the category is not found or CategoryID is empty, a basic notification will be sent instead.
Go: SendNotificationWithActions(ctx context.Context, options NotificationOptions) error
JavaScript: SendNotificationWithActions(options: NotificationOptions): Promise<void>
Returns: Error if the notification fails to send
Example:
err := runtime.SendNotificationWithActions(ctx, runtime.NotificationOptions{
ID: "notif-2",
Title: "Task Reminder",
Body: "Complete your task",
CategoryID: "TASK_CATEGORY",
})
await runtime.SendNotificationWithActions({
id: "notif-2",
title: "Task Reminder",
body: "Complete your task",
categoryId: "TASK_CATEGORY"
});
RegisterNotificationCategoryโ
Registers a notification category that can be used with interactive notifications. Registering a category with the same ID as a previously registered category will override it.
Go: RegisterNotificationCategory(ctx context.Context, category NotificationCategory) error
JavaScript: RegisterNotificationCategory(category: NotificationCategory): Promise<void>
Returns: Error if registration fails
Example:
err := runtime.RegisterNotificationCategory(ctx, runtime.NotificationCategory{
ID: "TASK_CATEGORY",
Actions: []runtime.NotificationAction{
{ID: "COMPLETE", Title: "Complete"},
{ID: "CANCEL", Title: "Cancel"},
},
})
await runtime.RegisterNotificationCategory({
id: "TASK_CATEGORY",
actions: [
{id: "COMPLETE", title: "Complete"},
{id: "CANCEL", title: "Cancel"}
]
});
RemoveNotificationCategoryโ
Removes a previously registered notification category.
Go: RemoveNotificationCategory(ctx context.Context, categoryId string) error
JavaScript: RemoveNotificationCategory(categoryId: string): Promise<void>
Returns: Error if removal fails
Example:
err := runtime.RemoveNotificationCategory(ctx, "TASK_CATEGORY")
await runtime.RemoveNotificationCategory("TASK_CATEGORY");
RemoveAllPendingNotificationsโ
Removes all pending notifications (macOS and Linux only).
Go: RemoveAllPendingNotifications(ctx context.Context) error
JavaScript: RemoveAllPendingNotifications(): Promise<void>
Returns: Error if removal fails
Example:
err := runtime.RemoveAllPendingNotifications(ctx)
await runtime.RemoveAllPendingNotifications();
RemovePendingNotificationโ
Removes a specific pending notification (macOS and Linux only).
Go: RemovePendingNotification(ctx context.Context, identifier string) error
JavaScript: RemovePendingNotification(identifier: string): Promise<void>
Returns: Error if removal fails
Example:
err := runtime.RemovePendingNotification(ctx, "notif-1")
await runtime.RemovePendingNotification("notif-1");
RemoveAllDeliveredNotificationsโ
Removes all delivered notifications (macOS and Linux only).
Go: RemoveAllDeliveredNotifications(ctx context.Context) error
JavaScript: RemoveAllDeliveredNotifications(): Promise<void>
Returns: Error if removal fails
Example:
err := runtime.RemoveAllDeliveredNotifications(ctx)
await runtime.RemoveAllDeliveredNotifications();
RemoveDeliveredNotificationโ
Removes a specific delivered notification (macOS and Linux only).
Go: RemoveDeliveredNotification(ctx context.Context, identifier string) error
JavaScript: RemoveDeliveredNotification(identifier: string): Promise<void>
Returns: Error if removal fails
Example:
err := runtime.RemoveDeliveredNotification(ctx, "notif-1")
await runtime.RemoveDeliveredNotification("notif-1");
RemoveNotificationโ
Removes a notification by identifier (Linux only). On macOS and Windows, this is a stub that always returns nil.
Go: RemoveNotification(ctx context.Context, identifier string) error
JavaScript: RemoveNotification(identifier: string): Promise<void>
Returns: Error if removal fails
Example:
err := runtime.RemoveNotification(ctx, "notif-1")
await runtime.RemoveNotification("notif-1");
OnNotificationResponseโ
Registers a callback function to handle notification responses when users interact with notifications.
Go: OnNotificationResponse(ctx context.Context, callback func(result NotificationResult))
OnNotificationResponse is not available in the JavaScript runtime. Instead, JavaScript applications should use the Events API to listen for notification responses. From your Go callback, emit an event that your JavaScript code can listen to.
Example:
runtime.OnNotificationResponse(ctx, func(result runtime.NotificationResult) {
if result.Error != nil {
return
}
// Emit an event that JavaScript can listen to
runtime.EventsEmit(ctx, "notification-response", result.Response)
})
runtime.EventsOn("notification-response", (response) => {
console.log("Notification response:", response);
switch (response.actionIdentifier) {
case "COMPLETE":
// Handle complete action
break;
case "CANCEL":
// Handle cancel action
break;
}
});
Optionsโ
NotificationOptionsโ
Go:
type NotificationOptions struct {
ID string `json:"id"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"` // (macOS and Linux only)
Body string `json:"body,omitempty"`
CategoryID string `json:"categoryId,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}
TypeScript:
interface NotificationOptions {
id: string;
title: string;
subtitle?: string; // macOS and Linux only
body?: string;
categoryId?: string;
data?: { [key: string]: any };
}
| Field | Description | Win | Mac | Lin |
|---|---|---|---|---|
| ID | Unique identifier for the notification | โ | โ | โ |
| Title | Main notification title | โ | โ | โ |
| Subtitle | Subtitle text (macOS and Linux only) |