Skip to content

notify ​

go
import "github.com/tangoui/tango/sys/notify"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package notify shows system notifications (Notification Center on macOS, the notification area on Windows, the freedesktop notification service on Linux) and reports clicks on them.

if ok, err := notify.RequestPermission(a); ok && err == nil {
	id, err := notify.Send(a, notify.Message{Title: "Build done", Body: "3 warnings"})
	...
}
notify.OnClick(a, func(id string) { win.Show(); win.Focus() })

Functions are safe from any goroutine once the app runs. They return ErrUnsupported when the backend has no notifications and tango.ErrNotRunning after the app quit.

Platform notes: on macOS notifications need the app to run from a .app bundle with a bundle identifier, and the user must allow them. A bare go run or go build executable returns an error. If RequestPermission returns "Notifications are not allowed for this application" when testing a bundle from a temporary directory, quit the app, move the signed bundle to /Applications or ~/Applications and relaunch it with Finder or open. Check the app in System Settings > Notifications if permission was already requested. See the notify guide.

Windows shows WinRT toast notifications. The app's ID (else Name) becomes the process's AppUserModelID and is registered under HKCU so an app without a Start-menu shortcut can show toasts; a click on a toast still in the Action Center calls OnClick while the app runs, but cannot launch a closed app (that needs a shortcut or package with the same AUMID and a COM activator). Where toasts are unavailable or turned off — or with TANGO_WINDOWS_TOAST=0 — they fall back to notification-area balloons. On Linux they go to org.freedesktop.Notifications over D-Bus. On iOS they are local UNUserNotificationCenter notifications, shown as banners also while the app is in the foreground; Send asks for permission the first time. On Android they go to a "Notifications" channel (a "Silent notifications" one for Silent); on Android 13+ the app must declare "notifications" in tan.json (POST_NOTIFICATIONS) and Send asks the first time. On both, a tap opens the app and calls OnClick; a tap on a notification of an earlier run (including the one that launched the app) arrives through OnClick too.

变量 ​

go
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

函数 ​

func OnClick ​

go
func OnClick(a *tango.App, fn func(id string))

OnClick registers fn to run on the main thread when the user clicks a notification this app sent. On macOS, iOS and Android, this includes notifications sent by an earlier run, including the click that launched the app. Register before Run or in OnReady to receive the launch click. IDs stay unique across runs.

func RequestPermission ​

go
func RequestPermission(a *tango.App) (bool, error)

RequestPermission asks the user, if the platform requires it and they were not asked before, to allow notifications. It reports whether notifications may be shown. It blocks until the user answers; called on the main thread it keeps the event loop running meanwhile.

func Send ​

go
func Send(a *tango.App, m Message) (string, error)

Send shows a notification and returns its id, which OnClick handlers receive when the user clicks it.

类型 ​

Message ​

go
type Message struct {
	Title    string
	Subtitle string // shown under the title on macOS; merged into the body elsewhere
	Body     string
	Silent   bool // no sound
}

Message is the content of a notification.

以 MIT 许可发布