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 ¶
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 ¶
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 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 ¶
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 ¶
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 ¶
WithHTTPClient sets the HTTP client used for registration and checkin.
func WithHeartbeat ¶
WithHeartbeat sets how often the listener pings the server to detect a dead connection. The default is 4 minutes.
func WithLogger ¶
WithLogger sets the logger. By default nothing is logged.
func WithMaxBackoff ¶
WithMaxBackoff caps the delay between reconnect attempts (default 5m).
func WithPersistentIDs ¶
WithPersistentIDs passes the persistent IDs of messages already handled in a previous run, so the server does not deliver them again.