notify

package
v0.1.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package notify delivers web notifications to the operating system and reports what the user did with them. The browser engine builds a Notification from the Notifications API; a Sink shows it. The native sinks are the freedesktop D-Bus service on Linux (internal/dbus, verified against a real dbus-daemon), the shell's balloon notification on Windows and NSUserNotification on macOS (those two are built and vetted but have never run on a real desktop). When no native service exists, Fallback hands the notification to the browser window, which shows an in-browser toast.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnavailable = errors.New("notify: no desktop notification service is available")

ErrUnavailable is returned by Native when the platform has no usable notification service.

Functions

This section is empty.

Types

type Action

type Action struct {
	ID    string
	Title string
	Icon  string
}

Action is a button on a notification (Notification.actions).

type CloseReason

type CloseReason int

CloseReason says why a notification was closed.

const (
	ReasonUnknown     CloseReason = iota
	ReasonUser                    // the user dismissed it
	ReasonTimeout                 // the system removed it after its time
	ReasonApplication             // Sink.Close was called
)

type Event

type Event struct {
	Kind   Kind
	ID     string
	Action string
	Reason CloseReason
	Err    error
}

Event is something that happened to a shown notification.

type Fallback

type Fallback struct {
	Primary Sink // may be nil
	Toast   ToastFunc
	// contains filtered or unexported fields
}

Fallback tries the native sink first and falls back to an in-browser toast when there is none or it fails.

func NewFallback

func NewFallback(primary Sink, toast ToastFunc) *Fallback

NewFallback combines a (possibly nil) native sink with a toast function.

func (*Fallback) Close

func (f *Fallback) Close(id string)

func (*Fallback) Emit

func (f *Fallback) Emit(e Event)

Emit lets the window report what the user did with a toast.

func (*Fallback) Name

func (f *Fallback) Name() string

func (*Fallback) SetHandler

func (f *Fallback) SetHandler(fn func(Event))

func (*Fallback) Show

func (f *Fallback) Show(n Notification) error

func (*Fallback) Shutdown

func (f *Fallback) Shutdown() error

type Freedesktop

type Freedesktop struct {
	// contains filtered or unexported fields
}

Freedesktop shows notifications through the org.freedesktop.Notifications service of the session bus (the Desktop Notifications Specification): Notify, CloseNotification and the NotificationClosed and ActionInvoked signals. Only signals sent by the current owner of the service name are believed.

func NewFreedesktop

func NewFreedesktop(conn *dbus.Conn, appName string) (*Freedesktop, error)

NewFreedesktop uses an established connection (a bus connection, or a peer connection to a service in tests).

func OpenFreedesktop

func OpenFreedesktop(appName string) (*Freedesktop, error)

OpenFreedesktop connects to the session bus and prepares the sink. It fails (ErrUnavailable) when there is no session bus or no notification service on it.

func (*Freedesktop) Capabilities

func (f *Freedesktop) Capabilities() []string

Capabilities returns what the service reported ("actions", "body", "body-markup", "persistence", ...).

func (*Freedesktop) Close

func (f *Freedesktop) Close(id string)

func (*Freedesktop) Name

func (f *Freedesktop) Name() string

func (*Freedesktop) SetHandler

func (f *Freedesktop) SetHandler(fn func(Event))

func (*Freedesktop) Show

func (f *Freedesktop) Show(n Notification) error

func (*Freedesktop) Shutdown

func (f *Freedesktop) Shutdown() error

type Kind

type Kind int

Kind of Event.

const (
	// Click: the user activated the notification body.
	Click Kind = iota + 1
	// ActionClick: the user pressed one of the action buttons (Event.Action is its id).
	ActionClick
	// Closed: the notification went away without a click (Event.Reason says why).
	Closed
	// Error: the notification could not be shown after Show returned.
	Error
)

type Lazy

type Lazy struct {
	// contains filtered or unexported fields
}

Lazy opens a native sink the first time a notification is shown, so that a browser that never shows one never connects to the desktop's notification service (and tests on a developer's desktop do not talk to it). When opening fails, the failure is remembered for retryAfter before the next attempt.

func NewLazy

func NewLazy(open func() (Sink, error)) *Lazy

NewLazy wraps an opener such as func() (Sink, error) { return Native("Sweef") }.

func (*Lazy) Close

func (l *Lazy) Close(id string)

func (*Lazy) Name

func (l *Lazy) Name() string

func (*Lazy) SetHandler

func (l *Lazy) SetHandler(f func(Event))

func (*Lazy) Show

func (l *Lazy) Show(n Notification) error

func (*Lazy) Shutdown

func (l *Lazy) Shutdown() error

type Notification

type Notification struct {
	ID                 string // unique per notification, assigned by the browser
	Origin             string // "https://example.com": shown so the user knows who is speaking
	Title, Body        string
	IconPath           string // a local image file for the icon ("" = the browser's)
	Tag                string // a notification with the same origin and tag replaces the earlier one
	Renotify           bool
	Silent             bool
	RequireInteraction bool
	Timestamp          int64 // milliseconds since the epoch, 0 = now
	Actions            []Action
	Lang, Dir          string
}

Notification is what is shown. Strings are plain text; sinks escape them for their platform.

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder is a Sink that remembers everything and lets a test play the user.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder makes an empty recording sink.

func (*Recorder) Click

func (r *Recorder) Click(id string)

Click plays the user clicking a notification.

func (*Recorder) ClickAction

func (r *Recorder) ClickAction(id, action string)

ClickAction plays the user pressing an action button.

func (*Recorder) Close

func (r *Recorder) Close(id string)

func (*Recorder) Closed

func (r *Recorder) Closed() []string

Closed returns the ids passed to Close.

func (*Recorder) Dismiss

func (r *Recorder) Dismiss(id string)

Dismiss plays the user closing a notification.

func (*Recorder) FailNext

func (r *Recorder) FailNext(err error)

func (*Recorder) Last

func (r *Recorder) Last() (Notification, bool)

Last returns the most recent notification.

func (*Recorder) Name

func (r *Recorder) Name() string

func (*Recorder) SetHandler

func (r *Recorder) SetHandler(f func(Event))

func (*Recorder) Show

func (r *Recorder) Show(n Notification) error

func (*Recorder) Shown

func (r *Recorder) Shown() []Notification

Shown returns every notification shown so far, oldest first.

func (*Recorder) Shutdown

func (r *Recorder) Shutdown() error

type Sink

type Sink interface {
	// Name is a short description for diagnostics ("freedesktop", "recorder", "toast").
	Name() string
	// Show displays n. It returns when the system accepted it; what the user does comes through the handler.
	Show(n Notification) error
	// Close removes a shown notification (no Closed event follows for it).
	Close(id string)
	// SetHandler registers the receiver of user events. It may be called from any goroutine and never blocks.
	SetHandler(func(Event))
	// Shutdown releases the sink's resources.
	Shutdown() error
}

Sink shows notifications.

func Native

func Native(appName string) (Sink, error)

Native opens the platform's notification service: org.freedesktop.Notifications on the session bus.

type ToastFunc

type ToastFunc func(Notification)

ToastFunc shows a notification inside the browser window (the app's toast).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL