notify

package module
v0.0.0-...-33951df Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: MIT, Unlicense Imports: 1 Imported by: 1

README

go-notify-darwin

This package binds to Apple's UNNotificationn framework in order to create rich notifications. A large part of the API is surfaced, however no guarantee is made about feature completeness or API stability.

Usage

Register your categories with notify.Init. Set your callback with notify.SetCallback. This will be called upon user interaction. Push notifications with notify.Notify.

Conveniences

In order to make this API convenient it allows you to specify actions within notifications directly. On the backend an ad-hoc UNNotificationCategory is created for that notification. The traditional method on macOS is to rather register all of your application's notification categories at init time, which you can do via the notify.Init function.

Caveats

For notification interactions to work you must have an NSApp running. The example relies on Gio for this.

Example

To run the example:

cd ./example && go generate . && ./example.app/Contents/MacOS/noti

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cancel

func Cancel()

func Init

func Init(categories ...Category)

func Notify

func Notify(n Notification)

func SetCallback

func SetCallback(cb CallbackFunc)

func SetLogger

func SetLogger(l *slog.Logger)

Types

type Action

type Action struct {
	ID    ActionID // globally unique, used in callback
	Title string
	Icon  string // name of asset within bundle to use as icon; TODO: could support system defined icons as well
}

Action is a button action.

type ActionID

type ActionID = string

type CallbackArgs

type CallbackArgs struct {
	Category CategoryID
	Action   ActionID
	UserData UserData // data attached to the notification by the application
	UserText string   // text input by the user (applies to text actions)
	Err      error    // any error that occured processing the user data
}

CallbackArgs contains the data from a user interaction of a notification.

type CallbackFunc

type CallbackFunc func(CallbackArgs)

CallbackFunc signature. Switch on the action to know how to interpret the dynamic data.

type Category

type Category struct {
	ID               CategoryID
	Summary          string // summarise the category
	Preview          string // hidden preview body placeholder
	Actions          []Action
	TextInputActions []TextInputAction
}

Category defines a set of actions that a notification can reference by ID to have them be applied.

type CategoryID

type CategoryID = string

type Notification

type Notification struct {
	Title    string
	Subtitle string
	Body     string

	// UserData is app supplied data that is passed back upon user interaction.
	UserData UserData

	// Attachments is a list of image names.
	// If the image name is an absolute path it will be followed literally.
	// If the image name is just a base name, the bundle will be searched for it.
	// The named image must exist on disk, must be PNG, JPEG or GIF and must be
	// less than 10MB.
	Attachments []string

	// Category to apply. If supplied the actions attached on this notification will be
	// ignored and the pre-existing category used.
	Category CategoryID

	// Actions, if supplied here, will provoke an ad-hoc Category to be created to
	// contain them on the native side. This is for convenience.
	Actions          []Action          // button action
	TextInputActions []TextInputAction // text input action
}

Notification describes

type TextInputAction

type TextInputAction struct {
	ID          ActionID // globally unique, used in callback
	Title       string
	Icon        string // name of asset within bundle to use as icon; TODO: could support system defined icons as well
	ButtonTitle string
	Placeholder string
}

TextInputAction allows for text input from user.

type UserData

type UserData = map[string]string

type UserText

type UserText = string

Jump to

Keyboard shortcuts

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