gcm

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 6 Imported by: 0

README

gcm

NOTE: I no longer maintain this library. Feel free to fork! :)

The Android SDK provides a nice convenience library (com.google.android.gcm.server) that greatly simplifies the interaction between Java-based application servers and Google's GCM servers. However, Google has not provided much support for application servers implemented in languages other than Java, specifically those written in the Go programming language. The gcm package helps to fill in this gap, providing a simple interface for sending GCM messages and automatically retrying requests in case of service unavailability.

Documentation: http://godoc.org/github.com/alexjlockwood/gcm

Getting Started

To install gcm, use go get:

go get github.com/alexjlockwood/gcm

Import gcm with the following:

import "github.com/alexjlockwood/gcm"

Sample Usage

Here is a quick sample illustrating how to send a message to the GCM server:

package main

import (
	"fmt"
	"net/http"

	"github.com/alexjlockwood/gcm"
)

func main() {
	// Create the message to be sent.
	data := map[string]interface{}{"score": "5x1", "time": "15:10"}
	regIDs := []string{"4", "8", "15", "16", "23", "42"}
	msg := gcm.NewMessage(data, regIDs...)

	// Create a Sender to send the message.
	sender := &gcm.Sender{ApiKey: "sample_api_key"}

	// Send the message and receive the response after at most two retries.
	response, err := sender.Send(msg, 2)
	if err != nil {
		fmt.Println("Failed to send message:", err)
		return
	}

	/* ... */
}

Note for Google AppEngine users

If your application server runs on Google AppEngine, you must import the appengine/urlfetch package and create the Sender as follows:

package sample

import (
	"appengine"
	"appengine/urlfetch"

	"github.com/alexjlockwood/gcm"
)

func handler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	client := urlfetch.Client(c)
	sender := &gcm.Sender{ApiKey: "sample_api_key", Http: client}

	/* ... */
}        

Documentation

Index

Constants

View Source
const (
	ResponseErrorMissingRegistration = "MissingRegistration"
	ResponseErrorInvalidRegistration = "InvalidRegistration"
	ResponseErrorMismatchSenderID    = "MismatchSenderId"
	ResponseErrorNotRegistered       = "NotRegistered"
	ResponseErrorMessageTooBig       = "MessageTooBig"
	ResponseErrorInvalidDataKey      = "InvalidDataKey"
	ResponseErrorInvalidTTL          = "InvalidTtl"
	ResponseErrorUnavailable         = "Unavailable"
	ResponseErrorInternalServerError = "InternalServerError"
	ResponseErrorInvalidPackageName  = "InvalidPackageName"
)
View Source
const (
	AndroidNotificationIcon = "ic_notification"
)

Variables

This section is empty.

Functions

func NewMessage

func NewMessage(data map[string]string, notification *messaging.Notification, tokens ...string) *messaging.MulticastMessage

NewMessage returns a new Message with the specified payload and Token(s).

Types

type JSONParseError

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

type Sender

type Sender struct {
	CredentialsJson string
	Client          *fcm.Client
}

Sender abstracts the interaction between the application server and the GCM server. The developer must obtain an API key from the Google APIs Console page and pass it to the Sender so that it can perform authorized requests on the application server's behalf. To send a message to one or more devices use the Sender's Send or SendNoRetry methods.

If Sender Client is nil, checkSender will automatically initialize a Client

func handler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	sender := &gcm.Sender{CredentialsJson: key}

	/* ... */
}

func (*Sender) Send

func (s *Sender) Send(msg *messaging.MulticastMessage, retries int) (*messaging.BatchResponse, []string, error)

Send sends a message to the GCM server, retrying in case of service unavailability. A non-nil error is returned if a non-recoverable error occurs (i.e. if the response status is not "200 OK").

Note that messages are retried using exponential backoff, and as a result, this method may block for several seconds.

func (*Sender) SendNoRetry

func (s *Sender) SendNoRetry(msg *messaging.MulticastMessage) (*messaging.BatchResponse, []string, error)

SendNoRetry 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. If msg is a valid MulticastMessage, then the failed tokens will also be returned.

type UnauthorizedError

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

type UnknownError

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

Jump to

Keyboard shortcuts

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