runtime
Package runtime is the Go API of the Drift notifications plugin (github.com/go-drift/drift/plugins/notifications/plugin in drift.yaml): local notifications the app schedules, and taps on them.
import notifications "github.com/go-drift/drift/plugins/notifications/runtime"
go func() {
err := notifications.Schedule(ctx, notifications.Request{
ID: "reminder",
Title: "Stand up",
At: time.Now().Add(time.Hour),
Data: map[string]any{"route": "/reminders"},
})
...
}()
notifications.Opens().Listen(platform.StreamHandler[notifications.Open]{OnEvent: func(o notifications.Open) {
drift.Dispatch(func() { navigate(o.Data["route"]) })
}})
Schedule and CurrentSettings wait for the platform, so call them from a goroutine; from the UI thread they fail with platform.ErrBlocksUIThread. Showing notifications needs the user's permission: platform.Notifications.Permission. Notifications are shown in the foreground too, and reported on Deliveries. Only this plugin's notifications reach Deliveries and Opens; push notifications come from the Firebase plugin.
func Cancel
func Cancel(id string) error
Cancel removes the notification with id, scheduled or shown. Any goroutine may call it.
func CancelAll
func CancelAll() error
CancelAll removes every notification this plugin scheduled or shows, leaving other plugins' (push notifications) alone. Any goroutine may call it.
func Deliveries
func Deliveries() *platform.Stream[Notification]
Deliveries streams this plugin's notifications as they are shown while the app runs.
func Opens
func Opens() *platform.Stream[Open]
Opens streams taps on this plugin's notifications, including the one that launched the app. Taps while no listener is subscribed are queued for the next one, so subscribe once the app can act on them.
func Schedule
func Schedule(ctx context.Context, req Request) error
Schedule schedules req, replacing any notification with the same ID. It waits for the platform to accept the request, so call it from a goroutine; from the UI thread it fails with [platform.ErrBlocksUIThread]. A request the platform rejects fails with the platform's error.
func SetBadge
func SetBadge(count int) error
SetBadge sets the app icon badge count (iOS; Android launchers show their own badges). Any goroutine may call it.
type Notification
Notification is a notification this plugin showed.
type Notification struct {
ID string
Title string
Body string
Data map[string]any
// Foreground reports whether the app was in the foreground.
Foreground bool
}
type Open
Open is the user tapping one of this plugin's notifications.
type Open struct {
ID string
Data map[string]any
}
type Request
Request describes a notification to schedule.
type Request struct {
// ID identifies the notification; scheduling the same ID again
// replaces it, and Cancel takes it.
ID string
// Title and Body are the notification's text.
Title string
Body string
// Data is delivered with the notification, to Deliveries and Opens.
// Values must be JSON-like: strings, numbers, booleans, lists, maps.
Data map[string]any
// At is when to show the notification. Zero means after Interval, or
// at once.
At time.Time
// Interval delays the first notification when At is zero, and is the
// period when Repeats is set.
Interval time.Duration
// Repeats shows the notification every Interval, from now. It needs an
// Interval of at least a minute, and cannot be combined with At.
Repeats bool
// ChannelID is the Android notification channel, created on first use
// with the ID as its name. Empty uses the plugin's default channel.
// Ignored on iOS.
ChannelID string
// Sound is an iOS sound file name in the app bundle; empty plays the
// default sound. Android uses the channel's sound.
Sound string
// Badge, when set, is the app icon badge count to show with the
// notification (iOS).
Badge *int
}
type Settings
Settings are the user's notification settings for the app. The permission itself is platform.Notifications.Permission.
type Settings struct {
AlertsEnabled bool
SoundsEnabled bool
BadgesEnabled bool
}
func CurrentSettings
func CurrentSettings(ctx context.Context) (Settings, error)
CurrentSettings returns the user's notification settings. It waits for the platform, so call it from a goroutine.
Generated by gomarkdoc