Documentation
¶
Overview ¶
Package fcm provides Firebase Cloud Messaging functionality for Golang
Here is a simple example illustrating how to use FCM library:
func main() {
ctx := context.Background()
client, err := fcm.NewClient(
ctx,
fcm.WithCredentialsFile("path/to/serviceAccountKey.json"),
)
if err != nil {
log.Fatal(err)
}
// Send to a single device
token := "test"
resp, err := client.Send(
ctx,
&messaging.Message{
Token: token,
Data: map[string]string{
"foo": "bar",
},
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println("success count:", resp.SuccessCount)
fmt.Println("failure count:", resp.FailureCount)
fmt.Println("message id:", resp.Responses[0].MessageID)
fmt.Println("error msg:", resp.Responses[0].Error)
}
Index ¶
- type Client
- func (c *Client) Send(ctx context.Context, message ...*messaging.Message) (*messaging.BatchResponse, error)
- func (c *Client) SendDryRun(ctx context.Context, message ...*messaging.Message) (*messaging.BatchResponse, error)
- func (c *Client) SendMulticast(ctx context.Context, message *messaging.MulticastMessage) (*messaging.BatchResponse, error)
- func (c *Client) SendMulticastDryRun(ctx context.Context, message *messaging.MulticastMessage) (*messaging.BatchResponse, error)
- func (c *Client) SubscribeTopic(ctx context.Context, tokens []string, topic string) (*messaging.TopicManagementResponse, error)
- func (c *Client) UnsubscribeTopic(ctx context.Context, tokens []string, topic string) (*messaging.TopicManagementResponse, error)
- type Option
- func WithCredentialsFile(filename string) Option
- func WithCredentialsJSON(data []byte) Option
- func WithCustomClientOption(opts ...option.ClientOption) Option
- func WithDebug(debug bool) Option
- func WithEndpoint(endpoint string) Option
- func WithHTTPClient(httpClient *http.Client) Option
- func WithHTTPProxy(proxyURL string) Option
- func WithProjectID(projectID string) Option
- func WithServiceAccount(serviceAccount string) Option
- func WithTokenSource(s oauth2.TokenSource) Option
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 abstracts the interaction between the application server and the FCM server via the Firebase Cloud Messaging HTTP v1 API. Authenticate it with service-account credentials (WithCredentialsFile / WithCredentialsJSON), an OAuth2 token source (WithTokenSource), or Google Application Default Credentials so that it can perform authorized requests on the application server's behalf. To send a message to one or more devices use the Client's Send method.
By default requests use a standard http.Client; supply your own with WithHTTPClient or route through a proxy with WithHTTPProxy.
Authorization Scopes Requires one of the following OAuth scopes: - https://www.googleapis.com/auth/firebase.messaging
func NewClient ¶
NewClient creates a new Firebase Cloud Messaging Client, applying the given options and using the default endpoint and http client unless overridden.
func (*Client) Send ¶
func (c *Client) Send( ctx context.Context, message ...*messaging.Message, ) (*messaging.BatchResponse, error)
Send delivers one or more messages to the FCM server, sending each message in its own request via SendEach. The returned BatchResponse reports the outcome of every message in resp.Responses together with SuccessCount/FailureCount; a non-nil error is returned only when the batch as a whole cannot be sent, not when individual messages fail, so callers must inspect the response to detect per-message errors.
func (*Client) SendDryRun ¶ added in v1.0.0
func (c *Client) SendDryRun( ctx context.Context, message ...*messaging.Message, ) (*messaging.BatchResponse, error)
SendDryRun sends the messages in the given array via Firebase Cloud Messaging in the dry run (validation only) mode.
func (*Client) SendMulticast ¶ added in v1.0.0
func (c *Client) SendMulticast( ctx context.Context, message *messaging.MulticastMessage, ) (*messaging.BatchResponse, error)
SendMulticast sends the given multicast message to all the FCM registration tokens specified.
func (*Client) SendMulticastDryRun ¶ added in v1.0.0
func (c *Client) SendMulticastDryRun( ctx context.Context, message *messaging.MulticastMessage, ) (*messaging.BatchResponse, error)
SendMulticastDryRun sends the given multicast message to all the specified FCM registration tokens in the dry run (validation only) mode.
func (*Client) SubscribeTopic ¶ added in v1.0.0
func (c *Client) SubscribeTopic( ctx context.Context, tokens []string, topic string, ) (*messaging.TopicManagementResponse, error)
SubscribeTopic subscribes a list of registration tokens to a topic.
The tokens list must not be empty, and have at most 1000 tokens.
func (*Client) UnsubscribeTopic ¶ added in v1.0.0
func (c *Client) UnsubscribeTopic( ctx context.Context, tokens []string, topic string, ) (*messaging.TopicManagementResponse, error)
UnsubscribeTopic unsubscribes a list of registration tokens from a topic.
The tokens list must not be empty, and have at most 1000 tokens.
type Option ¶
Option configurates Client with defined option.
func WithCredentialsFile ¶ added in v1.0.0
WithCredentialsFile returns a ClientOption that authenticates API calls with the given service account or refresh token JSON credentials file.
func WithCredentialsJSON ¶ added in v1.0.0
WithCredentialsJSON returns a ClientOption that authenticates API calls with the given service account or refresh token JSON credentials.
func WithCustomClientOption ¶ added in v1.2.0
func WithCustomClientOption(opts ...option.ClientOption) Option
WithCustomClientOption is an option function that allows you to provide custom client options. It appends the provided custom options to the client's options list. The custom options are applied when sending requests to the FCM server. If no custom options are provided, this function does nothing.
Parameters:
- opts: The custom client options to be appended to the client's options list.
Returns:
- An error if there was an issue appending the custom options to the client's options list, or nil otherwise.
func WithEndpoint ¶
WithEndpoint returns Option to configure endpoint.
func WithHTTPClient ¶
WithHTTPClient returns Option to configure HTTP Client.
func WithHTTPProxy ¶ added in v0.1.6
WithHTTPProxy returns Option to configure HTTP Client proxy.
func WithProjectID ¶ added in v1.0.0
WithProjectID returns Option to configure project ID.
func WithServiceAccount ¶ added in v1.0.0
WithServiceAccount returns Option to configure service account.
func WithTokenSource ¶ added in v1.0.0
func WithTokenSource(s oauth2.TokenSource) Option
WithTokenSource returns a ClientOption that specifies an OAuth2 token source to be used as the basis for authentication.