fcm

package module
v0.0.0-...-642309a Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2018 License: MIT Imports: 6 Imported by: 0

README

goFCM : Firebase Cloud Messaging Library

Usage

go get github.com/NyanStudio/goFCM

Example

Send notification to a registration token

package main

import (
	"fmt"
	"github.com/NyanStudio/goFCM"
)

const (
	serverKey = "YOUR_SERVER_KEY"
)

func main() {
	cm := new(fcm.Client)

	cm.SetServerKey(serverKey)

	cm.SetTo("REGISTRATION_TOKEN")

	cm.SetNotification(fcm.NotificationPayload{Title: "title", Body: "body"})

	rm, err := cm.SendMessage()
	if err != nil {
		fmt.Sprintln("ERR: %v", err)
	}

	fmt.Sprintln("RM: %v", rm)
}

Send notification to registration tokens

package main

import (
	"fmt"
	"github.com/NyanStudio/goFCM"
)

const (
	serverKey = "YOUR_SERVER_KEY"
)

func main() {
	cm := new(fcm.Client)

	cm.SetServerKey(serverKey)

	cm.SetRegistrationIds([]string{"REGISTRATION_TOKEN1", "REGISTRATION_TOKEN2"})

	cm.SetNotification(fcm.NotificationPayload{Title: "title", Body: "body"})

	rm, err := cm.SendMessage()
	if err != nil {
		fmt.Sprintln("ERR: %v", err)
	}

	fmt.Sprintln("RM: %v", rm)
}

Send notification to a topic

package main

import (
	"fmt"
	"github.com/NyanStudio/goFCM"
)

const (
	serverKey = "YOUR_SERVER_KEY"
)

func main() {
	cm := new(fcm.Client)

	cm.SetServerKey(serverKey)

	cm.SetTo("/topics/YOUR_TOPIC")

	cm.SetNotification(fcm.NotificationPayload{Title: "title", Body: "body"})

	rm, err := cm.SendMessage()
	if err != nil {
		fmt.Sprintln("ERR: %v", err)
	}

	fmt.Sprintln("RM: %v", rm)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	ServerKey string
	Message   HTTPMessage
}

Client stores the key and the Message

func (*Client) SendMessage

func (c *Client) SendMessage() (rm *HTTPResponse, err error)

SendMessage to firebase

func (*Client) SendMessageWithHTTPClient

func (c *Client) SendMessageWithHTTPClient(ctx context.Context) (rm *HTTPResponse, err error)

SendMessageWithHTTPClient to firebase

func (*Client) SetCollapseKey

func (c *Client) SetCollapseKey(collapseKey string)

SetCollapseKey identifies a group of messages

func (*Client) SetCondition

func (c *Client) SetCondition(condition string)

SetCondition sets a logical expression of conditions that determine the message target

func (*Client) SetContentAvailable

func (c *Client) SetContentAvailable(contentAvailable bool)

SetContentAvailable use this field to represent content-available in the APNs payload

func (*Client) SetData

func (c *Client) SetData(body interface{})

SetData sets data payload

func (*Client) SetDryRun

func (c *Client) SetDryRun(dryRun bool)

SetDryRun allows developers to test a request without actually sending a message

func (*Client) SetMutableContent

func (c *Client) SetMutableContent(mutableContent bool)

SetMutableContent use this field to represent mutable-content in the APNs payload

func (*Client) SetNotification

func (c *Client) SetNotification(np NotificationPayload)

SetNotification sets the notification payload

func (*Client) SetPriority

func (c *Client) SetPriority(priority string)

SetPriority sets the priority of the message

func (*Client) SetRegistrationIds

func (c *Client) SetRegistrationIds(registrationIds []string)

SetRegistrationIds sets the recipient of a multicast message

func (*Client) SetRestrictedPackageName

func (c *Client) SetRestrictedPackageName(restrictedPackageName string)

SetRestrictedPackageName sets the package name of the application where the registration tokens must match in order to receive the message

func (*Client) SetServerKey

func (c *Client) SetServerKey(serverKey string)

SetServerKey sets the server key

func (*Client) SetTimeToLive

func (c *Client) SetTimeToLive(timeToLive int)

SetTimeToLive sets how long (in seconds) the message should be kept in FCM storage if the device is offline.

func (*Client) SetTo

func (c *Client) SetTo(to string)

SetTo sets the recipient of a message

type HTTPMessage

type HTTPMessage struct {
	To                    string              `json:"to,omitempty"`                      // This parameter specifies the recipient of a message.
	RegistrationIds       []string            `json:"registration_ids,omitempty"`        // This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.
	Condition             string              `json:"condition,omitempty"`               // This parameter specifies a logical expression of conditions that determine the message target.
	NotificationKey       string              `json:"notification_key,omitempty"`        // This parameter is deprecated. Instead, use to to specify message recipients.
	CollapseKey           string              `json:"collapse_key,omitempty"`            // This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or becomes active.
	Priority              string              `json:"priority,omitempty"`                // Sets the priority of the message. Valid values are "normal" and "high." On iOS, these correspond to APNs priorities 5 and 10.
	ContentAvailable      bool                `json:"content_available,omitempty"`       // On iOS, use this field to represent content-available in the APNs payload. When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server. Note that silent notifications in APNs are not guaranteed to be delivered, and can depend on factors such as the user turning on Low Power Mode, force quitting the app, etc. On Android, data messages wake the app by default. On Chrome, currently not supported.
	MutableContent        bool                `json:"mutable_content,omitempty"`         // Currently for iOS 10+ devices only. On iOS, use this field to represent mutable-content in the APNs payload. When a notification is sent and this is set to true, the content of the notification can be modified before it is displayed, using a Notification Service app extension.
	DelayWhileIdle        bool                `json:"delay_while_idle,omitempty"`        // This parameter is deprecated.
	TimeToLive            int                 `json:"time_to_live,omitempty"`            // This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline. The maximum time to live supported is 4 weeks, and the default value is 4 weeks.
	RestrictedPackageName string              `json:"restricted_package_name,omitempty"` // This parameter specifies the package name of the application where the registration tokens must match in order to receive the message.
	DryRun                bool                `json:"dry_run,omitempty"`                 // This parameter, when set to true, allows developers to test a request without actually sending a message.
	Data                  interface{}         `json:"data,omitempty"`                    // This parameter specifies the custom key-value pairs of the message's payload.
	Notification          NotificationPayload `json:"notification,omitempty"`            // This parameter specifies the predefined, user-visible key-value pairs of the notification payload.
}

HTTPMessage -> Downstream HTTP messages

type HTTPResponse

type HTTPResponse struct {
	StatusCode   int
	MulticastID  int64                `json:"multicast_id"`
	Success      int                  `json:"success"`
	Failure      int                  `json:"failure"`
	CanonicalIds int                  `json:"canonical_ids"`
	Results      []HTTPResponseResult `json:"results,omitempty"`
	MessageID    int64                `json:"message_id,omitempty"`
	Error        string               `json:"error,omitempty"`
}

HTTPResponse -> Downstream HTTP message response

type HTTPResponseResult

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

HTTPResponseResult -> Downstream HTTP message response result

type NotificationPayload

type NotificationPayload struct {
	Title            string `json:"title,omitempty"`              // The notification's title.(iOS,Android,Web)
	Body             string `json:"body,omitempty"`               // The notification's body text.(iOS,Android,Web)
	AndroidChannelID string `json:"android_channel_id,omitempty"` // The notification's channel id (new in Android O).(Android)
	Icon             string `json:"icon,omitempty"`               // The notification's icon.(Android,Web)
	Sound            string `json:"sound,omitempty"`              // The sound to play when the device receives the notification.(iOS,Android)
	Badge            string `json:"badge,omitempty"`              // The value of the badge on the home screen app icon.(iOS)
	Tag              string `json:"tag,omitempty"`                // Identifier used to replace existing notifications in the notification drawer.(Android)
	Color            string `json:"color,omitempty"`              // The notification's icon color, expressed in #rrggbb format.(Android)
	ClickAction      string `json:"click_action,omitempty"`       // The action associated with a user click on the notification.(iOS,Android,Web)
	Subtitle         string `json:"subtitle,omitempty"`           // The notification's subtitle.(iOS)
	BodyLocKey       string `json:"body_loc_key,omitempty"`       // The key to the body string in the app's string resources to use to localize the body text to the user's current localization.(iOS,Android)
	BodyLocArgs      string `json:"body_loc_args,omitempty"`      // Variable string values to be used in place of the format specifiers in body_loc_key to use to localize the body text to the user's current localization.(iOS,Android)
	TitleLocKey      string `json:"title_loc_key,omitempty"`      // The key to the title string in the app's string resources to use to localize the title text to the user's current localization.(iOS,Android)
	TitleLocArgs     string `json:"title_loc_args,omitempty"`     // Variable string values to be used in place of the format specifiers in title_loc_key to use to localize the title text to the user's current localization.(iOS,Android)
}

NotificationPayload -> Notification payload support

Jump to

Keyboard shortcuts

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