notification

package
v1.18.1-0...-5af7aae Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ChannelMail is the name of the built-in mail delivery channel.
	ChannelMail = "mail"

	// ChannelDatabase is the name of the built-in database delivery channel.
	ChannelDatabase = "database"
)

Channel name constants. Use these instead of raw string literals in Notification.Via, Notifiable.RouteNotificationFor, and Manager.Route so a typo is caught at compile time rather than silently dropping a route.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel interface {
	// Name returns the unique identifier for this channel, e.g. ChannelMail, ChannelDatabase.
	Name() string

	Send(notifiable Notifiable, notification Notification) error
}

Channel is the interface every delivery driver must satisfy. Register custom channels via Manager.Extend.

type DatabaseNotification

type DatabaseNotification interface {
	Notification
	// ToDatabase returns the map that will be JSON-encoded into the data column.
	ToDatabase(notifiable Notifiable) map[string]any
}

type DatabaseRoutable

type DatabaseRoutable interface {
	RouteNotificationForDatabase() string
}

DatabaseRoutable is implemented by a Notifiable to provide the database channel's delivery route (the primary key persisted as NotifiableID) in a type-safe way, without matching the channel name in RouteNotificationFor.

RouteNotificationForDatabase is preferred over RouteNotificationFor, but returning "" is not an error by itself: like MailRoutable's empty-result fallback, the channel then falls back to RouteNotificationFor(ChannelDatabase). An empty result from both is an error.

type MailMessage

type MailMessage struct {
	// Subject is the email subject line. Defaults to the notification type name.
	Subject string
	// To overrides the recipient address(es). Leave empty to use
	// RouteNotificationFor(ChannelMail) / MailRoutable.
	To []string
	// From overrides the sender address. Leave empty to use the global mail.from config.
	From string
	// ReplyTo sets the Reply-To header.
	ReplyTo string
	// Content holds the plain-text and/or Html bodies — the same type
	// facades.Mail() itself uses, so nothing is lost or re-mapped between
	// a notification's mail representation and a plain Mailable's.
	Content contractsmail.Content
	// Attachments is a list of absolute file paths to attach.
	Attachments []string
	// Headers are arbitrary additional email headers.
	Headers map[string]string
}

type MailRoutable

type MailRoutable interface {
	RouteNotificationForMail(notification Notification) map[string]string
}

MailRoutable is implemented by a Notifiable to provide multiple mail recipients (address→name mapping). It is preferred over RouteNotificationFor, but an empty result is not an error by itself: the mail channel falls back to RouteNotificationFor(ChannelMail). An empty result from both is an error.

type MailableNotification

type MailableNotification interface {
	Notification
	// ToMail returns the MailMessage used to build the outgoing email.
	ToMail(notifiable Notifiable) MailMessage
}

type Manager

type Manager interface {
	Send(notifiable Notifiable, notification Notification) error

	SendNow(notifiable Notifiable, notification Notification) error

	Extend(channel Channel)

	Channel(name string) Channel

	Route(channel string, route any) OnDemandNotifiable
}

type Notifiable

type Notifiable interface {
	// RouteNotificationFor returns the delivery address for specified channel.
	// Concrete address types vary by channel implementation:
	// mail:
	//   string        single recipient address
	//   []string      multiple recipient addresses
	//   map[string]string address-name recipient mapping
	// database:
	//   string        model primary key (numeric IDs auto converted via cast.ToString)
	// Custom channels support arbitrary custom types. Unrecognized types for a channel are treated as no valid route.
	//
	// Prefer the typed alternatives for the built-in channels instead of
	// matching on the channel name constants (ChannelMail / ChannelDatabase)
	// here: implement MailRoutable (mail) or DatabaseRoutable (database)
	// and the channel uses them directly, so a typo can't silently drop a
	// route.
	RouteNotificationFor(channel string) any
}

type Notification

type Notification interface {
	Via(notifiable Notifiable) []string
}

type NotificationWithAfterSending

type NotificationWithAfterSending interface {
	Notification
	AfterSending(notifiable Notifiable, channel string) error
}

type NotificationWithBackoff

type NotificationWithBackoff interface {
	Notification
	// Backoff returns the delay before each retry attempt on channel,
	// in order; the last value repeats for subsequent attempts.
	Backoff(channel string) []time.Duration
}

NotificationWithBackoff is an optional extension for queued notifications that want to control the delay before each retry attempt. Mirrors contracts/broadcasting.ShouldBroadcastWithBackoff exactly: Backoff(channel) is called once, while the notification is still live, and returns the FULL per-attempt schedule up front — not re-invoked per retry. ShouldRetry indexes into the captured slice by attempt number, and the last value repeats for any attempt beyond the slice's length (min(attempt-1, len(backoff)-1)), matching Laravel's Worker::calculateBackoff and BroadcastJob.ShouldRetry precisely.

Backoff applies to every retry, whether capped by the notification's own Tries or the queue worker's Tries config; the last value repeats.

type NotificationWithDatabaseConnection

type NotificationWithDatabaseConnection interface {
	Notification
	// DatabaseConnection returns the connection name to use. Return ""
	// for the default connection.
	DatabaseConnection() string
}

NotificationWithDatabaseConnection is implemented by a Notification to select a non-default database connection for the database channel.

Breaking change: this interface was previously named DatabaseRoutable, which is now the notifiable-side route interface. Any code implementing the old name must be updated to NotificationWithDatabaseConnection.

type NotificationWithID

type NotificationWithID interface {
	Notification
	ID() string
}

type NotificationWithShouldSend

type NotificationWithShouldSend interface {
	Notification
	ShouldSend(notifiable Notifiable, channel string) bool
}

type NotificationWithTries

type NotificationWithTries interface {
	Notification
	// Tries returns the maximum number of attempts for the given
	// channel. 0 / not implementing this interface means no retry policy
	// is declared and the queue worker's Tries config applies.
	Tries(channel string) int
}

NotificationWithTries is an optional extension for queued notifications that want to cap retry attempts on delivery failure. Mirrors contracts/broadcasting.ShouldBroadcastWithTries exactly, for consistency across the two queued-retry mechanisms in this codebase.

type OnDemandNotifiable

type OnDemandNotifiable interface {
	Notifiable

	Route(channel string, route any) OnDemandNotifiable

	Notify(notification Notification) error

	NotifyNow(notification Notification) error
}

type ResolvableChannel

type ResolvableChannel interface {
	Channel

	// Resolve computes what Deliver will need. route is whatever
	// RouteNotificationFor returned for this channel; payload is this
	// channel's message type, JSON-marshaled.
	Resolve(notifiable Notifiable, notification Notification) (route string, payload []byte, err error)

	// Deliver sends using only the plain data Resolve produced.
	Deliver(route string, payload []byte) error
}

type ShouldQueue

type ShouldQueue interface {
	// OnQueue returns the queue name to use. Return "" for the default queue.
	OnQueue() string
	// OnConnection returns the connection name to use. Return "" for the default.
	OnConnection() string
}

Jump to

Keyboard shortcuts

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