Documentation
¶
Overview ¶
Package fcm wraps Firebase Cloud Messaging for sending push notifications to single devices, multiple tokens, or topics.
Initialization accepts a service-account JSON either inline or via a file path. With neither set, Application Default Credentials are used.
Example:
c, err := fcm.New(ctx, fcm.Config{
ProjectID: "my-project",
CredentialJSON: []byte(os.Getenv("FCM_SERVICE_ACCOUNT")),
})
_, err = c.Send(ctx, deviceToken, fcm.Notification{
Title: "Pesanan Diterima",
Body: "Pesanan #1234 telah diterima.",
Module: "TRANSACTION",
Type: "ORDER_RECEIVED",
Route: "/orders/1234",
})
Index ¶
- Constants
- type Client
- func (c *Client) Send(ctx context.Context, token string, notif Notification) (string, error)
- func (c *Client) SendMulticast(ctx context.Context, tokens []string, notif Notification) (*MulticastResult, error)
- func (c *Client) SendToTopic(ctx context.Context, topic string, notif Notification) (string, error)
- type Config
- type MulticastResult
- type Notification
- type TokenError
Constants ¶
const MaxTokensPerBatch = 500
MaxTokensPerBatch is the FCM-imposed cap on tokens per multicast call. SendMulticast automatically chunks larger lists.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a *messaging.Client.
func (*Client) Send ¶
Send delivers a notification to a single device token. Returns the FCM message ID on success.
func (*Client) SendMulticast ¶
func (c *Client) SendMulticast( ctx context.Context, tokens []string, notif Notification, ) (*MulticastResult, error)
SendMulticast delivers a notification to up to MaxTokensPerBatch tokens per underlying call, chunking larger token lists transparently.
func (*Client) SendToTopic ¶
SendToTopic delivers a notification to all subscribers of a topic.
type Config ¶
type Config struct {
ProjectID string
CredentialJSON []byte // raw service account JSON content
CredentialFile string // path to service account JSON file
}
Config controls FCM client construction.
type MulticastResult ¶
type MulticastResult struct {
SuccessCount int
FailureCount int
Errors []TokenError // one entry per failed token
}
MulticastResult aggregates results across one or more underlying batch calls (SendEachForMulticast caps at MaxTokensPerBatch tokens per call).
type Notification ¶
type Notification struct {
Title string
Body string
Module string
Type string
Route string
Data map[string]string
}
Notification carries the fields used to build an FCM message.
Title and Body are surfaced as the user-visible notification. Module / Type / Route are merged into the data payload (under those keys) so client apps can route deep-links. Data is merged last and may override.
type TokenError ¶
TokenError pairs a failed token with its error.