notifykit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 29 Imported by: 2 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 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

View Source
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.

View Source
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

type Endpoint struct {
	Socket    string
	TokenFile string
}

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.

const (
	// Info is routine news.
	Info Level = iota
	// Warn needs attention soon.
	Warn
	// Alert needs attention now.
	Alert
)

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel reads "info", "warn" or "alert".

func (Level) String

func (l Level) String() string

String is "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

func New(cfg Config) (*Notifier, error)

New checks cfg and returns a Notifier. It does no I/O: the macOS helper is assembled on the first Send.

func (*Notifier) Backend

func (n *Notifier) Backend() (string, error)

Backend reports the backend Send uses, choosing it if it has not been chosen yet. Useful for a "doctor" command.

func (*Notifier) Enabled

func (n *Notifier) Enabled() (bool, string)

Enabled reports whether Send would reach a backend, and if not, why.

func (*Notifier) Endpoint

func (n *Notifier) Endpoint() (Endpoint, error)

Endpoint reports where Listen serves, without starting it.

func (*Notifier) Listen

func (n *Notifier) Listen(ctx context.Context) error

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.

func (*Notifier) Send

func (n *Notifier) Send(ctx context.Context, nt Notification) (Result, error)

Send shows nt. A nil error with a non-empty Result.Skipped means delivery is switched off, which is not a failure.

type Recorder

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

Recorder is a Backend that keeps what it is sent, for tests.

func (*Recorder) Name

func (*Recorder) Name() string

Name is "recorder".

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

func Ping(ctx context.Context, ep Endpoint, nt Notification) (Result, error)

Ping hands nt to the application listening at ep and returns what it did with it. The application must be running Notifier.Listen.

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.

Jump to

Keyboard shortcuts

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