Notifications
Send an alert when a photo is ready or an operator needs to act. GoForj provides configured senders through internal/notifications, with a log driver for local capture and a webhook driver for delivery to a configured endpoint.
Each send runs synchronously in the caller. Use Mail for email composition. Use a durable queue when an alert needs deferred execution or retries.
Enable Notifications
Start with the App from Quickstart. Stop forj dev and add notification to the existing render.components list in .goforj.yml. Render the notification manager and its Wire providers.
forj renderUse these settings in .env to capture notifications locally.
NOTIFICATION_DRIVER=log
NOTIFICATION_SUPPORTED_DRIVERS=logThe manager owns sender configuration and transport cleanup. Your application code borrows a notification.Sender through constructor injection.
Send a Local Alert
From the Project root, create a command.
forj make:command alerts:send --no-openThe generator creates internal/alerts/send_cmd.go, adds its provider to app/wire/inject_cmd_app.go, and exposes it in app/commands.go.
Replace internal/alerts/send_cmd.go with this file. Replace example.com/photodrop in the import with your module path from go.mod.
package alerts
import (
"context"
"example.com/photodrop/internal/notifications"
"github.com/goforj/notification"
)
// SendCmd captures a sample alert through the configured sender.
type SendCmd struct {
sender notification.Sender
}
// NewSendCmd selects the app's default sender at the provider boundary.
func NewSendCmd(manager *notifications.Manager) *SendCmd {
return &SendCmd{sender: manager.Default()}
}
// Signature exposes the command through the app CLI.
func (*SendCmd) Signature() string {
return `name:"alerts:send" help:"Send a sample alert"`
}
// Run preserves command cancellation during the send.
func (c *SendCmd) Run(ctx context.Context) error {
return c.sender.Send(ctx, notification.Message{
Title: "Photo ready",
Body: "Photo 42 is ready to view.",
})
}NewSendCmd receives the manager from the app's existing providers and selects its default sender. The command retains the sender contract, so its send call stays the same when configuration selects another driver. Use the same constructor pattern in an application service when several callers share the alert workflow.
Verify Local Capture
Build to update the Wire call, then send the alert.
forj build &&
forj alerts:send
# INFO notification captured status=capturedThe log driver omits the message title and body. A successful call proves local capture. With a webhook, success means provider acceptance, which does not prove that a recipient saw the alert.
Configure Webhook Delivery
Destinations and credentials belong in deployment configuration. Follow the notification environment reference to add webhook to NOTIFICATION_SUPPORTED_DRIVERS and configure its URL, format, timeout, and optional bearer token. Run forj generate --notification and forj build after adding a supported driver or named sender.
Named senders provide separate destinations. For example, declaring NOTIFICATION_ALERTS_DRIVER creates manager.Alerts(). Select that accessor in the constructor instead of manager.Default(). Each named webhook needs its own URL.
Changing the active driver within the compiled manifest requires configuration and a restart. The manager makes no network requests during construction and closes its owned transports during app shutdown.
Test and Retry Sends
For unit tests, construct the command or service with a recording implementation of notification.Sender, then assert the title, body, and returned error. Keep tests independent of real destinations. The Notification library documents test helpers and error classifications.
The manager adds no workers, retries, fallback, or fanout. Return send errors to the caller as the command does above. For durable background delivery, call the sender from a job handler and choose an appropriate queue driver. Account for duplicate alerts when retrying jobs, especially when a request times out after the provider may have accepted it.