Notifications
Wails provides a comprehensive cross-platform notification system for desktop applications. This runtime allows you to display native system notifications with support for interactive elements like action buttons and text input fields.
Notifications are currently unsupported in the JS runtime.
Basic Usage
Initializing Notifications
First, initialize the notification system. This should be called during app startup (typically in OnStartup):
err := runtime.InitializeNotifications(a.ctx)
if err != nil {
// Handle initialization error
// On macOS, this may fail if bundle identifier is not set
}
Then, check if notifications are available on the current platform:
if runtime.IsNotificationAvailable(a.ctx) {
// Notifications are supported
// On macOS, this checks for macOS 10.14+
// On Windows and Linux, this always returns true
}
On macOS, you'll need to request permission before sending notifications:
authorized, err := runtime.CheckNotificationAuthorization(a.ctx)
if err != nil {
// Handle authorization error
}
if !authorized {
authorized, err = runtime.RequestNotificationAuthorization(a.ctx)
if err != nil || !authorized {
// Handle permission denial
}
}
On Windows and Linux, authorization is not required as these platforms don't have permission systems.
Sending Basic Notifications
Send a basic notification with a unique ID, title, optional subtitle (macOS and Linux), and body text:
err := runtime.SendNotification(a.ctx, runtime.NotificationOptions{
ID: "calendar-invite-001",
Title: "New Calendar Invite",
Subtitle: "From: Jane Doe", // Optional - macOS and Linux only
Body: "Tap to view the event",
})
if err != nil {
// Handle error
}
Interactive Notifications
Interactive notifications allow users to respond with button actions or text input. You must first register a notification category that defines the available actions.