fcm

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

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

Go to latest
Published: Aug 21, 2017 License: MIT Imports: 11 Imported by: 0

README

Firebase Cloud Messaging (FCM) Library for Go (golang)

This library uses HTTP/JSON Firebase Cloud Messaging connection server protocol. It's based heavily on github.com/NaySoftware/go-fcm but cleans up the API to pass golint tests, and be App Engine compatible.

Documentation

Index

Constants

View Source
const (
	// MaxTTL the default ttl for a notification
	MaxTTL = 2419200
	// PriorityHigh notification priority
	PriorityHigh = "high"
	// PriorityNormal notification priority
	PriorityNormal = "normal"
)

Variables

View Source
var (
	// HTTPClient can be used to use a custom HTTP client for requests to the API.
	// If nil, then the default http.Client will be used
	HTTPClient *http.Client
	// Timeout is the default timeout for HTTP clients
	Timeout = 10 * time.Second
)

Functions

This section is empty.

Types

type APNResult

type APNResult struct {
	APNSToken         string `json:"apns_token"`
	Status            string `json:"status"`
	RegistrationToken string `json:"registration_token"`
}

APNResult is a sub-struct of the API response for APNTokenImportRequest

type APNTokenImportRequest

type APNTokenImportRequest struct {
	Application string   `json:"application"`
	Sandbox     bool     `json:"sandbox"`
	APNSTokens  []string `json:"apns_tokens"`
}

APNTokenImportRequest is an APN token import request data structure

type APNTokenImportResult

type APNTokenImportResult struct {
	Results []APNResult `json:"results"`
}

APNTokenImportResult is the API response for APNTokenImportRequest

type ApnsBatchRequest

type ApnsBatchRequest struct {
	App        string   `json:"application,omitempty"`
	Sandbox    bool     `json:"sandbox,omitempty"`
	ApnsTokens []string `json:"apns_tokens,omitempty"`
}

ApnsBatchRequest apns import request

type ApnsBatchResponse

type ApnsBatchResponse struct {
	Results    []map[string]string `json:"results,omitempty"`
	Error      string              `json:"error,omitempty"`
	Status     string
	StatusCode int
}

ApnsBatchResponse apns import response

type BatchRequest

type BatchRequest struct {
	To        string   `json:"to,omitempty"`
	RegTokens []string `json:"registration_tokens,omitempty"`
}

BatchRequest add/remove request

type BatchResponse

type BatchResponse struct {
	Error      string              `json:"error,omitempty"`
	Results    []map[string]string `json:"results,omitempty"`
	Status     string
	StatusCode int
}

BatchResponse add/remove response

type Client

type Client struct {
	APIKey string
}

Client stores the key and the Message (Msg)

func NewClient

func NewClient(apiKey string) *Client

NewClient init and create fcm client

func (*Client) ApnsBatchImportRequest

func (c *Client) ApnsBatchImportRequest(ctx context.Context, apnsReq *ApnsBatchRequest) (*ApnsBatchResponse, error)

ApnsBatchImportRequest apns import requst

func (*Client) BatchSubscribeToTopic

func (c *Client) BatchSubscribeToTopic(ctx context.Context, tokens []string, topic string) (*BatchResponse, error)

BatchSubscribeToTopic subscribes (many) devices/tokens to a given topic

func (*Client) BatchUnsubscribeFromTopic

func (c *Client) BatchUnsubscribeFromTopic(ctx context.Context, tokens []string, topic string) (*BatchResponse, error)

BatchUnsubscribeFromTopic unsubscribes (many) devices/tokens from a given topic

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, out interface{}) (*http.Response, error)

Do sends an HTTP request and decodes the results into `out`

func (*Client) Get

func (c *Client) Get(ctx context.Context, urlStr string, out interface{}) (*http.Response, error)

Get sends a HTTP GET request to the urlStr and decodes the response into `out`

func (*Client) GetInfo

func (c *Client) GetInfo(ctx context.Context, withDetails bool, instanceIDToken string) (*InstanceIDInfoResponse, error)

GetInfo gets the instance id info

func (*Client) ImportAPNToken

func (c *Client) ImportAPNToken(ctx context.Context, req *APNTokenImportRequest) (*APNTokenImportResult, error)

ImportAPNToken sends an APNTokenImportRequest to the API

func (*Client) NotificationGroup

func (c *Client) NotificationGroup(ctx context.Context, req *NotificationGroupRequest) (*NotificationGroupResponse, error)

NotificationGroup sends a notification group request to the API

func (*Client) Post

func (c *Client) Post(ctx context.Context, urlStr string, data interface{}, out interface{}) (*http.Response, error)

Post sends a POST request with JSON payload to the urlStr, and decodes the response into out

func (*Client) Send

func (c *Client) Send(ctx context.Context, m *Message) error

Send sends a message

func (*Client) SendTopic

func (c *Client) SendTopic(ctx context.Context, m *Message) (*TopicResponse, error)

SendTopic sends the message to the subscribers of the topic

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, topic string, tokens ...string) error

Subscribe adds subscriber(s) to the topic

func (*Client) SubscribeToTopic

func (c *Client) SubscribeToTopic(ctx context.Context, instanceIDToken, topic string) (*SubscribeResponse, error)

SubscribeToTopic subscribes a single device/token to a topic

type Error

type Error struct {
	Error string `json:"error"`
}

type InstanceIDInfoResponse

type InstanceIDInfoResponse struct {
	Application        string                                  `json:"application,omitempty"`
	AuthorizedEntity   string                                  `json:"authorizedEntity,omitempty"`
	ApplicationVersion string                                  `json:"applicationVersion,omitempty"`
	AppSigner          string                                  `json:"appSigner,omitempty"`
	AttestStatus       string                                  `json:"attestStatus,omitempty"`
	Platform           string                                  `json:"platform,omitempty"`
	ConnectionType     string                                  `json:"connectionType,omitempty"`
	ConnectDate        string                                  `json:"connectDate,omitempty"`
	Error              string                                  `json:"error,omitempty"`
	Rel                map[string]map[string]map[string]string `json:"rel,omitempty"`
}

InstanceIDInfoResponse response for instance id info request

type Message

type Message struct {
	Notification Notification `json:"notification"`
	Priority     string       `json:"priority,omitempty"`
	To           string       `json:"to"`
}

Message represents fcm request message

type Notification

type Notification struct {
	Title       string `json:"title"`
	Body        string `json:"body"`
	Icon        string `json:"icon"`
	ClickAction string `json:"click_action"`
}

type NotificationGroupRequest

type NotificationGroupRequest struct {
	Operation           string   `json:"operation"`
	NotificationKeyName string   `json:"notification_key_name"`
	RegistrationIDs     []string `json:"registration_ids"`
}

NotificationGroupRequest is a notification group request payload. See https://firebase.google.com/docs/cloud-messaging/js/device-group

type NotificationGroupResponse

type NotificationGroupResponse struct {
	NotificationKey string `json:"notification_key"`
}

NotificationGroupResponse is a notification group response. See https://firebase.google.com/docs/cloud-messaging/js/device-group

type NotificationPayload

type NotificationPayload struct {
	Title        string `json:"title,omitempty"`
	Body         string `json:"body,omitempty"`
	Icon         string `json:"icon,omitempty"`
	Sound        string `json:"sound,omitempty"`
	Badge        string `json:"badge,omitempty"`
	Tag          string `json:"tag,omitempty"`
	Color        string `json:"color,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"`
}

NotificationPayload notification message payload

type ResponseStatus

type ResponseStatus struct {
	Ok           bool
	StatusCode   int
	MulticastID  int64               `json:"multicast_id"`
	Success      int                 `json:"success"`
	Fail         int                 `json:"failure"`
	CanonicalIDs int                 `json:"canonical_ids"`
	Results      []map[string]string `json:"results,omitempty"`
	MsgID        int64               `json:"message_id,omitempty"`
	Err          string              `json:"error,omitempty"`
	RetryAfter   string
}

ResponseStatus represents fcm response message - (tokens and topics)

func (*ResponseStatus) GetRetryAfterTime

func (r *ResponseStatus) GetRetryAfterTime() (t time.Duration, e error)

GetRetryAfterTime converts the retrey after response header to a time.Duration

func (*ResponseStatus) IsTimeout

func (r *ResponseStatus) IsTimeout() bool

IsTimeout check whether the response timeout based on http response status code and if any error is retryable

type SubscribeResponse

type SubscribeResponse struct {
	Error      string `json:"error,omitempty"`
	Status     string
	StatusCode int
}

SubscribeResponse response for single topic subscribtion

type TopicResponse

type TopicResponse struct {
	MessageID    int64                 `json:"message_id"`
	MulticastID  int64                 `json:"multicast_id,omitempty"`
	Success      int                   `json:"success,omitempty"`
	Failure      int                   `json:"failure,omitempty"`
	CanonicalIDs int                   `json:"canonical_ids,omitempty"`
	Results      []TopicResponseResult `json:"results,omitempty"`
}

TopicResponse is the API response when sending a message to a topic

func (TopicResponse) Error

func (t TopicResponse) Error() error

Err returns the error message, if any

type TopicResponseResult

type TopicResponseResult struct {
	Error string `json:"error"`
}

Jump to

Keyboard shortcuts

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