apns

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: MIT Imports: 18 Imported by: 0

README

APNs Provider

HTTP/2 Apple Push Notification service (APNs) provider for Go with token-based connection

Example:

key, err := apns.AuthKeyFromFile("AuthKey_XXXXXXXXXX.p8")
if err != nil {
	log.Fatal(err)
}

token := apns.NewToken(key, "XXXXXXXXXX", "YYYYYYYYYY")
client := apns.NewClient(token, nil)

n := &apns.Notification{
	DeviceToken: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
	Host:        apns.HostDevelopment,
	Topic:       "com.example.app",
	Payload: apns.BuildPayload(&apns.APS{
		Alert: "Hi",
	}, nil),
}

fmt.Println(client.Push(n))

Documentation

Index

Constants

View Source
const (
	HostDevelopment         = "https://api.sandbox.push.apple.com"
	HostDevelopmentPort2197 = "https://api.sandbox.push.apple.com:2197"
	HostProduction          = "https://api.push.apple.com"
	HostProductionPort2197  = "https://api.push.apple.com:2197"
)
View Source
const (
	PushTypeAlert        = "alert"
	PushTypeBackground   = "background"
	PushTypeVoIP         = "voip"
	PushTypeComplication = "complication"
	PushTypeFileprovider = "fileprovider"
	PushTypeMDM          = "mdm"
)
View Source
const (
	PriorityLow  = 5
	PriorityHigh = 10
)
View Source
const SoundDefault = "default"
View Source
const StatusSent = http.StatusOK

Variables

This section is empty.

Functions

func AuthKeyFromBytes

func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error)

func AuthKeyFromFile

func AuthKeyFromFile(filename string) (*ecdsa.PrivateKey, error)

func BuildPayload

func BuildPayload(aps *APS, custom map[string]interface{}) map[string]interface{}

func GenerateBearer

func GenerateBearer(key *ecdsa.PrivateKey, keyId, teamId string, issuedAt int64) (string, error)

Types

type APS

type APS struct {
	Alert            interface{} `json:"alert,omitempty"`
	Badge            interface{} `json:"badge,omitempty"`
	Sound            interface{} `json:"sound,omitempty"`
	ThreadID         string      `json:"thread-id,omitempty"`
	Category         string      `json:"category,omitempty"`
	ContentAvailable int         `json:"content-available,omitempty"`
	MutableContent   int         `json:"mutable-content,omitempty"`
	TargetContentID  string      `json:"target-content-id,omitempty"`
	URLArgs          []string    `json:"url-args,omitempty"`
}

type Alert

type Alert struct {
	Title           string   `json:"title,omitempty"`
	Subtitle        string   `json:"subtitle,omitempty"`
	Body            string   `json:"body,omitempty"`
	LaunchImage     string   `json:"launch-image,omitempty"`
	TitleLocKey     string   `json:"title-loc-key,omitempty"`
	TitleLocArgs    []string `json:"title-loc-args,omitempty"`
	SubtitleLocKey  string   `json:"subtitle-loc-key,omitempty"`
	SubtitleLocArgs []string `json:"subtitle-loc-args,omitempty"`
	LocKey          string   `json:"loc-key,omitempty"`
	LocArgs         []string `json:"loc-args,omitempty"`
	Action          string   `json:"action,omitempty"`
	ActionLocKey    string   `json:"action-loc-key,omitempty"`
	SummaryArg      string   `json:"summary-arg,omitempty"`
	SummaryArgCount int      `json:"summary-arg-count,omitempty"`
}

type Client

type Client struct {
	Token      *Token
	HTTPClient *http.Client
}

func NewClient

func NewClient(token *Token, httpClient *http.Client) *Client

func (*Client) Push

func (c *Client) Push(n *Notification) (*Response, error)

func (*Client) PushWithContext

func (c *Client) PushWithContext(ctx context.Context, n *Notification) (*Response, error)

type Notification

type Notification struct {
	DeviceToken string
	Host        string

	ID         string // header: apns-id
	Topic      string // header: apns-topic
	PushType   string // header: apns-push-type
	Expiration string // header: apns-expiration
	Priority   int    // header: apns-priority
	CollapseID string // header: apns-collapse-id

	Payload interface{}
}

func (*Notification) BuildRequest

func (n *Notification) BuildRequest() (*http.Request, error)

func (*Notification) BuildRequestWithContext

func (n *Notification) BuildRequestWithContext(ctx context.Context) (*http.Request, error)

func (*Notification) MarshalJSON

func (n *Notification) MarshalJSON() ([]byte, error)

func (*Notification) Path

func (n *Notification) Path() string

func (*Notification) SetHeaders

func (n *Notification) SetHeaders(h http.Header)

func (*Notification) URL

func (n *Notification) URL() string

type Response

type Response struct {
	Id        string // header: apns-id
	Status    int    // header: :status
	Reason    string
	Timestamp int64
}

func ParseResponse

func ParseResponse(r *http.Response) (*Response, error)

type Sound

type Sound struct {
	Critical int     `json:"critical,omitempty"`
	Name     string  `json:"name,omitempty"`
	Volume   float32 `json:"volume,omitempty"`
}

type Token

type Token struct {
	sync.Mutex
	Key             *ecdsa.PrivateKey
	KeyID           string
	TeamID          string
	RefreshInterval int64
	IssuedAt        int64
	Bearer          string
}

func NewToken

func NewToken(key *ecdsa.PrivateKey, keyID, teamID string) *Token

func (*Token) Expired

func (t *Token) Expired() bool

func (*Token) Generate

func (t *Token) Generate() (string, error)

func (*Token) GenerateIfExpired

func (t *Token) GenerateIfExpired() (string, error)

func (*Token) SetAuthorization

func (t *Token) SetAuthorization(h http.Header) error

Jump to

Keyboard shortcuts

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