webpush

package
v0.0.0-...-db4472a Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 7 Imported by: 0

README

Webpush Notifications

go.dev reference

Prerequisites

Generate VAPID Public and Private Keys for the notification service. This can be done using many tools, one of which is GenerateVAPIDKeys from webpush-go.

Compatibility

This service is compatible with the Web Push Protocol and VAPID.

For a list of compatible browsers, see this for the Push API and this for the Web Notifications.

Usage

package main

import (
	"context"
	"log"

	"github.com/nikoksr/notify"
	"github.com/nikoksr/notify/service/webpush"
)

const vapidPublicKey = "..."  // Add a vapidPublicKey
const vapidPrivateKey = "..." // Add a vapidPrivateKey

func main() {
	subscription := webpush.Subscription{
		Endpoint: "https://your-endpoint",
		Keys: {
			Auth:   "...",
			P256dh: "...",
		},
	}

	webpushSvc := webpush.New(vapidPublicKey, vapidPrivateKey)
	webpushSvc.AddReceivers(subscription)

	notifier := notify.NewWithServices(webpushSvc)

	if err := notifier.Send(context.Background(), "TEST", "Message using golang notifier library"); err != nil {
		log.Fatalf("notifier.Send() failed: %s", err.Error())
	}

	log.Println("Notification sent successfully")
}

Documentation

Overview

Package webpush provides a service for sending messages to viber.

Usage:

package main

import (
    "context"
    "log"

    "github.com/nikoksr/notify"
    "github.com/nikoksr/notify/service/webpush"

)

const vapidPublicKey = "..."  // Add a vapidPublicKey
const vapidPrivateKey = "..." // Add a vapidPrivateKey

func main() {
        subscription := webpush.Subscription{
            Endpoint: "https://your-endpoint",
            Keys: {
                Auth:   "...",
                P256dh: "...",
            },
        }

        webpushSvc := webpush.New(vapidPublicKey, vapidPrivateKey)
        webpushSvc.AddReceivers(subscription)

        notifier := notify.NewWithServices(webpushSvc)

        if err := notifier.Send(context.Background(), "TEST", "Message using golang notifier library"); err != nil {
            log.Fatalf("notifier.Send() failed: %s", err.Error())
        }

        log.Println("Notification sent successfully")
    }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithData

func WithData(ctx context.Context, data map[string]interface{}) context.Context

WithData binds the data to the context so that it will be used by the Service.Send method automatically. Data is a map[string]interface{} and acts as a metadata field that is sent along with the message payload.

func WithOptions

func WithOptions(ctx context.Context, options Options) context.Context

WithOptions binds the options to the context so that they will be used by the Service.Send method automatically. Options are settings that allow you to customize the sending behavior of a message.

Types

type Options

type Options = webpush.Options

Options are optional settings for the sending of a message. It's a type alias for webpush.Options.

type Service

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

Service encapsulates the webpush notification system along with the internal state

func New

func New(vapidPublicKey string, vapidPrivateKey string) *Service

New returns a new instance of the Service

func (*Service) AddReceivers

func (s *Service) AddReceivers(subscriptions ...Subscription)

AddReceivers adds one or more subscriptions to the Service.

func (*Service) Send

func (s *Service) Send(ctx context.Context, subject, message string) error

Send sends a message to all the webpush subscriptions that have been added to the Service. The subject and message arguments are the subject and message of the messagePayload payload. The context can be used to optionally add options and data to the messagePayload payload. See the WithOptions and WithData functions.

type Subscription

type Subscription = webpush.Subscription

Subscription is a JSON representation of a webpush subscription. It's a type alias for webpush.Subscription.

type Urgency

type Urgency = webpush.Urgency

Urgency indicates the importance of the message. It's a type alias for webpush.Urgency.

var (
	// UrgencyVeryLow requires device state: on power and Wi-Fi
	UrgencyVeryLow Urgency = webpush.UrgencyVeryLow

	// UrgencyLow requires device state: on either power or Wi-Fi
	UrgencyLow Urgency = webpush.UrgencyLow

	// UrgencyNormal excludes device state: low battery
	UrgencyNormal Urgency = webpush.UrgencyNormal

	// UrgencyHigh admits device state: low battery
	UrgencyHigh Urgency = webpush.UrgencyHigh
)

These are exposed Urgency constants from the webpush package.

Jump to

Keyboard shortcuts

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