ccs

package
v0.0.0-...-08c1e33 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2016 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ccs provides GCM CCS (Cloud Connection Server) client implementation using XMPP. https://developer.android.com/google/gcm/ccs.html

Example

Example demonstrating the use of CCS implementation in an application server.

package main

import (
	"log"

	"github.com/soygul/gcm/ccs"
)

// Example demonstrating the use of CCS implementation in an application server.
func main() {
	c, err := ccs.Connect("gcm-preprod.googleapis.com:5236", "gcm_sender_id", "gcm_api_key", true)
	if err != nil {
		log.Fatalf("GCM CCS connection cannot be established")
	}

	// Send a test message. Replace "device_registration_id" with an actual GCM registration ID from a device.
	n, err := c.Send(&ccs.OutMsg{To: "device_registration_id", Data: map[string]string{"test_message": "GCM CCS client testing message."}})
	if err != nil {
		log.Printf("Failed to send message to CCS server with error: %v\n", err)
	}
	log.Printf("Message sent with %v bytes written to the connection\n", n)

	// Start receiving messages from the CCS server.
	for {
		log.Println("Waiting for incoming CCS messages")
		m, err := c.Receive()
		if err != nil {
			log.Printf("Incoming CCS error: %v\n", err)
		}

		go handleMessage(m)
	}
}

func handleMessage(m *ccs.InMsg) {
	log.Printf("Incoming CCS message: %v\n", m)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Host, SenderID string
	// contains filtered or unexported fields
}

Conn is a GCM CCS connection.

func Connect

func Connect(host, senderID, apiKey string, debug bool) (*Conn, error)

Connect connects to GCM CCS server denoted by host (production or staging CCS endpoint URI) along with relevant credentials. Debug mode dumps all CSS communications to stdout.

func (*Conn) Close

func (c *Conn) Close() error

Close a CSS connection.

func (*Conn) Receive

func (c *Conn) Receive() (*InMsg, error)

Receive waits to receive the next incoming messages from the CCS connection.

func (*Conn) Send

func (c *Conn) Send(m *OutMsg) (n int, err error)

Send sends a message to GCM CCS server and returns the number of bytes written and any error encountered. If empty message ID is given, it's auto-generated and message object is modified with the generated ID.

type InMsg

type InMsg struct {
	From        string            `json:"from"`
	ID          string            `json:"message_id"`
	Category    string            `json:"category"`
	Data        map[string]string `json:"data"`
	MessageType string            `json:"message_type"`
	ControlType string            `json:"control_type"`
	Err         string            `json:"error"`
	ErrDesc     string            `json:"error_description"`
}

InMsg is an incoming GCM CCS message.

type OutMsg

type OutMsg struct {
	To                       string            `json:"to"`
	ID                       string            `json:"message_id"`
	Data                     map[string]string `json:"data,omitempty"`
	MessageType              string            `json:"message_type,omitempty"`
	CollapseKey              string            `json:"collapse_key,omitempty"`
	TimeToLive               int               `json:"time_to_live,omitempty"` //default:2419200 (in seconds = 4 weeks)
	ContentAvailable         bool              `json:"content_available,omitempty"`
	Priority                 string            `json:"priority,omitempty"`
	DelayWhileIdle           bool              `json:"delay_while_idle,omitempty"`           //default:false
	DeliveryReceiptRequested bool              `json:"delivery_receipt_requested,omitempty"` //default:false
}

OutMsg is a message to be sent to GCM CCS. If ID field is not set, it will be generated automatically using crypto/rand. Google recommends Data field to be strings key/value pairs and keys cannot be reserved words described in GCM server documentation. https://developer.android.com/google/gcm/ccs.html#format

Jump to

Keyboard shortcuts

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