fcm

package module
Version: v0.0.0-...-f3796d8 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2018 License: Apache-2.0 Imports: 8 Imported by: 1

README

Golang FCM

Basic implementation of FCM (firebase cloud messaging) in Go. Only HTTP requests with JSON payload are supported. This package uses legacy HTTP API (pre-v1 API).

Documentation

https://godoc.org/github.com/tinode/fcm

Usage:

  client := fcm.NewClient(your_fcm_api_key)

  message := &fcm.HttpMessage{...initialize your message...}
  response := client.SendHttp(message)

The client is safe to use from multiple go routines at the same time. The client maintains a pool of HTTP connections. It recycles them as needed. Do not recreate client for every request because it's wasteful. SendHttp is a blocking call.

Sample code: https://github.com/tinode/chat/blob/master/server/push/fcm/push_fcm.go

Installation

go get github.com/tinode/fcm

Documentation

Index

Constants

View Source
const (
	PriorityHigh   = "high"
	PriorityNormal = "normal"
)
View Source
const (
	ErrorMissingRegistration       = "MissingRegistration"
	ErrorInvalidRegistration       = "InvalidRegistration"
	ErrorNotRegistered             = "NotRegistered"
	ErrorInvalidPackageName        = "InvalidPackageName"
	ErrorMismatchSenderId          = "MismatchSenderId"
	ErrorMessageTooBig             = "MessageTooBig"
	ErrorInvalidDataKey            = "InvalidDataKey"
	ErrorInvalidTtl                = "InvalidTtl"
	ErrorUnavailable               = "Unavailable"
	ErrorInternalServerError       = "InternalServerError"
	ErrorDeviceMessageRateExceeded = "DeviceMessageRateExceeded"
	ErrorTopicsMessageRateExceeded = "TopicsMessageRateExceeded"
)

Error messages

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(apikey string) *Client

NewClient returns an FCM client. The client is expected to be long-lived. It maintains an internal pool of HTTP connections. Multiple sumultaneous Send requests can be issued on the same client.

func (*Client) GetRetryAfter

func (c *Client) GetRetryAfter() uint

GetRetryAfter returns the number fo seconds to wait before retrying Send in case the previous Send has failed.

func (*Client) PostHttp

func (c *Client) PostHttp(msg *HttpMessage) (<-chan *HttpResponse, error)

PostHttp is a non-blocking version of Send. Not implemented yet.

func (*Client) SendHttp

func (c *Client) SendHttp(msg *HttpMessage) (*HttpResponse, error)

SendHttp is a blocking call to send an HTTP message to FCM server. Multiple Send requests can be issued simultaneously on the same Client.

type HttpMessage

type HttpMessage struct {
	To                    string        `json:"to,omitempty"`
	RegistrationIds       []string      `json:"registration_ids,omitempty"`
	Condition             string        `json:"condition,omitempty"`
	CollapseKey           string        `json:"collapse_key,omitempty"`
	Priority              string        `json:"priority,omitempty"`
	ContentAvailable      bool          `json:"content_available,omitempty"`
	TimeToLive            *uint         `json:"time_to_live,omitempty"`
	RestrictedPackageName string        `json:"restricted_package_name,omitempty"`
	DryRun                bool          `json:"dry_run,omitempty"`
	Data                  interface{}   `json:"data,omitempty"`
	Notification          *Notification `json:"notification,omitempty"`
}

HttpMessage is an FCM HTTP request message

type HttpResponse

type HttpResponse struct {
	MulticastId  int      `json:"multicast_id"`
	Success      int      `json:"success"`
	Fail         int      `json:"failure"`
	CanonicalIds int      `json:"canonical_ids"`
	Results      []Result `json:"results,omitempty"`
}

HttpResponse is an FCM response message

type Notification

type Notification struct {
	Title        string `json:"title,omitempty"`
	Body         string `json:"body,omitempty"`
	Sound        string `json:"sound,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"`

	// Android only
	Icon  string `json:"icon,omitempty"`
	Tag   string `json:"tag,omitempty"`
	Color string `json:"color,omitempty"`

	// iOS only
	Badge string `json:"badge,omitempty"`
}

Notification notification message structure

type Result

type Result struct {
	MessageId      string `json:"message_id"`
	RegistrationId string `json:"registration_id"`
	Error          string `json:"error"`
}

Source Files

Jump to

Keyboard shortcuts

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