snsutil

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: LGPL-2.1 Imports: 5 Imported by: 0

README

AWS Simple Notification Service (SNS) wrapper

This package contains wrapper functions for SNS service to simplify the normal usages

Example

import (
  "fmt"
  snsutil "buuuksg/saas-api/internal/util/sns"
)

appArn := "put application arn here"
deviceToken := "put device token here"

// initialize the service with default configuration, using env vars for credentials
snsSvc := snsutil.New()

// register a new device
resp, err := snsSvc.RegisterDevice(appArn, deviceToken)
if err != nil {
  panic(fmt.Errorf("error creating sns endpoint: %+v", err))
}
// grab the arn
deviceArn := *resp.EndpointArn
fmt.Printf("generated sns arn: %s\n", deviceArn)

// sample data for push notification
datapayload := map[string]interface{}{
  "ghost": map[string]interface{}{
    "type":   "message type",
    "field1": "blah blah",
    "field2": "ok",
  },
}

// send the push notification to Android devices
output, err := snsSvc.SendToAndroid(deviceArn, snsutil.FCMPayload{Data: datapayload})
// or iOS devices
output, err = snsSvc.SendToIOS(deviceArn, snsutil.APNSPayload{Data: datapayload})

// or to all devices without knowing the OS
output, err = snsSvc.SendToDevice(deviceArn, snsutil.Message{
  APNS:        &snsutil.APNSPayload{Data: datapayload},
  APNSSandbox: &snsutil.APNSPayload{Data: datapayload},
  FCM:         &snsutil.FCMPayload{Data: datapayload},
})
if err != nil {
  panic(fmt.Errorf("error publishing message: %+v", err))
}
fmt.Printf("publish output: %+v\n", output)

// all done, to remove the device from SNS
_, err = snsSvc.DeregisterDevice(deviceArn)
if err != nil {
  panic(fmt.Errorf("error deregistering device : %+v", err))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APNSAlert

type APNSAlert struct {
	Title    string `json:"title,omitempty"`
	Subtitle string `json:"subtitle,omitempty"`
	Body     string `json:"body,omitempty"`
}

APNSAlert represents the APNS alert object

type APNSNotification

type APNSNotification struct {
	Alert            *APNSAlert `json:"alert,omitempty"`
	Sound            string     `json:"sound,omitempty"`
	Badge            *int       `json:"badge,omitempty"`
	ContentAvailable int        `json:"content-available,omitempty"`
	Category         *string    `json:"category,omitempty"`
	MutableContent   *int       `json:"mutable-content,omitempty"`
}

APNSNotification represents the user-visible of the notification for APNS platform

type APNSPayload

type APNSPayload struct {
	Data         map[string]interface{}
	Notification *APNSNotification
	HighPriority bool
}

APNSPayload represents the APNS message payload

func (APNSPayload) MarshalJSON

func (ap APNSPayload) MarshalJSON() ([]byte, error)

MarshalJSON modifies json marshal output for APNSPayload

type FCMNotification

type FCMNotification struct {
	Title    string `json:"title,omitempty"`
	Subtitle string `json:"subtitle,omitempty"`
	Body     string `json:"body,omitempty"`
	Sound    string `json:"sound,omitempty"`
	Badge    *int   `json:"badge,omitempty"`
}

FCMNotification represents the user-visible of the notification for FCM platform

type FCMPayload

type FCMPayload struct {
	Data         map[string]interface{}
	Notification *FCMNotification
	HighPriority bool
}

FCMPayload represents the FCM message payload

func (FCMPayload) MarshalJSON

func (fp FCMPayload) MarshalJSON() ([]byte, error)

MarshalJSON modifies json marshal output for FCMPayload

type Message

type Message struct {
	APNS        *APNSPayload
	APNSSandbox *APNSPayload
	FCM         *FCMPayload
}

Message represents the message structure for SNS message

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

MarshalJSON modifies json marshal output for Message

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service represents the snsutil service

func New

func New() *Service

New initializes SNS service with default config

func (*Service) CreateAPNSApplication

func (s *Service) CreateAPNSApplication(name string, cert string, key string, sandbox bool) (string, error)

CreateAPNSApplication creates new Apple APNS platform application on SNS service

func (*Service) CreateApplication

func (s *Service) CreateApplication(name string, platform string, attr map[string]*string) (string, error)

CreateApplication creates new platform application on SNS service

func (*Service) CreateFCMApplication

func (s *Service) CreateFCMApplication(name string, key string) (string, error)

CreateFCMApplication creates new FCM platform application on SNS service

func (*Service) DeleteApplication

func (s *Service) DeleteApplication(appArn string) error

DeleteApplication deletes the platform application from SNS service

func (*Service) DeregisterDevice

func (s *Service) DeregisterDevice(endpointArn string) error

DeregisterDevice remove a device from SNS application

func (*Service) RegisterDevice

func (s *Service) RegisterDevice(appArn, deviceToken string) (string, error)

RegisterDevice registers a device with SNS application

func (*Service) SendToAndroid

func (s *Service) SendToAndroid(target string, payload FCMPayload) (string, error)

SendToAndroid sends push notification to an Android device using FCM platform

func (*Service) SendToDevice

func (s *Service) SendToDevice(target string, msg Message) (string, error)

SendToDevice sends push notification to a device. The "msg" should contains correct payload for FCM or APNS plarform, depends on the "target" OS

func (*Service) SendToIOS

func (s *Service) SendToIOS(target string, payload APNSPayload) (string, error)

SendToIOS sends push notification to an IOS device using APNS platform

func (*Service) SendToTopic

func (s *Service) SendToTopic(topic string, msg Message) (string, error)

SendToTopic sends push notification to a topic. The "msg" should contains payload for both FCM and APNS plarforms

func (*Service) UpdateAPNSApplication

func (s *Service) UpdateAPNSApplication(arn string, cert string, key string, sandbox bool) (string, error)

UpdateAPNSApplication updates Apple APNS platform application attributes

func (*Service) UpdateApplication

func (s *Service) UpdateApplication(appArn string, attr map[string]*string) error

UpdateApplication updates the platform application attributes

func (*Service) UpdateFCMApplication

func (s *Service) UpdateFCMApplication(arn string, key string) error

UpdateFCMApplication updates FCM platform application attributes

Jump to

Keyboard shortcuts

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