push

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 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 push receives remote push notifications: Firebase Cloud Messaging (FCM) on Android, the Apple Push Notification service (APNs) on iOS and macOS, the Windows Push Notification Services (WNS) on Windows, and UnifiedPush on Linux. Elsewhere its functions return giode.ErrNotSupported.

The app hands Token's result to its server, which sends messages to that device through FCM or APNs. A notification message arriving while the app is in the background is shown by the system; tapping it opens the app and calls Handlers.Open. Messages arriving while the app runs (and data-only messages) go to Handlers.Message.

Setup, beyond importing the package:

  • Android: a Firebase project with the app added, and its google-services.json set as android.google_services in giode.toml. The giode CLI adds Firebase Messaging and the package's service to the app.
  • iOS and macOS: push enabled for the app id in the Apple developer account, and ios.push (or macos.push) set to "development" or "production" in giode.toml, which adds the aps-environment entitlement. The server sends to APNs with the app's key.
  • Windows: the app packaged as MSIX (giode build -target msix) and associated with its Microsoft Store listing; Token is the WNS channel URI, and the server sends to it with the listing's credentials. Toasts arriving while the app isn't running are shown by Windows.
  • Linux: a UnifiedPush distributor on the user's desktop (KDE's KUnifiedPush, ntfy...). Token is the app's endpoint URL; the server posts messages to it, encrypted with Keys (Web Push, RFC 8291). Messages for an app that isn't running start it (the package installs a D-Bus service file for that); the app shows what it wants with giode/notification.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDistributor = errors.New("push: no UnifiedPush distributor on the session bus")

ErrNoDistributor is returned by Token on Linux when no UnifiedPush distributor runs: the user installs one (KUnifiedPush, ntfy's desktop app...).

Functions

func Listen

func Listen(a *giode.App, h Handlers) error

Listen starts delivering push events to h, including the ones that arrived before it (a notification that launched the app). Call it early, before App.Run; a later call replaces the handlers.

func RequestPermission

func RequestPermission(ctx context.Context, a *giode.App) (bool, error)

RequestPermission asks the user to allow notifications, which iOS, macOS and Android 13 and later need before showing them; elsewhere it reports true. It reports whether they're allowed.

func Token

func Token(ctx context.Context) (string, error)

Token returns the device's push token: the FCM registration token on Android, the APNs device token (hex) on iOS and macOS, the WNS channel URI on Windows, the UnifiedPush endpoint URL on Linux. It registers the device with the push service the first time, which needs a network connection.

func Unregister

func Unregister() error

Unregister deletes the device's token, so the server's messages no longer arrive, until Token registers again.

Types

type Handlers

type Handlers struct {
	// Token is called when the device's token changes (FCM rotates
	// them); send the new one to the server.
	Token func(token string)
	// Message is called for a message received while the app runs.
	Message func(Message)
	// Open is called when the user opened the app by tapping a
	// notification.
	Open func(Message)
}

Handlers receive push events. They run on their own goroutine: use App.Post to change the UI.

type Message

type Message struct {
	// ID is the message id FCM or APNs gave it, when there is one.
	ID string
	// Title and Body are the notification's text; empty for a data-only
	// message.
	Title, Body string
	// Data is the message's custom key-value payload.
	Data map[string]string
}

Message is a received push message.

type WebPushKeys

type WebPushKeys struct {
	P256dh string `json:"p256dh"`
	Auth   string `json:"auth"`
}

WebPushKeys are a device's Web Push keys, in the URL-safe base64 Web Push servers expect: the public key (p256dh) and the auth secret.

func Keys

func Keys() (WebPushKeys, error)

Keys returns the device's Web Push keys (Linux), which the server encrypts messages with; the package decrypts them. Messages that aren't encrypted arrive as they are. Elsewhere it returns giode.ErrNotSupported.

Jump to

Keyboard shortcuts

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