notify

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 5 Imported by: 1

README

go-notify: Go bindings for libnotify

go-notify implements Go bindings for libnotify to create, send, and update OS level notifications.

Installation

This package requires Cgo and the libnotify (libnotify-dev) shared library to be installed.

On Debian can install with apt install libnotify-dev on Arch with pacman -S libnotify.

Usage

A simple example without any error checking:

// Get an instance of a Notifier
notifier, _ := notify.NewNotifier("An app name")

// Create and show a notification
notification, _ := notifier.NewNotification(
        "A summary", "Some body text", "a/path/to/an/icon.png")

// Set notification urgency (Low, Normal, Critical)
notification.SetUrgency(notify.Critical)

// Set notification timeout in milliseconds
notification.SetTimeout(10000)

// Add an action and a callback to the notification
cb := func() {
       fmt.Println("Action was used!") 
}
notification.AddAction("actionId", "label", cb)

notification.Show()

// Update an existing notification (or send a new one if one hasn't
// been sent)
notification.Update(
    "A new summary",
    "Some different body text",
    "another/path/to/icon.png",
)

// Remove an existing notification
notification.Close()

See example/example.go for a more complete example.

Roadmap

  • Add API that returns a Notification instance to manage multiple notifications at once
  • Support notification urgencies
  • Support notification timeouts
  • Support actions
  • Better support for notification icons (PixBuf?)

Authors

The original code for these bindings was written by @codegoalie and provided under the MIT license. This fork was written by me, Stefan van den Akker.

Documentation

Overview

Package notify implements Go bindings for libnotify to create, send, and update OS level notifications. It does not shell out to `notify-send` so it can update existing notifications as well as create new ones.

This package requires Cgo and the libnotify (libnotify-dev) shared library to be installed.

On Ubuntu or Debian you can install with: sudo apt-get install libnotify-dev

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDebugLogger added in v0.6.0

func SetDebugLogger(logger Logger)

SetDebugLogger set the Logger that will receive debug logging.

func SetErrorLogger added in v0.6.0

func SetErrorLogger(logger Logger)

SetErrorLogger set the Logger that will receive error logging.

Types

type Logger added in v0.6.0

type Logger func(...interface{})

Logger logs relevant information about the state of the application. Idea stolen from https://old.reddit.com/r/golang/comments/4eh1c3/logging_in_libraries_whats_the_best_practice/

type Notification

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

Notification represents an instance of a single notification

func (*Notification) AddAction added in v0.6.0

func (n *Notification) AddAction(actionId, label string, onActionCallback NotificationActionCallback) error

AddAction adds an action to a notification. `label` is what appears on the notification button. `onActionCallback` is the function invoked when the action is triggered. Multiple actions can be added to the same notification.

func (*Notification) Close

func (n *Notification) Close() error

Close removes the notification from the OS

func (*Notification) SetTimeout added in v0.5.0

func (n *Notification) SetTimeout(timeout Timeout) error

SetTimeout sets the timeout of the notification in milliseconds. To use the default expiration time, pass notify.DefaultTimeout. To set the notification to never expire, pass notify.InfinityTimeout.

func (*Notification) SetUrgency

func (n *Notification) SetUrgency(urgency Urgency) error

SetUrgency sets the notification urgency (Low, Normal, Critical)

func (*Notification) Show

func (n *Notification) Show() error

Show sends the notification to the OS for displaying

func (*Notification) Update

func (n *Notification) Update(summary, body, icon string) error

Update an existing notification with new information

type NotificationActionCallback added in v0.6.0

type NotificationActionCallback func()

NotificationActionCallback is called when an action on a notification is invoked

type Notifier

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

Notifier is an instance of an application sending notifications

func NewNotifier

func NewNotifier(appName string) (*Notifier, error)

NewNotifier initializes a new application to send notifications. It's required to call this function first.

func (*Notifier) ApplicationName

func (n *Notifier) ApplicationName() string

ApplicationName returns the current application's initialized name

func (*Notifier) NewNotification

func (n *Notifier) NewNotification(summary, body, icon string) (*Notification, error)

NewNotification creates a new notification

type Timeout added in v0.5.0

type Timeout int64

Timeout represent the time in milliseconds after which a a notification will be closed. Should be > 0. Two special values exist, the default timeout (denoted by -1) and the infinity timeout (denoted by 0).

const (
	// DefaultTimeout is the default expiration time on a notification.
	DefaultTimeout Timeout = -1

	// InfinityTimeout set the notification to never expire. It stays open
	// until closed by the calling API or the user.
	InfinityTimeout = 0
)

type Urgency

type Urgency int

Urgency represents the urgency of the notification

const (
	Low Urgency = iota
	Normal
	Critical
)

3 levels of urgency:

Low: used for unimportant notifications. Normal: used for most standard notifications. Critical: used for very important notifications.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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