pushnotification

package module
v0.0.0-...-57140b9 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: MIT Imports: 8 Imported by: 0

README

pushnotification

License

Package pushnotification sends push notification via Amazon SNS based on the device token. Provide the type of the device and optional the EndpointArn. The EndpointArn will be created on the fly if needed. This can be stored afterwards for future use. Device types ios, android and windows are supported by default, for other platforms you need to provide the device type. If APNSSandbox is provided and the type is ios, a pushnotofication will be sent to both APNS and APNSSandbox, the endpoint for APNS will be populated. When you need the endpoint for APNSSandbox, don't provide APNSSandbox but a custom type in platforms and provide a custom type for the device.

Example

package main

import (
	"log"
	"os"

	"github.com/capptions/pushnotification"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/joho/godotenv"
)

func main() {
	if err := godotenv.Load(); err != nil {
		log.Fatal(err)
	}

	svc := Service{
		Key:    os.Getenv("KEY"),
		Secret: os.Getenv("SECRET"),
		Region: os.Getenv("REGION"),
		// provide at least one of these, based on the devicetype
		APNS:        os.Getenv("APNS"),
		APNSSandbox: os.Getenv("APNSSandbox"),
		GCM:         os.Getenv("GCM"),
		Windows:     os.Getenv("WINDOWS"),
		Platforms: map[string]string{
			"baidu": os.Getenv("baidu"),
		},
	}

	device := &pushnotification.Device{
		Token:       os.Getenv("DEVICE_TOKEN"),
		Type:        os.Getenv("DEVICE_TYPE"), //'ios|android|windows|custom'
		EndpointArn: os.Getenv("DEVICE_ENDPOINT_ARN"), //optional
	}

	err := svc.Send(device, &pushnotification.Data{
		Subject: aws.String("Subject"),
		Alert:   aws.String("Nice test message"),
		Sound:   aws.String("default"),
		Badge:   aws.Int(1),
		Data: map[string]interface{}{
			"entity": "57cd5acb3b983b0b00bdabf7",
			"topic":  "Nice topic",
			"data": map[string]interface{}{
				"evenmore": "568cef4ca041160a00911dd6",
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
}
KEY=your_key SECRET=your_secret REGION=eu-west-1 APNS=app_ARN DEVICE_TOKEN=device_token DEVICE_TYPE=ios go test

Setup SNS stuff on Amazon

Amazon SNS

Create platform application for each supported device type in Amazon SNS and remember the application ARN.

Amazon IAM

Create a user and add this policy. The 'resources' should list the application ARNs you created.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1472041226000",
            "Effect": "Allow",
            "Action": [
                "sns:Publish",
                "sns:CreatePlatformEndpoint",
                "sns:GetEndpointAttributes",
                "sns:SetEndpointAttributes"
            ],
            "Resource": [
                "iOS",
                "Android"
            ]
        }
    ]
}
Devices

The endpoints for the device tokens will be added on the fly, you can however store the EndpointArn for reuse. The endpoint will be stored in the Device struct. If the provided endpoint is empty, it will be created, otherwise it will be reused when possible. Function 'IsCreated' will return if the endpoint is (re)created or not.

device := &pushnotification.Device{
	Token:       os.Getenv("DEVICE_TOKEN"),
	Type:        os.Getenv("DEVICE_TYPE"), //'ios|android|windows|custom'
	EndpointArn: os.Getenv("DEVICE_ENDPOINT_ARN"), //optional
}

if device.IsCreated() {
	log.Printf("New endpoint arn '%v' created for device token '%v'", device.EndpointArn, device.Token)
}

License

pushnotification is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Alert   *string                `json:"alert,omitempty"`
	Subject *string                `json:"subject,omitempty"`
	Sound   *string                `json:"sound,omitempty"`
	Badge   *int                   `json:"badge,omitempty"`
	Data    map[string]interface{} `json:"custom_data"`
}

Data is the data of the sending pushnotification.

type Device

type Device struct {
	Token       string
	Type        string
	EndpointArn string
	// contains filtered or unexported fields
}

func (*Device) IsCreated

func (device *Device) IsCreated() bool

type Service

type Service struct {
	Key         string
	Secret      string
	Region      string
	GCM         string
	APNS        string
	APNSSandbox string
	Windows     string
	Platforms   map[string]string
}

Service is the main entry point into using this package.

func (*Service) Send

func (service *Service) Send(device *Device, data *Data) (err error)

Send sends a push notification

Jump to

Keyboard shortcuts

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