Documentation
¶
Overview ¶
Package notifykit posts native desktop notifications under the calling application's own name and icon.
On macOS it assembles a small signed helper app, "<AppName> Notifier.app", from a prebuilt notifier embedded in this package, so a banner says "sange" with sange's icon rather than "Script Editor". It falls back to the osascript macOS provides when the helper cannot be used, and never to a third-party notifier: every app ships its own helper. On Linux and the BSDs it uses notify-send, and on Windows a PowerShell toast.
Desktop delivery switches itself off under `go test` and in CI, so a test suite never puts a banner on a developer's screen. Environment variables turn it on or off explicitly (see Config.EnvPrefix).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupported means the platform has no usable notifier. ErrUnsupported = errors.New("notifykit: no desktop notifier is available here") // ErrDenied means the user has not allowed notifications for this app. ErrDenied = errors.New("notifykit: notifications are not allowed for this app (System Settings > Notifications)") )
Errors returned by Send.
var ErrInvalid = errors.New("notifykit: invalid notification")
ErrInvalid wraps every refusal of a notification's icon or click action.
Functions ¶
func RunCLI ¶
func RunCLI(ctx context.Context, n *Notifier, args []string, stdin io.Reader, stdout, stderr io.Writer) int
RunCLI is a ready-made `notify` command for an application to mount, so every app offers the same three verbs:
<app> notify send [flags] show a notification from this process <app> notify ping [flags] hand one to the running app (its Listen) <app> notify doctor say whether notifications work, and why not
Flags: -title (or -message), -subtitle, -body (or -description), -icon, -open, -group, -level info|warn|alert, -sound, and -json to read one notification as JSON from stdin instead. It returns the process exit status: 0 done, 1 failed, 2 usage.
Types ¶
type Backend ¶
type Backend interface {
Name() string
Send(ctx context.Context, nt Notification) error
}
Backend delivers a notification. Implement it to send somewhere else, or use a Recorder in tests.
type Config ¶
type Config struct {
// AppName is the name the user sees, e.g. "sange". Required.
AppName string
// AppID is a reverse-DNS identifier for the product, from its own domain
// (e.g. "sh.sange.notify"), else "com.simtabi.<binary>". Required on macOS.
AppID string
// BundleID is the macOS helper app's bundle identifier, which is what
// System Settings > Notifications remembers permission by. Empty means
// AppID+".notifier". Set it to keep the permission an existing helper app
// already has.
BundleID string
// Icon is a square PNG, ideally 1024x1024. Optional: without it the macOS
// helper shows the generic app icon and notify-send shows none.
Icon []byte
// EnvPrefix names the per-application switches, e.g. "SANGE" reads
// SANGE_NOTIFY_DESKTOP (1/0, true/false, on/off) and SANGE_NOTIFY_BACKEND.
// Empty means the application has no switches of its own; the global
// NOTIFYKIT_DESKTOP and NOTIFYKIT_BACKEND still apply.
EnvPrefix string
// Disabled turns desktop delivery off whatever the environment says.
Disabled bool
// StateDir is where the macOS helper app and the icon file are written.
// Empty means $XDG_STATE_HOME/<app>/notifykit, else
// ~/.local/state/<app>/notifykit (%LocalAppData%\<app>\notifykit on
// Windows).
StateDir string
// MinInterval suppresses a repeat of the same Group within this long.
// Zero means never suppress.
MinInterval time.Duration
// Timeout bounds one delivery. Zero means 10s.
Timeout time.Duration
// Backend replaces the platform backend, e.g. a [Recorder] in tests. A
// Backend set here is always used unless Disabled is true: the test and CI
// switches only stop real desktop delivery.
Backend Backend
// Getenv reads the environment. Nil means os.Getenv.
Getenv func(string) string
// Log receives one line per fallback or failure. Nil discards them.
Log io.Writer
}
Config describes the application the notifications come from.
type Endpoint ¶
Endpoint is where a running application accepts notifications from other local processes: a Unix-domain socket and the file holding the token every message must carry. Both live in the application's state directory, which only the user can enter.
type Level ¶
type Level int
Level is how urgent a notification is. Backends that have urgency map it (notify-send's --urgency); the others ignore it.
func ParseLevel ¶
ParseLevel reads "info", "warn" or "alert".
type Notification ¶
type Notification struct {
Title string `json:"title"` // the message: the first line; the app name when empty
Subtitle string `json:"subtitle,omitempty"` // second line (macOS only; folded into Body elsewhere)
Body string `json:"body,omitempty"` // the description: the text under the title
Level Level `json:"level,omitempty"`
// Group collapses related notifications: on macOS a new notification in a
// group replaces the previous one in Notification Center, and with
// [Config.MinInterval] set, repeats within the interval are suppressed.
Group string `json:"group,omitempty"`
Sound bool `json:"sound,omitempty"` // play the default sound, where the backend can
// Icon is an image for this notification only: an absolute path to a PNG,
// JPEG or GIF of at most 10 MB, not a symlink. On macOS it is attached to the
// banner (the app's own icon still marks it); on Linux and Windows it
// replaces the app's icon. Empty means the app's Config.Icon. It is checked
// and copied into the state directory, so the file itself is never moved.
Icon string `json:"icon,omitempty"`
// Open is run when the notification is clicked, with no shell: an absolute
// path to this program's own executable (run with no arguments), or an
// http(s) URL on 127.0.0.1, localhost or ::1. Anything else is refused. Only
// the macOS helper acts on a click.
Open string `json:"open,omitempty"`
}
Notification is one banner. Only Title has a default (the app name); every other field may be left empty.
type Notifier ¶
type Notifier struct {
// contains filtered or unexported fields
}
Notifier sends notifications for one application. It is safe for concurrent use.
func New ¶
New checks cfg and returns a Notifier. It does no I/O: the macOS helper is assembled on the first Send.
func (*Notifier) Backend ¶
Backend reports the backend Send uses, choosing it if it has not been chosen yet. Useful for a "doctor" command.
func (*Notifier) Listen ¶
Listen accepts notifications from other local processes (Ping, or `<app> notify ping`) until ctx ends, and shows each through Send. Every start writes a fresh random token; a message without it is refused.
Only the user can reach the socket: the state directory is made 0700, the socket and token 0600. A message is one JSON line of at most 16 KiB, read within five seconds, and its text is cleaned like any other notification, so a sender can change what a banner says but nothing else.
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder is a Backend that keeps what it is sent, for tests.
func (*Recorder) Send ¶
func (r *Recorder) Send(_ context.Context, nt Notification) error
Send records nt.
func (*Recorder) Sent ¶
func (r *Recorder) Sent() []Notification
Sent returns a copy of everything recorded so far.
type Result ¶
type Result struct {
Backend string // the backend that delivered it, empty when none did
Suppressed bool // dropped by MinInterval
Skipped string // why desktop delivery is off, when it is
// Pending means macOS is still asking the user whether to allow this
// app's notifications (the first run). It shows once they allow it.
Pending bool
}
Result says what happened to one notification.
func Ping ¶
Ping hands nt to the application listening at ep and returns what it did with it. The application must be running Notifier.Listen.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
try
command
Command try posts one real notification from a throwaway app called "notifykit-try", so a developer can see the banner, its name and its icon.
|
Command try posts one real notification from a throwaway app called "notifykit-try", so a developer can see the banner, its name and its icon. |