fcm

package
v0.0.50 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 6 Imported by: 0

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

View Source
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 New

func New(ctx context.Context, cfg Config) (*Client, error)

New constructs a Client.

func (*Client) Send

func (c *Client) Send(ctx context.Context, token string, notif Notification) (string, error)

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

func (c *Client) SendToTopic(ctx context.Context, topic string, notif Notification) (string, error)

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

type TokenError struct {
	Token string
	Err   error
}

TokenError pairs a failed token with its error.

Jump to

Keyboard shortcuts

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