fcm

package module
v0.0.0-...-2d832b9 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2021 License: MIT Imports: 9 Imported by: 1

README

Fcm Http client

Library uses legacy http protocol for sending messages to devices using device token: https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_using_the_legacy_app_server_protocols

Documentation for initial gcm library, which was replaced with FCM by changing message endpoint: http://godoc.org/github.com/alexjlockwood/gcm

Getting Started

To install use go get:

go get github.com/Smarp/fcm-http

Import with the following:

import "github.com/Smarp/fcm-http"

Sample Usage

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

package main

import (
	"fmt"
	"net/http"

	fcm "github.com/Smarp/fcm-http"
)

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 := fcm.NewMessage(data, regIDs...)

	// Create a Sender to send the message.
	sender := &fcm.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"

	fcm "github.com/Smarp/fcm-http"
)

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

	/* ... */
}        

Documentation

Index

Constants

View Source
const (
	// FcmEndpoint is the endpoint for sending messages to the FCM server.
	FcmEndpoint = "https://fcm.googleapis.com/fcm/send"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	RegistrationIDs       []string               `json:"registration_ids"`
	CollapseKey           string                 `json:"collapse_key,omitempty"`
	Data                  map[string]interface{} `json:"data,omitempty"`
	DelayWhileIdle        bool                   `json:"delay_while_idle,omitempty"`
	TimeToLive            int                    `json:"time_to_live,omitempty"`
	RestrictedPackageName string                 `json:"restricted_package_name,omitempty"`
	DryRun                bool                   `json:"dry_run,omitempty"`
	Notification          Notification           `json:"notification"`
	// contains filtered or unexported fields
}

Message is used by the application server to send a message to the FCM server. See the documentation for FCM Architectural Overview for more information: https://firebase.google.com/docs/cloud-messaging/http-server-ref

func NewMessage

func NewMessage(data map[string]interface{}, regIDs ...string) *Message

NewMessage returns a new Message with the specified payload and registration IDs. @DEPRECATED as no validation here and client should create itself freely

func (*Message) WithContext

func (m *Message) WithContext(ctx context.Context)

WithContext attach context to message

type Notification

type Notification struct {
	Title string `json:"title"`
	Body  string `json:"body"`
}

type Response

type Response struct {
	MulticastID  int64    `json:"multicast_id"`
	Success      int      `json:"success"`
	Failure      int      `json:"failure"`
	CanonicalIDs int      `json:"canonical_ids"`
	Results      []Result `json:"results"`
}

Response represents the FCM server's response to the application server's sent message. See the documentation for FCM Architectural

type Result

type Result struct {
	MessageID      string `json:"message_id"`
	RegistrationID string `json:"registration_id"`
	Error          string `json:"error"`
}

Result represents the status of a processed message.

type Sender

type Sender struct {
	ApiKey string
	Http   *http.Client
}

func (*Sender) Send

func (s *Sender) Send(msg *Message, retries int) (*Response, error)

Send sends a message to the FCM 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 *Message) (*Response, 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 (i.e. if the response status is not "200 OK").

Jump to

Keyboard shortcuts

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