push

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package push receives notifications from your server: APNs on iOS, Firebase Cloud Messaging on Android, and Web Push in a browser.

nimbus native plugin add push

Android needs your Firebase project's google-services.json, at native/android/app/google-services.json. On iOS, turn on Push Notifications for the app in your Apple developer account. The web needs a VAPID key pair (see NewVAPIDKeys), and your server sends to browsers with SendWeb.

ok, err := push.Request(ctx)
t, err := push.GetToken(ctx)
// send t.Value and t.Service to your server
go push.Watch(ctx, func(m push.Message) { openOrder(m.Data["order"]) })

Index

Constants

This section is empty.

Variables

View Source
var ErrGone = errors.New("push: subscription expired or unsubscribed")

ErrGone is the browser having unsubscribed: delete the token.

View Source
var VAPID = Keys{
	Public:  os.Getenv("NIMBUS_VAPID_PUBLIC"),
	Private: os.Getenv("NIMBUS_VAPID_PRIVATE"),
	Subject: os.Getenv("NIMBUS_VAPID_SUBJECT"),
}

VAPID is the key pair GetToken and SendWeb use, from NIMBUS_VAPID_PUBLIC, NIMBUS_VAPID_PRIVATE and NIMBUS_VAPID_SUBJECT unless you set it.

Functions

func Request

func Request(ctx context.Context) (bool, error)

Request asks to show notifications, once.

func SendWeb added in v0.0.14

func SendWeb(ctx context.Context, token string, n Notification) error

SendWeb sends a notification to a browser, by the Value of a "web" Token. It returns ErrGone when the subscription has ended.

func Watch

func Watch(ctx context.Context, f func(Message)) error

Watch calls f with each message until ctx ends, including the tap that opened the app. It blocks, so run it from a goroutine in an effect.

Types

type Keys added in v0.0.14

type Keys struct {
	Public  string // base64url; the browser subscribes with it
	Private string // base64url; keep it on your server
	Subject string // "mailto:you@example.com" or your site's https address, for push services to reach you
}

Keys is a VAPID key pair: it tells browsers' push services that a message comes from your server. Make one with NewVAPIDKeys, once, and keep it; a new pair orphans every browser that subscribed with the old one.

func NewVAPIDKeys added in v0.0.14

func NewVAPIDKeys() (Keys, error)

NewVAPIDKeys makes a key pair for the web. Run it once and store the pair as NIMBUS_VAPID_PUBLIC and NIMBUS_VAPID_PRIVATE.

type Message

type Message struct {
	Title  string            `json:"title"`  // its heading
	Body   string            `json:"body"`   // its text
	Data   map[string]string `json:"data"`   // what your server sent with it
	Tapped bool              `json:"tapped"` // whether they tapped it to get here
}

Message is a notification that arrived while the app was open, or one that was tapped.

func Messages

func Messages(ctx context.Context) ([]Message, error)

Messages returns what has arrived since the last call.

type Notification added in v0.0.14

type Notification struct {
	Title string            `json:"title"`          // its heading
	Body  string            `json:"body"`           // its text
	Data  map[string]string `json:"data,omitempty"` // Message.Data on arrival
	URL   string            `json:"url,omitempty"`  // the page a tap opens, such as "/orders/42"; "/" by default
	Tag   string            `json:"tag,omitempty"`  // a new one with the same tag replaces the old
	TTL   time.Duration     `json:"-"`              // how long the push service keeps trying (default a day)
}

Notification is what SendWeb shows.

type Token

type Token struct {
	Value   string `json:"value"`   // send this to your server
	Service string `json:"service"` // "apns", "fcm" or "web"
}

Token is where your server sends to reach this phone.

func GetToken

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

GetToken registers with APNs or Firebase and returns this phone's token. It can change: send it to your server each time the app starts. In a browser it subscribes with your VAPID public key, and Value is the subscription, for SendWeb.

Jump to

Keyboard shortcuts

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