payload

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package payload prepares a JSON payload to push.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrIncomplete = errors.New("payload does not contain necessary fields")
)

Validation errors.

Functions

This section is empty.

Types

type APS

type APS struct {
	// Alert dictionary.
	Alert Alert

	// Badge to display on the app icon.
	// Set to badge.Preserve (default), badge.Clear
	// or a specific value with badge.New(n).
	Badge badge.Badge

	// Details how to play alert.
	Sound Sound

	// Thread identifier to create notification groups in iOS 12 or newer.
	ThreadID string

	// Category identifier for custom actions in iOS 8 or newer.
	Category string

	// Content available is for silent notifications
	// with no alert, sound, or badge.
	ContentAvailable bool

	// Mutable is used for Service Extensions introduced in iOS 10.
	MutableContent bool

	// Content identifier.
	TargetContentID string

	// URL arguments for Safari pushes: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html#//apple_ref/doc/uid/TP40013225-CH3-SW17
	SafariURLArgs []string

	InterruptionLevel InterruptionLevel

	RelevanceScore float32
}

APS is Apple's reserved namespace. Use it for payloads destined to mobile devices (iOS).

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/alexei-g-aloteq/buford/payload"
	"github.com/alexei-g-aloteq/buford/payload/badge"
)

func main() {
	p := payload.APS{
		Alert: payload.Alert{Body: "Hello HTTP/2"},
		Badge: badge.New(42),
		Sound: payload.Sound{SoundName: "bingbong.aiff"},
	}

	b, err := json.Marshal(p)
	if err != nil {
		// handle error
	}
	fmt.Printf("%s", b)
}
Output:

{"aps":{"alert":"Hello HTTP/2","badge":42,"sound":"bingbong.aiff"}}

func (*APS) Map

func (a *APS) Map() map[string]interface{}

Map returns the payload as a map that you can customize before serializing it to JSON.

Example

Use Map to add custom values to the payload.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/alexei-g-aloteq/buford/payload"
)

func main() {
	p := payload.APS{
		Alert: payload.Alert{Body: "Topic secret message"},
	}
	pm := p.Map()
	pm["acme2"] = []string{"bang", "whiz"}

	b, err := json.Marshal(pm)
	if err != nil {
		// handle error
	}
	fmt.Printf("%s", b)
}
Output:

{"acme2":["bang","whiz"],"aps":{"alert":"Topic secret message"}}

func (APS) MarshalJSON

func (a APS) MarshalJSON() ([]byte, error)

MarshalJSON allows you to json.Marshal(aps) directly.

func (*APS) Validate

func (a *APS) Validate() error

Validate that a payload has the correct fields.

Example
package main

import (
	"fmt"

	"github.com/alexei-g-aloteq/buford/payload"
	"github.com/alexei-g-aloteq/buford/payload/badge"
)

func main() {
	p := payload.APS{
		Badge: badge.Preserve,
		Sound: payload.Sound{SoundName: "bingbong.aiff"},
	}
	if err := p.Validate(); err != nil {
		fmt.Println(err)
	}
}
Output:

payload does not contain necessary fields

type Alert

type Alert struct {
	// Title is a short string shown briefly on Apple Watch in iOS 8.2 or newer.
	Title        string   `json:"title,omitempty"`
	TitleLocKey  string   `json:"title-loc-key,omitempty"`
	TitleLocArgs []string `json:"title-loc-args,omitempty"`

	// Subtitle added in iOS 10
	Subtitle        string   `json:"subtitle,omitempty"`
	SubtitleLocKey  string   `json:"subtitle-loc-key,omitempty"`
	SubtitleLocArgs []string `json:"subtitle-loc-args,omitempty"`

	// Body text of the alert message.
	Body    string   `json:"body,omitempty"`
	LocKey  string   `json:"loc-key,omitempty"`
	LocArgs []string `json:"loc-args,omitempty"`

	// Key for localized string for "View" button.
	ActionLocKey string `json:"action-loc-key,omitempty"`

	// Image file to be used when user taps or slides the action button.
	LaunchImage string `json:"launch-image,omitempty"`

	// String for "View" button on Safari.
	SafariAction string `json:"action,omitempty"`
}

Alert dictionary.

type Browser

type Browser struct {
	Alert   BrowserAlert
	URLArgs []string
}

Browser for Safari Push Notifications.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/alexei-g-aloteq/buford/payload"
)

func main() {
	p := payload.Browser{
		Alert: payload.BrowserAlert{
			Title:  "Flight A998 Now Boarding",
			Body:   "Boarding has begun for Flight A998.",
			Action: "View",
		},
		URLArgs: []string{"boarding", "A998"},
	}

	b, err := json.Marshal(p)
	if err != nil {
		// handle error
	}
	fmt.Printf("%s", b)
}
Output:

{"aps":{"alert":{"title":"Flight A998 Now Boarding","body":"Boarding has begun for Flight A998.","action":"View"},"url-args":["boarding","A998"]}}

func (Browser) MarshalJSON

func (p Browser) MarshalJSON() ([]byte, error)

MarshalJSON allows you to json.Marshal(browser) directly.

func (*Browser) Validate

func (p *Browser) Validate() error

Validate browser payload.

type BrowserAlert

type BrowserAlert struct {
	// Title and Body are required
	Title string `json:"title"`
	Body  string `json:"body"`
	// Action button label (defaults to "Show")
	Action string `json:"action,omitempty"`
}

BrowserAlert for Safari Push Notifications.

type InterruptionLevel added in v0.16.0

type InterruptionLevel string
const (
	InterruptionLevelPassive       InterruptionLevel = "passive"
	InterruptionLevelActive        InterruptionLevel = "active"
	InterruptionLevelTimeSensitive InterruptionLevel = "time-sensitive"
	InterruptionLevelCritical      InterruptionLevel = "critical"
)

type MDM

type MDM struct {
	Token string `json:"mdm"`
}

MDM payload for mobile device management.

func (*MDM) Validate

func (p *MDM) Validate() error

Validate MDM payload.

type Sound added in v0.15.0

type Sound struct {
	SoundName      string  `json:"name,omitempty"`
	IsCritical     int     `json:"critical,omitempty"`
	CriticalVolume float32 `json:"volume,omitempty"`
}

Sound dictionary.

Directories

Path Synopsis
Package badge allows you to preserve, set or clear the number displayed on your App icon.
Package badge allows you to preserve, set or clear the number displayed on your App icon.

Jump to

Keyboard shortcuts

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