expo

package module
v0.0.0-...-5576f70 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2017 License: MIT Imports: 8 Imported by: 0

README

exponent-server-sdk-go

Build Status codecov codebeat badge Go Report Card GoDoc

Server side library for working with Exponent using Go

Installing

To install this library, simply run:

go get github.com/Terminux/exponent-server-sdk-go

Usage

Token Checker

Check if the token is a valid Expo push token

  expo.IsExpoPushToken(token)
Send Single Message
  message := expo.PushMessage{To: token, Body: "content"}

  message.Send()
  // or
  expo.SendPushNotification(&message)
Send Several Messages
  expo.SendPushNotifications([]*expo.PushMessage{
    &expo.PushMessage{To: token1, Body: "first message"},
    &expo.PushMessage{To: token2, Body: "another message"},
    &expo.PushMessage{To: token3, Body: "last message"},
  })
Chunks Messages

Split the chunk messages into several chunks messages

  expo.ChunkPushNotifications(messages)
Example

Here's a sample showcasing many features of expo.

import (
  "fmt"
  "os"

  "github.com/Terminux/exponent-server-sdk-go"
)

func main() {
	token := os.Getenv("EXPO_TOKEN")

	if expo.IsExpoPushToken(token) {
		message := expo.PushMessage{
			To:    token,
			Title: "Notification title",
			Body:  "Notification content",
			Data:  struct{ Value string }{"mydata"}}

		apiRes, apiErr, err := message.Send()
		if err != nil {
			panic(err)
		}

		fmt.Println("apiRes:", apiRes)
		fmt.Println("apiErr:", apiErr)
	}
}

to run example

$ EXPO_TOKEN=your_expo_token go run main.go
Based on

Documentation

Overview

Package expo is used to send push notifications to Expo Experiences from a Go server

A simplest example:

package main
import "github.com/Terminux/exponent-server-sdk-go"

const token = "EXPO_TOKEN"

func main() {
	if expo.IsExpoPushToken(token) {
		message := expo.PushMessage{
			To:    token,
			Title: "Notification title",
			Body:  "Notification content",
			Sound: "default",
			Badge: 1
			Data:  struct{ Value string }{"mydata"}}}

		message.Send()
	}
}

Index

Constants

This section is empty.

Variables

View Source
var ChunkLimit = 100

ChunkLimit allows to set the max message in each chunk. This variable is used on ChunkPushNotifications function. The ChunkLimit can be increase or decrease but it is not recommanded to set higher than 100

View Source
var MaxBodySizeWithoutGzip = 1024

MaxBodySizeWithoutGzip allows to set the max length of body allowed to be send without gzip. The MaxBodySizeWithoutGzip can be increase or decrease but it is not recommanded to set higher than 1024

Functions

func ChunkPushNotifications

func ChunkPushNotifications(messages []*PushMessage) [][]*PushMessage

ChunkPushNotifications returns an array of chunks The chunks size is determined with the ChunkLimit variable

func IsExpoPushToken

func IsExpoPushToken(token string) bool

IsExpoPushToken determines if the token is a Expo push token

func SendPushNotification

func SendPushNotification(message *PushMessage) (*PushNotificationResult, *PushNotificationError, error)

SendPushNotification allows to send the message

func SendPushNotifications

func SendPushNotifications(messages []*PushMessage) (r []*PushNotificationResult, e []*PushNotificationError, err error)

SendPushNotifications allows to send several messages at the same times Is highly recommanded to not send more than 100 messages at once

Types

type PushMessage

type PushMessage struct {
	// To is an Expo push token specifying the recipient of this message.
	To string `json:"to"`

	// Title is the title to display in the notification. On iOS this is displayed only on Apple Watch.
	Title string `json:"title,omitempty"`

	// Body is the push notification content
	Body string `json:"body,omitempty"`

	// Data is a JSON object delivered to your app. It may be up to about 4KiB; the total
	// notification payload sent to Apple and Google must be at most 4KiB or else you will get a "Message Too Big" error.
	Data interface{} `json:"data,omitempty"`

	// Sound to play when the recipient receives this notification.
	// Specify "default" to play the device's default notification sound, or omit this field to play no sound.
	Sound string `json:"sound,omitempty"`

	// TTL (Time to Live) is the number of seconds for which the message may be kept around for redelivery if it hasn't been delivered yet.
	TTL int `json:"ttl"`

	// Expiration is a timestamp since the UNIX epoch specifying when the message expires.
	Expiration int `json:"expiration"`

	// Priority is the delivery priority of the message.
	// Possible values: normal | hight | default or omit field to use the default priority
	Priority string `json:"priority,omitempty"`

	// Badge is the number to display in the badge on the app icon
	Badge int `json:"badge"`
}

PushMessage is the message sended to the Expo api

func (*PushMessage) Send

Send allows to send the current message

type PushNotificationError

type PushNotificationError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Details string `json:"details"`
	Stack   string `json:"stack"`
}

PushNotificationError is the result error returned by the Expo api

type PushNotificationResult

type PushNotificationResult struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Details struct {
		Error string `json:"error"`
	} `json:"details"`
}

PushNotificationResult is the result returned by the Expo api

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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