gcm

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

GCM Library for Go GoDoc Build Status Coverage Status Go Report Card

Provides the following functionality for Google Cloud Messaging:

  1. Sending messages via HTTP or XMPP.
  2. Receiving messages from XMPP.

Documentation: see godoc

Installation

$ go get github.com/kolesa-team/go-gcm

Status

This is a rework of go-gcm library. It has the following improvements:

  • code refactored, HTTP and XMPP clients separated
  • monitors XMPP connection with pings, redials when ping fails
  • handles CONNECTION_DRAINING properly
  • graceful close
  • exponential backoff when sending a message
  • circuit breaker for each provider
  • improved logging with logrus
  • ginkgo tests
  • various govet/golint fixes
  • automatic builds with Travis and coverage with Codecov

This library is in Beta. We will make an effort to support the library, but we reserve the right to make incompatible changes when necessary.

Limitations

Note that GCM limitations are not enforced by the library. Instead, all valid requests are sent to the server and a corresponding error is returned (Message Too Big, Device Message Rate Exceeded, etc).

Documentation

Overview

Package gcm provides send and receive GCM functionality.

Package gcm provides send and receive GCM functionality.

Package gcm provides send and receive GCM functionality.

Index

Constants

View Source
const (
	// DefaultMinBackoff is a default min delay for backoff.
	DefaultMinBackoff = 1 * time.Second
	// DefaultMaxBackoff is the default max delay for backoff.
	DefaultMaxBackoff = 10 * time.Second
)
View Source
const (
	// DefaultPingInterval is a default interval between pings.
	DefaultPingInterval = 20 * time.Second
	// DefaultPingTimeout is a default time to wait for ping replies.
	DefaultPingTimeout = 30 * time.Second

	// CCSAck is a positive acknowledge.
	CCSAck = "ack"
	// CCSNack is a negative acknowledge.
	CCSNack = "nack"
	// CCSControl is a CCS upstream control message.
	CCSControl = "control"
	// CCSReceipt is an upstream receipt message.
	CCSReceipt = "receipt"

	// CCSDraining is an upstream CSS message it wants to drain the current connection.
	CCSDraining = "CONNECTION_DRAINING"

	// XMPPIQResult is a result for IQ query.
	XMPPIQResult = "result"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CCSMessage added in v1.5.0

type CCSMessage struct {
	From             string `json:"from, omitempty"`
	MessageID        string `json:"message_id, omitempty"`
	MessageType      string `json:"message_type, omitempty"`
	RegistrationID   string `json:"registration_id,omitempty"`
	Error            string `json:"error,omitempty"`
	ErrorDescription string `json:"error_description,omitempty"`
	Category         string `json:"category, omitempty"`
	Data             Data   `json:"data,omitempty"`
	ControlType      string `json:"control_type,omitempty"`
}

CCSMessage is an XMPP message sent from CCS. The CCS message can be an upstream message (device to server) or a message from CCS (e.g. a delivery receipt, a control, etc).

type Client added in v1.5.0

type Client interface {
	ID() string
	SendHTTP(m HTTPMessage) (*HTTPResponse, error)
	SendXMPP(m XMPPMessage) (string, int, error)
	Close() error
}

Client defines an interface for the GCM client.

func NewClient added in v1.5.0

func NewClient(config *Config, h MessageHandler) (Client, error)

NewClient creates a new GCM client for this senderID.

type Config added in v1.5.0

type Config struct {
	SenderID          string `json:"sender_id"`
	APIKey            string `json:"api_key"`
	Sandbox           bool   `json:"sandbox"`
	MonitorConnection bool   `json:"monitor_connection"`
	Debug             bool   `json:"debug"`
	PingInterval      int    `json:"ping_interval"`
	PingTimeout       int    `json:"ping_timeout"`
}

Config is a container for GCM client configuration data.

type Data

type Data map[string]interface{}

Data defines the custom payload of a GCM message.

type HTTPMessage added in v1.5.0

type HTTPMessage struct {
	To                    string        `json:"to,omitempty"`
	RegistrationIDs       []string      `json:"registration_ids,omitempty"`
	CollapseKey           string        `json:"collapse_key,omitempty"`
	Priority              string        `json:"priority,omitempty"`
	ContentAvailable      bool          `json:"content_available,omitempty"`
	MutableContent        bool          `json:"mutable_content,omitempty"`
	TimeToLive            *uint         `json:"time_to_live,omitempty"`
	RestrictedPackageName string        `json:"restricted_package_name,omitempty"`
	DryRun                bool          `json:"dry_run,omitempty"`
	Data                  Data          `json:"data,omitempty"`
	Notification          *Notification `json:"notification,omitempty"`
}

HTTPMessage defines a downstream GCM HTTP message.

type HTTPResponse added in v1.5.0

type HTTPResponse struct {
	StatusCode   int          `json:"-"`
	MulticastID  int64        `json:"multicast_id"`
	Success      uint         `json:"success"`
	Failure      uint         `json:"failure"`
	CanonicalIds uint         `json:"canonical_ids"`
	Results      []HTTPResult `json:"results,omitempty"`
	// Topic message HTTP response only.
	MessageID uint   `json:"message_id,omitempty"`
	Error     string `json:"error,omitempty"`
}

HTTPResponse is the GCM connection server response to an HTTP downstream message.

type HTTPResult added in v1.5.0

type HTTPResult struct {
	MessageID      string `json:"message_id,omitempty"`
	RegistrationID string `json:"registration_id,omitempty"`
	Error          string `json:"error,omitempty"`
}

HTTPResult represents the result of a single processed HTTP request.

type MessageHandler

type MessageHandler func(cm CCSMessage) error

MessageHandler is the type for a function that handles a CCS message.

type Notification

type Notification struct {
	// Common fields.
	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 fields.
	Icon  string `json:"icon,omitempty"`
	Tag   string `json:"tag,omitempty"`
	Color string `json:"color,omitempty"`

	// iOS-only fields
	Badge          string `json:"badge,omitempty"`
	MutableContent bool   `json:"mutable_content,omitempty"`
}

Notification defines the notification payload of a GCM message. NOTE: contains keys for both Android and iOS notifications.

type XMPPMessage added in v1.5.0

type XMPPMessage struct {
	To                       string        `json:"to,omitempty"`
	MessageID                string        `json:"message_id"`
	MessageType              string        `json:"message_type,omitempty"`
	CollapseKey              string        `json:"collapse_key,omitempty"`
	Priority                 string        `json:"priority,omitempty"`
	ContentAvailable         bool          `json:"content_available,omitempty"`
	MutableContent           bool          `json:"mutable_content,omitempty"`
	TimeToLive               *uint         `json:"time_to_live,omitempty"`
	DeliveryReceiptRequested bool          `json:"delivery_receipt_requested,omitempty"`
	DryRun                   bool          `json:"dry_run,omitempty"`
	Data                     Data          `json:"data,omitempty"`
	Notification             *Notification `json:"notification,omitempty"`
}

XMPPMessage defines a downstream GCM XMPP message.

Directories

Path Synopsis
cmd
gcm-logger command
Program gcm-logger logs and echoes as a GCM "server".
Program gcm-logger logs and echoes as a GCM "server".

Jump to

Keyboard shortcuts

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