push

package
v0.167.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 25 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 delivers notifications to people's phones through Apple's push service (APNs), and seals what a phone hands its server so that only the relay that sealed it can use it.

A TeaNode server never talks to Apple for its own phones unless it is the relay: Apple takes notifications for an app only from a key of the developer account that publishes it. The relay (internal/api/v1api/apipush) holds that key; every other server sends to the relay (relay_client.go), and what it sends is already encrypted for the phone (payload.go).

Index

Constants

View Source
const (
	ProductionHost = "https://api.push.apple.com"
	SandboxHost    = "https://api.sandbox.push.apple.com"
)

The two APNs hosts: production for App Store and TestFlight builds, the sandbox for builds installed from Xcode.

View Source
const (
	PushTypeAlert        = "alert"
	PushTypeLiveActivity = "liveactivity"
)

Push types: an ordinary notification, or an update to a Live Activity on the lock screen.

View Source
const (
	PathRegistrations = api.PathPushRegistrations
	PathNotifications = api.PathPushNotifications
)

Paths of the relay's two endpoints, under the relay's address.

View Source
const (
	ErrorCodeGone      = "gone"
	ErrorCodeTooLarge  = "too_large"
	ErrorCodeRefused   = "refused"
	ErrorCodeLimited   = "limited"
	ErrorCodeUnreached = "unreached"
)

Error codes the relay answers with, which a sender maps back to errors.

View Source
const PhoneKeyBytes = 32

PhoneKeyBytes is the length of a phone's payload key.

Variables

View Source
var (
	// ErrGone is APNs saying the device token is no longer valid: the app
	// was deleted, or the phone gave the app a new token.
	ErrGone = errors.New("push: the phone is no longer registered for notifications")

	// ErrTooLarge is a payload over what APNs takes.
	ErrTooLarge = errors.New("push: the notification is too large")

	// ErrRefused is APNs refusing the request for a reason a retry will not
	// change, such as a topic the key does not cover.
	ErrRefused = errors.New("push: Apple refused the notification")
)

What a send can come to besides success. A caller acts on these: a gone device is deleted, the others are retried later or reported.

Functions

func ParseKey

func ParseKey(keyPEM string) (*ecdsa.PrivateKey, error)

ParseKey reads an APNs authentication key, a PKCS #8 P-256 key: the .p8 file Apple hands out, or the base64 between its first and last lines on one line, which is what a secret read from a pipe can carry.

Types

type APNs

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

APNs sends notifications to Apple with a token-based authentication key.

func NewAPNs

func NewAPNs(keyID, teamID, topic, keyPEM string) (*APNs, error)

NewAPNs builds a client from an APNs authentication key: its identifier, the team it belongs to, the app's bundle identifier, and the .p8 file.

func (*APNs) Send

func (self *APNs) Send(ctx context.Context, deviceToken string, isSandbox bool, notification *Notification) error

Send delivers one notification to one device.

func (*APNs) UseHosts

func (self *APNs) UseHosts(client *http.Client, productionHost, sandboxHost string)

UseHosts sends through client to the given hosts instead of Apple's: for a test standing in a server of its own.

type Button

type Button struct {
	Label string `json:"label"`

	// ButtonAction is what pressing it does: approve, deny, answer:<option
	// number>, reply or open.
	ButtonAction string `json:"buttonAction"`
}

Button is one action on a notification.

type Content

type Content struct {
	// NotificationKind is what this is: alert, herdr_question,
	// goal_needs_you, speak_first, approval, question, answer or test.
	NotificationKind string `json:"notificationKind"`

	ConversationID string `json:"conversationId,omitempty"`
	RunID          string `json:"runId,omitempty"`
	CallID         string `json:"callId,omitempty"`

	Title string `json:"title"`
	Body  string `json:"body"`

	// Buttons are what can be pressed on the notification itself.
	Buttons []Button `json:"buttons,omitempty"`

	// ThreadID groups notifications on the lock screen.
	ThreadID string `json:"threadId,omitempty"`

	// For a coding session's question: where to send the answer.
	Computer            string `json:"computer,omitempty"`
	PaneID              string `json:"paneId,omitempty"`
	QuestionFingerprint string `json:"questionFingerprint,omitempty"`
}

Content is a notification as the phone shows it, before encryption.

func DecryptContent

func DecryptContent(key []byte, sealed string) (*Content, error)

DecryptContent reverses EncryptContent's sealing, for tests and for anybody checking what a phone will see.

type DeliverRequest

type DeliverRequest struct {
	PushRegistration string    `json:"pushRegistration"`
	Delivery         *Delivery `json:"delivery"`
}

DeliverRequest is what a server sends the relay for one notification.

type Delivery

type Delivery struct {
	// Sealed is the content, encrypted for the phone.
	Sealed string `json:"sealed"`

	// ThreadID groups notifications on the lock screen, and CollapseID makes
	// a newer notification replace an older one. Both are hashed with the
	// phone's key, so Apple and the relay never see a conversation or a
	// card's identifier.
	ThreadID   string `json:"threadId,omitempty"`
	CollapseID string `json:"collapseId,omitempty"`

	// ExpiresAt is when Apple gives up on a phone it cannot reach; zero is
	// deliveryLifetime from now.
	ExpiresAt time.Time `json:"expiresAt,omitzero"`

	// ActivityState is, for a Live Activity, the one word it shows: one of
	// activityStates. A Live Activity's delivery carries nothing else.
	ActivityState string `json:"activityState,omitempty"`

	// IsActivityEnded ends the Live Activity, showing ActivityState.
	IsActivityEnded bool `json:"isActivityEnded,omitempty"`

	// ActivityChangedAt is when the turn came to ActivityState, on the
	// server's clock. iOS shows the update with the latest aps.timestamp, so
	// it is what orders a turn's states; the relay's own clock would order
	// them by when each reached it.
	ActivityChangedAt time.Time `json:"activityChangedAt,omitzero"`
}

Delivery is a notification as a server hands it to the relay: the content already encrypted for the phone, and the little Apple needs to route it. The relay writes everything Apple shows itself (relay.go), so a server holding a registration can make the phone show only an encrypted notification from the app, never words of its own.

func EncryptContent

func EncryptContent(key []byte, content *Content) (*Delivery, error)

EncryptContent seals content for the phone holding key, shortening the body when the payload the relay will make of it would be over what APNs takes.

type ErrorResponse

type ErrorResponse struct {
	ErrorCode    string `json:"errorCode"`
	ErrorMessage string `json:"errorMessage"`
}

ErrorResponse is what the relay answers when it does not deliver.

type Notification

type Notification struct {
	// PushType is PushTypeAlert or PushTypeLiveActivity.
	PushType string `json:"pushType"`

	// Priority is 10 to deliver now, 5 to let the phone save power.
	Priority int `json:"priority,omitempty"`

	// CollapseID makes a newer notification replace an older one with the
	// same identifier, such as a coding session's latest state.
	CollapseID string `json:"collapseId,omitempty"`

	// ExpiresAt is when APNs should give up on a phone it cannot reach.
	// Zero delivers once or not at all.
	ExpiresAt time.Time `json:"expiresAt,omitzero"`

	// Payload is the JSON APNs delivers: the "aps" dictionary and anything
	// beside it.
	Payload json.RawMessage `json:"payload"`
}

Notification is what is delivered: the payload APNs passes to the phone, and how APNs should treat it.

type RegisterRequest

type RegisterRequest struct {
	DeviceToken string `json:"deviceToken"`
	IsSandbox   bool   `json:"isSandbox"`

	// PushType is PushTypeAlert, the phone itself, which is the default, or
	// PushTypeLiveActivity, one Live Activity's push token.
	PushType string `json:"pushType,omitempty"`
}

RegisterRequest is what a phone sends the relay to register.

type RegisterResponse

type RegisterResponse struct {
	PushRegistration string `json:"pushRegistration"`
}

RegisterResponse is the sealed registration the phone hands its server.

type Registration

type Registration struct {
	DeviceToken string    `json:"deviceToken"`
	IsSandbox   bool      `json:"isSandbox"`
	PushType    string    `json:"pushType"`
	CreatedAt   time.Time `json:"createdAt"`
}

Registration is what a sealed registration holds.

type Relay

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

Relay is what a relay does, whether asked over HTTP by another server (internal/api/v1api/apipush) or by its own server, which sends to itself without the network.

func NewRelay

func NewRelay(apns *APNs) (*Relay, error)

NewRelay builds a relay around its APNs client, sealing registrations with a key made from the client's.

func (*Relay) DeliverTo

func (self *Relay) DeliverTo(ctx context.Context, registration *Registration, delivery *Delivery, now time.Time) error

DeliverTo sends a delivery to an opened registration's phone: an alert to the phone, or a state to one Live Activity.

func (*Relay) Open

func (self *Relay) Open(pushRegistration string, now time.Time) (*Registration, error)

Open reads the registration a server presents.

func (*Relay) Register

func (self *Relay) Register(deviceToken string, isSandbox bool, pushType string, now time.Time) (string, error)

Register seals a phone's device token, or one Live Activity's push token, into the registration it hands its server.

A Live Activity's update is shown by iOS as it arrives, with no extension in between to decrypt it, so it crosses Apple and the relay readable. It therefore carries no words at all: the relay writes it from one of a few states (activityStates), and the app's widget says what each means.

func (*Relay) Send

func (self *Relay) Send(ctx context.Context, pushRegistration string, delivery *Delivery) error

Send lets a server that is the relay send to itself.

type RelayClient

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

RelayClient sends notifications to a relay over HTTP.

func NewRelayClient

func NewRelayClient(baseURL string) *RelayClient

NewRelayClient sends to the relay at baseURL, such as https://mail.teanode.com.

func (*RelayClient) Send

func (self *RelayClient) Send(ctx context.Context, pushRegistration string, delivery *Delivery) error

Send asks the relay to deliver a notification.

type Sealer

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

Sealer seals and opens registrations.

func NewSealer

func NewSealer(keyID string, key *ecdsa.PrivateKey) (*Sealer, error)

NewSealer builds a sealer from the relay's APNs key, the one secret only the relay has, rather than from the server secret, which an operator may rotate for reasons of their own. The key's identifier is written in front of every registration: a relay given another key answers the old registrations as gone, so servers clear them and the phones register again.

func (*Sealer) Open

func (self *Sealer) Open(sealed string, now time.Time) (*Registration, error)

Open reads a sealed registration. One sealed under another key, or past its lifetime, is ErrGone: the server should forget it and wait for the phone to register again. One that does not open is ErrRefused.

func (*Sealer) Seal

func (self *Sealer) Seal(registration *Registration) (string, error)

Seal writes a registration as the opaque text a phone hands its server.

type Sender

type Sender interface {
	Send(ctx context.Context, pushRegistration string, delivery *Delivery) error
}

Sender is how a server sends a notification for one of its phones: to the relay over HTTP, or to itself when it is the relay.

Jump to

Keyboard shortcuts

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