fcm

package module
v0.0.0-...-fa5603d Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2020 License: MIT Imports: 16 Imported by: 0

README

go-fcm

CI

Go client library for sending push notifications to android devices through Google FCM service. Docs

Documentation

Index

Constants

View Source
const (
	NotificationPriorityUnspecified NotificationPriority = "PRIORITY_UNSPECIFIED"
	NotificationPriorityMin         NotificationPriority = "PRIORITY_MIN"
	NotificationPriorityLow         NotificationPriority = "PRIORITY_LOW"
	NotificationPriorityDefault     NotificationPriority = "PRIORITY_DEFAULT"
	NotificationPriorityHigh        NotificationPriority = "PRIORITY_HIGH"
	NotificationPriorityMax         NotificationPriority = "PRIORITY_MAX"

	VisibilityPrivate Visibility = "PRIVATE"
	VisibilityPublic  Visibility = "PUBLIC"
	VisibilitySecret  Visibility = "SECRET"

	AndroidMessagePriorityNormal AndroidMessagePriority = "NORMAL"
	AndroidMessagePriorityHigh   AndroidMessagePriority = "HIGH"
)
View Source
const (
	// DefaultEndpoint contains endpoint URL of FCM service.
	// this constant value are used as audience value for the auth token
	// be careful in in case of changes
	DefaultEndpoint = "https://fcm.googleapis.com/"
)

Variables

View Source
var (
	// ErrInvalidMessage occurs if push notitication message is nil.
	ErrInvalidMessage = errors.New("message is invalid")

	// ErrInvalidTarget occurs if message topic is empty.
	ErrInvalidTarget = errors.New("topic is invalid or registration ids are not set")
)
View Source
var DefaultHTTPAdapter = NewFastHTTPAdapter(&fasthttp.Client{})

Functions

This section is empty.

Types

type AndroidConfig

type AndroidConfig struct {
	CollapseKey           string                 `json:"collapse_key,omitempty"`
	Priority              AndroidMessagePriority `json:"priority,omitempty"`
	Ttl                   string                 `json:"ttl,omitempty"`
	RestrictedPackageName string                 `json:"restricted_package_name,omitempty"`
	Data                  map[string]string      `json:"data,omitempty"`
	Notification          *AndroidNotification   `json:"notification,omitempty"`
	FCMOptions            *AndroidFCMOptions     `json:"fcm_options,omitempty"`
	DirectBootOk          bool                   `json:"direct_boot_ok,omitempty"`
}

See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidConfig

type AndroidFCMOptions

type AndroidFCMOptions struct {
	AnalyticsLabel string `json:"analytics_label,omitempty"`
}

AndroidFCMOptions contains additional options for features provided by the FCM Android SDK.

type AndroidMessagePriority

type AndroidMessagePriority string

type AndroidNotification

type AndroidNotification struct {
	Title                 string               `json:"title,omitempty"`
	Body                  string               `json:"body,omitempty"`
	Icon                  string               `json:"icon,omitempty"`
	Color                 string               `json:"color,omitempty"`
	Sound                 string               `json:"sound,omitempty"`
	Tag                   string               `json:"tag,omitempty"`
	ClickAction           string               `json:"click_action,omitempty"`
	BodyLocKey            string               `json:"body_loc_key,omitempty"`
	BodyLocArgs           []string             `json:"body_loc_args,omitempty"`
	TitleLocKey           string               `json:"title_loc_key,omitempty"`
	TitleLocArgs          []string             `json:"title_loc_args,omitempty"`
	ChannelId             string               `json:"channel_id,omitempty"`
	Ticker                string               `json:"ticker,omitempty"`
	Sticky                bool                 `json:"sticky,omitempty"`
	EventName             string               `json:"event_name,omitempty"`
	LocalOnly             bool                 `json:"local_only,omitempty"`
	NotificationPriority  NotificationPriority `json:"notification_priority,omitempty"`
	DefaultSound          bool                 `json:"default_sound,omitempty"`
	DefaultVibrateTimings bool                 `json:"default_vibrate_timings,omitempty"`
	DefaultLightSettings  bool                 `json:"default_light_settings,omitempty"`
	VibrateTimings        []string             `json:"vibrate_timings,omitempty"`
	Visibility            Visibility           `json:"visibility,omitempty"`
	NotificationCount     int                  `json:"notification_count,omitempty"`
	LightSettings         *LightSettings       `json:"light_settings,omitempty"`
	Image                 string               `json:"image,omitempty"`
}

Notification specifies the predefined, user-visible key-value pairs of the notification payload. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidNotification

type Client

type Client interface {
	// Send sends a message to the FCM server without retrying in case of service
	// unavailability. A non-nil error is returned if a non-recoverable error
	// occurs (i.e. if the sendResponse status code is not between 200 and 299).
	Send(ctx context.Context, msg *Message) error
}

SimpleClient abstracts the interaction between the application server and the FCM server via HTTP protocol. It uses Service Account Private Key for authentication.

If the `HTTP` field is nil, a zeroed http.SimpleClient will be allocated and used to send messages.

type ClientMock

type ClientMock struct {
	SendMock mClientMockSend
	// contains filtered or unexported fields
}

ClientMock implements Client

func NewClientMock

func NewClientMock(t minimock.Tester) *ClientMock

NewClientMock returns a mock for Client

func (*ClientMock) MinimockFinish

func (m *ClientMock) MinimockFinish()

MinimockFinish checks that all mocked methods have been called the expected number of times

func (*ClientMock) MinimockSendDone

func (m *ClientMock) MinimockSendDone() bool

MinimockSendDone returns true if the count of the Send invocations corresponds the number of defined expectations

func (*ClientMock) MinimockSendInspect

func (m *ClientMock) MinimockSendInspect()

MinimockSendInspect logs each unmet expectation

func (*ClientMock) MinimockWait

func (m *ClientMock) MinimockWait(timeout mm_time.Duration)

MinimockWait waits for all mocked methods to be called the expected number of times

func (*ClientMock) Send

func (mmSend *ClientMock) Send(ctx context.Context, msg *Message) (err error)

Send implements Client

func (*ClientMock) SendAfterCounter

func (mmSend *ClientMock) SendAfterCounter() uint64

SendAfterCounter returns a count of finished ClientMock.Send invocations

func (*ClientMock) SendBeforeCounter

func (mmSend *ClientMock) SendBeforeCounter() uint64

SendBeforeCounter returns a count of ClientMock.Send invocations

type ClientMockSendExpectation

type ClientMockSendExpectation struct {
	Counter uint64
	// contains filtered or unexported fields
}

ClientMockSendExpectation specifies expectation struct of the Client.Send

func (*ClientMockSendExpectation) Then

Then sets up Client.Send return parameters for the expectation previously defined by the When method

type ClientMockSendParams

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

ClientMockSendParams contains parameters of the Client.Send

type ClientMockSendResults

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

ClientMockSendResults contains results of the Client.Send

type Color

type Color struct {
	Red   float32 `json:"red,omitempty"`
	Green float32 `json:"green,omitempty"`
	Blue  float32 `json:"blue,omitempty"`
	Alpha float32 `json:"alpha,omitempty"`
}

type Error

type Error string
const ErrUnregistered Error = "Unregistered"

This error can be caused by missing registration tokens, unregistered or expired tokens.

func (Error) Error

func (e Error) Error() string

type FastHTTPAdapter

type FastHTTPAdapter struct {
	Client *fasthttp.Client
}

func NewFastHTTPAdapter

func NewFastHTTPAdapter(c *fasthttp.Client) *FastHTTPAdapter

func (*FastHTTPAdapter) Do

type FastHTTPDoer

type FastHTTPDoer interface {
	Do(ctx context.Context, req *fasthttp.Request, resp *fasthttp.Response) error
}

FastHTTPDoer defines methods used to perform http requests using fasthttp library.

type LightSettings

type LightSettings struct {
	Color            Color  `json:"color"`
	LightOnDuration  string `json:"light_on_duration,omitempty"`
	LightOffDuration string `json:"light_off_duration,omitempty"`
}

type Message

type Message struct {
	Name         string            `json:"name,omitempty"`
	Data         map[string]string `json:"data,omitempty"`
	Notification *Notification     `json:"notification,omitempty"`
	Android      *AndroidConfig    `json:"android,omitempty"`

	// one of
	Token     string `json:"token,omitempty"`
	Topic     string `json:"topic,omitempty"`
	Condition string `json:"condition,omitempty"`
}

Message represents list of targets, options, and payload for HTTP JSON messages. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message easyjson:json

func (Message) MarshalEasyJSON

func (v Message) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Message) MarshalJSON

func (v Message) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Message) UnmarshalEasyJSON

func (v *Message) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Message) UnmarshalJSON

func (v *Message) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

func (*Message) Validate

func (msg *Message) Validate() error

Validate returns an error if the message is not well-formed.

type NoopTokenSource

type NoopTokenSource struct{}

func (*NoopTokenSource) Token

func (n *NoopTokenSource) Token() (*oauth2.Token, error)

type Notification

type Notification struct {
	Title string `json:"title,omitempty"`
	Body  string `json:"body,omitempty"`
	Image string `json:"image,omitempty"`
}

See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification

type NotificationPriority

type NotificationPriority string

type Option

type Option func(*SimpleClient) error

Option configurates SimpleClient with defined option.

func WithCredentialsData

func WithCredentialsData(bb []byte) Option

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint returns Option to configure FCM Endpoint.

func WithHTTPClient

func WithHTTPClient(httpClient FastHTTPDoer) Option

WithHTTPClient returns Option to configure HTTP Client.

type SimpleClient

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

func NewClient

func NewClient(serviceAccountJSONData []byte, opts ...Option) *SimpleClient

NewClient creates new Firebase Cloud Messaging SimpleClient based on API key and with default endpoint and http client.

func (*SimpleClient) Send

func (c *SimpleClient) Send(ctx context.Context, msg *Message) error

Send implementation of Client interface. Docs for the reference: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send

type Visibility

type Visibility string

Jump to

Keyboard shortcuts

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