fcm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 22 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 fcm is a client for Firebase Cloud Messaging that registers as an Android app and receives push messages over Google's MCS protocol (mtalk.google.com:5228), the way an Android device does.

The package knows nothing about Rust+; see package push for that.

creds, err := fcm.Register(ctx, app)
l := fcm.NewListener(creds)
err = l.Run(ctx, func(m fcm.Message) { ... })

Index

Constants

This section is empty.

Variables

View Source
var ErrLoginRejected = errors.New("fcm: MCS login rejected")

ErrLoginRejected is returned by Listener.Run when MCS refuses the device credentials; register again to get new ones.

Functions

func CheckIn

func CheckIn(ctx context.Context, androidID, securityToken uint64, opts ...Option) (id, token uint64, err error)

CheckIn checks a device in with Google. With zero arguments it creates a new device; with existing credentials it refreshes them, which Google expects devices to do before connecting.

Types

type App

type App struct {
	APIKey      string // Firebase API key
	ProjectID   string // Firebase project ID
	SenderID    string // GCM sender ID (project number)
	AppID       string // GMS app ID, "1:<sender>:android:<hash>"
	PackageName string // Android package name
	PackageCert string // SHA-1 of the signing certificate, hex
}

App identifies the Android app to register as. The values come from the app's google-services.json and signing certificate.

type Credentials

type Credentials struct {
	// AndroidID and SecurityToken are the GCM device credentials used to
	// log in to MCS.
	AndroidID     uint64
	SecurityToken uint64
	// Token is the FCM registration token senders address messages to.
	Token string
}

Credentials identify a registered device. Keep them to receive messages later without registering again.

func Register

func Register(ctx context.Context, app App, opts ...Option) (Credentials, error)

Register registers a new device for app and returns its credentials. It creates a Firebase installation, checks in a new Android device and requests an FCM token for it.

func (Credentials) MarshalJSON

func (c Credentials) MarshalJSON() ([]byte, error)

MarshalJSON writes {"gcm":{"androidId","securityToken"},"fcm":{"token"}}.

func (*Credentials) UnmarshalJSON

func (c *Credentials) UnmarshalJSON(b []byte) error

UnmarshalJSON reads the layout written by MarshalJSON.

type HTTPError

type HTTPError struct {
	URL        string
	StatusCode int
	Body       string
}

HTTPError is returned when an endpoint answers with a non-2xx status.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Listener

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

Listener receives messages for a registered device. Create it with NewListener and call Run.

func NewListener

func NewListener(creds Credentials, opts ...Option) *Listener

NewListener returns a listener for the device identified by creds.

func (*Listener) PersistentIDs

func (l *Listener) PersistentIDs() []string

PersistentIDs returns the IDs of messages received but not yet acknowledged to the server. Save them and pass them to WithPersistentIDs to avoid duplicates after a restart.

func (*Listener) Run

func (l *Listener) Run(ctx context.Context, handle func(Message)) error

Run connects to MCS and calls handle for every message until ctx is done, reconnecting with backoff when the connection drops. Messages are delivered at most once per Listener; handle runs on Run's goroutine.

Run returns ctx.Err() when ctx is done, or ErrLoginRejected or an *HTTPError (checkin 400/401/403) when the credentials are no longer valid.

type Message

type Message struct {
	ID           string
	From         string
	Category     string
	PersistentID string
	// Sent is when the sender sent the message, if the server reports it.
	Sent time.Time
	// Data holds the key/value payload ("app data") of the message.
	Data map[string]string
	// RawData is the binary payload, if any.
	RawData []byte
}

Message is a push message received from FCM.

type Option

type Option func(*config)

Option configures Register and NewListener.

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient sets the HTTP client used for registration and checkin.

func WithHeartbeat

func WithHeartbeat(d time.Duration) Option

WithHeartbeat sets how often the listener pings the server to detect a dead connection. The default is 4 minutes.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger. By default nothing is logged.

func WithMaxBackoff

func WithMaxBackoff(d time.Duration) Option

WithMaxBackoff caps the delay between reconnect attempts (default 5m).

func WithPersistentIDs

func WithPersistentIDs(ids []string) Option

WithPersistentIDs passes the persistent IDs of messages already handled in a previous run, so the server does not deliver them again.

Directories

Path Synopsis
Package fcmpb contains the Go types generated from the Google checkin and MCS protocol definitions (proto/fcm).
Package fcmpb contains the Go types generated from the Google checkin and MCS protocol definitions (proto/fcm).

Jump to

Keyboard shortcuts

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