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 ¶
- Variables
- type Action
- type CloseReason
- type Event
- type Fallback
- type Freedesktop
- type Kind
- type Lazy
- type Notification
- type Recorder
- func (r *Recorder) Click(id string)
- func (r *Recorder) ClickAction(id, action string)
- func (r *Recorder) Close(id string)
- func (r *Recorder) Closed() []string
- func (r *Recorder) Dismiss(id string)
- func (r *Recorder) FailNext(err error)
- func (r *Recorder) Last() (Notification, bool)
- func (r *Recorder) Name() string
- func (r *Recorder) SetHandler(f func(Event))
- func (r *Recorder) Show(n Notification) error
- func (r *Recorder) Shown() []Notification
- func (r *Recorder) Shutdown() error
- type Sink
- type ToastFunc
Constants ¶
This section is empty.
Variables ¶
ErrUnavailable is returned by Native when the platform has no usable notification service.
Functions ¶
This section is empty.
Types ¶
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 ¶
NewFallback combines a (possibly nil) native sink with a toast function.
func (*Fallback) SetHandler ¶
func (*Fallback) Show ¶
func (f *Fallback) Show(n Notification) 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 (*Lazy) SetHandler ¶
func (*Lazy) Show ¶
func (l *Lazy) Show(n Notification) 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 (*Recorder) ClickAction ¶
ClickAction plays the user pressing an action button.
func (*Recorder) Last ¶
func (r *Recorder) Last() (Notification, bool)
Last returns the most recent notification.
func (*Recorder) SetHandler ¶
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.
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.
type ToastFunc ¶
type ToastFunc func(Notification)
ToastFunc shows a notification inside the browser window (the app's toast).