Documentation
¶
Overview ¶
Package uebpush provides a Go library for receiving Web Push notifications via Google Cloud Messaging (GCM) / Firebase Cloud Messaging (FCM).
Index ¶
- type Client
- func (c *Client) ClearPersistentIDs() error
- func (c *Client) Close() error
- func (c *Client) Credentials() *credentials.Credentials
- func (c *Client) Listen(ctx context.Context) error
- func (c *Client) PersistentIDs() []string
- func (c *Client) Register(ctx context.Context) (*SubscriptionInfo, error)
- func (c *Client) Run(ctx context.Context) error
- func (c *Client) SaveCredentials(path string) error
- func (c *Client) SetCredentials(creds *credentials.Credentials) error
- type Config
- type Hooks
- type Message
- type Option
- func WithCredentials(creds *credentials.Credentials) Option
- func WithDisableFileStorage() Option
- func WithOnConnected(fn func()) Option
- func WithOnDisconnected(fn func(error)) Option
- func WithOnError(fn func(error)) Option
- func WithOnMessage(fn func(*Message)) Option
- func WithOnRegister(fn func(*SubscriptionInfo)) Option
- func WithPersistentIDs(ids []string) Option
- func WithPersistentIDsLoader(loader PersistentIDsLoader) Option
- func WithPersistentIDsSaver(saver PersistentIDsSaver) Option
- type PersistentIDsLoader
- type PersistentIDsSaver
- type SubscriptionInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Web Push client that can register for push notifications and listen for incoming messages.
func NewClient ¶
NewClient creates a new Web Push client with the given configuration. If credentials are not provided via WithCredentials, attempts to load from file.
func (*Client) ClearPersistentIDs ¶
ClearPersistentIDs removes all stored persistent IDs.
func (*Client) Credentials ¶
func (c *Client) Credentials() *credentials.Credentials
Credentials returns the current credentials, or nil if not registered.
func (*Client) Listen ¶
Listen connects to MCS and listens for push notifications. This method blocks until the context is cancelled. Credentials must be available (either loaded, from Register, or via WithCredentials).
func (*Client) PersistentIDs ¶
PersistentIDs returns the list of unacknowledged message IDs. If a custom loader was provided, it will be used. Otherwise, reads from file.
func (*Client) Register ¶
func (c *Client) Register(ctx context.Context) (*SubscriptionInfo, error)
Register performs device registration with GCM/FCM to obtain push credentials. Returns subscription info that can be sent to a push server.
func (*Client) Run ¶
Run performs registration (if needed) and starts listening for notifications. This is a convenience method that combines Register and Listen.
func (*Client) SaveCredentials ¶
SaveCredentials saves the current credentials to the specified path.
func (*Client) SetCredentials ¶
func (c *Client) SetCredentials(creds *credentials.Credentials) error
SetCredentials sets the credentials directly.
type Config ¶
type Config struct {
// Origin is the web push origin URL (e.g., "https://example.com").
// Required for registration.
Origin string
// VAPIDKey is the VAPID public key from the push service (base64url encoded).
// Required for registration.
VAPIDKey string
// CredentialsFile is the path to store/load credentials.
// Defaults to "credentials.json".
CredentialsFile string
// PersistentIDsFile is the path to store unacknowledged message IDs.
// Defaults to "persistent_ids.json".
PersistentIDsFile string
}
Config holds the configuration for the Web Push client.
type Hooks ¶
type Hooks struct {
// OnRegister is called after successful registration with subscription info
OnRegister func(info *SubscriptionInfo)
// OnMessage is called when a push notification is received and decrypted
OnMessage func(msg *Message)
// OnConnected is called when the MCS connection is established
OnConnected func()
// OnDisconnected is called when the MCS connection is lost
OnDisconnected func(err error)
// OnError is called when an error occurs during operation
OnError func(err error)
}
Hooks contains callback functions for various client events.
type Message ¶
type Message struct {
// PersistentID is the unique message identifier from GCM
PersistentID string
// Payload is the decrypted message content
Payload []byte
// AppData contains metadata from the push message headers
AppData map[string]string
}
Message represents a decrypted push notification message.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithCredentials ¶
func WithCredentials(creds *credentials.Credentials) Option
WithCredentials sets the credentials directly, bypassing file loading.
func WithDisableFileStorage ¶
func WithDisableFileStorage() Option
WithDisableFileStorage disables automatic file-based storage for credentials and persistent IDs. Useful when providing custom storage handlers.
func WithOnConnected ¶
func WithOnConnected(fn func()) Option
WithOnConnected sets the OnConnected hook.
func WithOnDisconnected ¶
WithOnDisconnected sets the OnDisconnected hook.
func WithOnMessage ¶
WithOnMessage sets the OnMessage hook.
func WithOnRegister ¶
func WithOnRegister(fn func(*SubscriptionInfo)) Option
WithOnRegister sets the OnRegister hook.
func WithPersistentIDs ¶
WithPersistentIDs sets the initial persistent IDs directly.
func WithPersistentIDsLoader ¶
func WithPersistentIDsLoader(loader PersistentIDsLoader) Option
WithPersistentIDsLoader sets a custom function to load persistent IDs. Takes precedence over file-based loading.
func WithPersistentIDsSaver ¶
func WithPersistentIDsSaver(saver PersistentIDsSaver) Option
WithPersistentIDsSaver sets a custom function to save persistent IDs. Takes precedence over file-based saving.
type PersistentIDsLoader ¶
PersistentIDsLoader is a function that loads persistent IDs from custom storage.
type PersistentIDsSaver ¶
PersistentIDsSaver is a function that saves persistent IDs to custom storage.
type SubscriptionInfo ¶
type SubscriptionInfo struct {
// Endpoint is the FCM push endpoint URL
Endpoint string `json:"endpoint"`
// P256dh is the client's ECDH public key (base64url encoded)
P256dh string `json:"p256dh"`
// Auth is the authentication secret (base64url encoded)
Auth string `json:"auth"`
}
SubscriptionInfo contains the Web Push subscription details needed to send push notifications to this client.