Help us shape the future of Go! Take the 2026 Go Developer Survey

notifications

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 11 Imported by: 3

Documentation

Overview

Package notifications provides cross-platform notification capabilities for desktop applications. It supports macOS, Windows, and Linux with a consistent API while handling platform-specific differences internally. Key features include:

  • Basic notifications with title, subtitle, and body
  • Interactive notifications with buttons and actions
  • Notification categories for reusing configurations
  • Custom sounds (default / silent / named)
  • Attached media (images on every platform; audio/video on macOS)
  • Threading / grouping by ThreadID
  • Priority via InterruptionLevel (passive/active/timeSensitive/critical)
  • Scheduled delivery (native on macOS; in-process timer on Windows + Linux)
  • Updating an in-flight notification by ID
  • User feedback handling with a unified callback system

Platform-specific notes:

  • macOS: Requires a properly bundled and signed application. Critical interruption level requires the Critical Alert entitlement; without it the level silently degrades.
  • Windows: Uses Windows Toast notifications via the wintoast subpackage. Reply fields are supported. Scheduled notifications use an in-process timer and are lost if the app exits before delivery. Update-by-ID redelivers as a new notification (true replace requires upstream wintoast support for tag/group).
  • Linux: Uses D-Bus org.freedesktop.Notifications. Reply fields are NOT supported (not part of the spec). Subtitle is concatenated into the body. Scheduled notifications use an in-process timer.

See the NotificationOptions godoc and the notifications example app for the full per-feature support matrix.

Index

Constants

View Source
const (
	InterruptionLevelPassive       = "passive"
	InterruptionLevelActive        = "active"
	InterruptionLevelTimeSensitive = "timeSensitive"
	InterruptionLevelCritical      = "critical"
)

Allowed values for NotificationOptions.InterruptionLevel.

View Source
const DefaultActionIdentifier = "DEFAULT_ACTION"

Variables

This section is empty.

Functions

This section is empty.

Types

type NotificationAction

type NotificationAction struct {
	ID          string `json:"id,omitempty"`
	Title       string `json:"title,omitempty"`
	Destructive bool   `json:"destructive,omitempty"` // (macOS-specific)
}

NotificationAction represents an action button for a notification.

type NotificationAttachment

type NotificationAttachment struct {
	ID   string `json:"id,omitempty"`
	Path string `json:"path"`
	// Type is an optional placement/UTI hint.
	//   On macOS: a UTI like "public.png" / "public.audio" (often inferred).
	//   On Windows: "hero" | "appLogoOverride" | "inline" (default "inline").
	//   On Linux: ignored (always image-path hint).
	Type string `json:"type,omitempty"`
}

NotificationAttachment is a media file shown with the notification. Path is an absolute filesystem path (or "file://" URL on macOS).

type NotificationCategory

type NotificationCategory struct {
	ID               string               `json:"id,omitempty"`
	Actions          []NotificationAction `json:"actions,omitempty"`
	HasReplyField    bool                 `json:"hasReplyField,omitempty"`
	ReplyPlaceholder string               `json:"replyPlaceholder,omitempty"`
	ReplyButtonTitle string               `json:"replyButtonTitle,omitempty"`
}

NotificationCategory groups actions for notifications.

type NotificationOptions

type NotificationOptions struct {
	ID         string                 `json:"id"`
	Title      string                 `json:"title"`
	Subtitle   string                 `json:"subtitle,omitempty"` // (macOS and Linux only)
	Body       string                 `json:"body,omitempty"`
	CategoryID string                 `json:"categoryId,omitempty"`
	Data       map[string]interface{} `json:"data,omitempty"`

	// Sound controls the sound played on delivery.
	//   nil                                -> platform default sound
	//   &NotificationSound{Silent: true}   -> no sound
	//   &NotificationSound{Name: "Ping"}   -> named/bundled sound
	// On macOS, Name is resolved by [UNNotificationSound soundNamed:] and
	// requires the audio file to live under the bundle's Library/Sounds.
	// On Windows, Name is used as-is if it begins with "ms-winsoundevent:" or
	// "ms-appx:"; otherwise it is wrapped in "ms-winsoundevent:" for built-in
	// event names. On Linux it is forwarded as the freedesktop "sound-name"
	// hint (theme-dependent).
	Sound *NotificationSound `json:"sound,omitempty"`

	// Attachments are media files shown alongside the notification. macOS
	// supports multiple attachments of any media type; Windows and Linux
	// honour the first image-typed attachment (Linux limits to one per spec).
	Attachments []NotificationAttachment `json:"attachments,omitempty"`

	// ThreadID groups related notifications together in Notification Center
	// (macOS) / Action Center (Windows) / the notification daemon (Linux).
	ThreadID string `json:"threadId,omitempty"`

	// InterruptionLevel controls notification priority. One of "passive",
	// "active" (default), "timeSensitive", "critical". Critical requires
	// macOS 12+ and the Critical Alert entitlement. Linux maps to the
	// freedesktop urgency hint; Windows maps to <toast scenario="...">.
	InterruptionLevel string `json:"interruptionLevel,omitempty"`

	// Schedule defers delivery. macOS uses a native trigger and persists
	// across app restarts. Windows and Linux fall back to an in-process
	// time.AfterFunc timer that does NOT survive an app exit.
	Schedule *NotificationSchedule `json:"schedule,omitempty"`
}

NotificationOptions contains configuration for a notification.

New optional fields (Sound, Attachments, ThreadID, InterruptionLevel, Schedule) gracefully degrade when a platform cannot honour them; see the package-level godoc for the per-platform support matrix.

type NotificationResponse

type NotificationResponse struct {
	ID               string                 `json:"id,omitempty"`
	ActionIdentifier string                 `json:"actionIdentifier,omitempty"`
	CategoryID       string                 `json:"categoryIdentifier,omitempty"`
	Title            string                 `json:"title,omitempty"`
	Subtitle         string                 `json:"subtitle,omitempty"` // (macOS and Linux only)
	Body             string                 `json:"body,omitempty"`
	UserText         string                 `json:"userText,omitempty"`
	UserInfo         map[string]interface{} `json:"userInfo,omitempty"`
}

NotificationResponse represents the response sent by interacting with a notification.

type NotificationResult

type NotificationResult struct {
	Response NotificationResponse
	Error    error
}

NotificationResult represents the result of a notification response, returning the response or any errors that occurred.

type NotificationSchedule

type NotificationSchedule struct {
	DelaySeconds int   `json:"delaySeconds,omitempty"`
	At           int64 `json:"at,omitempty"`
}

NotificationSchedule defers delivery. Exactly one of DelaySeconds or At must be set. At is interpreted as Unix seconds (UTC).

type NotificationService

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

Service represents the notifications service

var (
	NotificationService_ *NotificationService
)

func New

func New() *NotificationService

Creates a new Notifications Service.

func (*NotificationService) CheckNotificationAuthorization

func (ns *NotificationService) CheckNotificationAuthorization() (bool, error)

func (*NotificationService) OnNotificationResponse

func (ns *NotificationService) OnNotificationResponse(callback func(result NotificationResult))

OnNotificationResponse registers a callback function that will be called when a notification response is received from the user.

func (*NotificationService) RegisterNotificationCategory

func (ns *NotificationService) RegisterNotificationCategory(category NotificationCategory) error

func (*NotificationService) RemoveAllDeliveredNotifications

func (ns *NotificationService) RemoveAllDeliveredNotifications() error

func (*NotificationService) RemoveAllPendingNotifications

func (ns *NotificationService) RemoveAllPendingNotifications() error

func (*NotificationService) RemoveDeliveredNotification

func (ns *NotificationService) RemoveDeliveredNotification(identifier string) error

func (*NotificationService) RemoveNotification

func (ns *NotificationService) RemoveNotification(identifier string) error

func (*NotificationService) RemoveNotificationCategory

func (ns *NotificationService) RemoveNotificationCategory(categoryID string) error

func (*NotificationService) RemovePendingNotification

func (ns *NotificationService) RemovePendingNotification(identifier string) error

func (*NotificationService) RequestNotificationAuthorization

func (ns *NotificationService) RequestNotificationAuthorization() (bool, error)

Public methods that delegate to the implementation.

func (*NotificationService) SendNotification

func (ns *NotificationService) SendNotification(options NotificationOptions) error

func (*NotificationService) SendNotificationWithActions

func (ns *NotificationService) SendNotificationWithActions(options NotificationOptions) error

func (*NotificationService) ServiceName

func (ns *NotificationService) ServiceName() string

ServiceName returns the name of the service.

func (*NotificationService) ServiceShutdown

func (ns *NotificationService) ServiceShutdown() error

ServiceShutdown is called when the service is unloaded.

func (*NotificationService) ServiceStartup

func (ns *NotificationService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error

ServiceStartup is called when the service is loaded.

func (*NotificationService) UpdateNotification

func (ns *NotificationService) UpdateNotification(options NotificationOptions) error

UpdateNotification updates an in-flight notification by ID. On macOS this is auto-deduplicated by UNUserNotificationCenter; on Linux it uses the D-Bus replaces_id parameter. On Windows it currently redelivers as a new notification (true replace requires upstream wintoast support for tag/group).

type NotificationSound

type NotificationSound struct {
	Silent bool   `json:"silent,omitempty"`
	Name   string `json:"name,omitempty"`
}

NotificationSound configures audio playback for a notification.

Jump to

Keyboard shortcuts

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