@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
152 lines (117 loc) • 5.77 kB
Markdown
---
name: in-app-notifications
description: >-
Show signed-in visitors their in-app notifications: useNotifications for the
inbox, its unread count, paging, the unread-only list, marking read and
unread, and the Studio texts; the InAppNotifications, InAppNotification and
NotificationTexts types; and how to check that in-app notifications are on.
Notifications are sent by Softr Workflows, never by app code.
metadata:
type: sub-skill
library: softr-sdk
library_version: 1.23.0
---
The builder turns in-app notifications on in Studio, under Users → Notifications, and writes the inbox texts there. Notifications are sent by a Softr Workflow's "Send notification" action, which the builder sets up in Studio. What the app adds is one thing: an inbox, a bell with an unread badge that opens the visitor's notifications.
> **CRITICAL**: Never send a notification from app code, and never store notifications in a table of the app's own. Sending is a Softr Workflow's "Send notification" action. When the builder asks for "notify X when Y", point them to that workflow; when Y is something the app does, the app triggers the workflow (load the Softr SDK `workflows` guidance). The app itself adds only the inbox.
>
> **CRITICAL**: Never call the platform's notification API yourself. `useNotifications()` reads and changes the visitor's notifications through the app's own server.
## Setup
```ts
import { useNotifications } from '@softr/sdk'
import type { InAppNotification, InAppNotifications, NotificationTexts } from '@softr/sdk'
```
```ts
type NotificationTexts = {
buttonLabel: string | null // what the builder wrote in Studio; any of these may be missing
allTabName: string | null
allTabEmptyMessage: string | null
unreadTabName: string | null
unreadTabEmptyMessage: string | null
markAllAsReadLabel: string | null
markAsReadLabel: string | null
markAsUnreadLabel: string | null
}
type InAppNotification = {
id: string
title: string | null
description: string | null
link: string | null // a path in the app, or a full URL
read: boolean
createdAt: number | null // milliseconds since the epoch
}
type InAppNotifications =
| { status: 'unavailable' } // loading, signed out, or in-app notifications off
| {
status: 'ready'
texts: NotificationTexts
defaultIcon: string | null // the app's logo, for every notification card
notifications: InAppNotification[] // loaded so far, newest first
unreadCount: number
unreadOnly: boolean
showUnreadOnly: (unreadOnly: boolean) => void
hasMore: boolean
loadMore: () => Promise<void>
markRead: (id: string) => Promise<void>
markUnread: (id: string) => Promise<void>
markAllRead: () => Promise<void>
}
```
Ask the running app for `/_softr/notifications.json`: `200` means they are on, `404` means they are off in Studio or the plan does not include them. Do not build an inbox against a `404`: in-app notifications have to be turned on first. A change in Studio can take up to two minutes to reach the app.
Render it once, where the visitor always sees it: in the app's navigation, next to the signed-in visitor's menu. Use the inbox component the template ships when it has one. It shows nothing unless the status is `ready`.
Any other component can call `useNotifications()` too, such as a notifications page or a badge in a mobile header. Every call reads the same list and unread count, and the page polls once, so marking an item read in one place updates the others.
```tsx
function NotificationInbox() {
const inbox = useNotifications()
if (inbox.status !== 'ready') return null
return (
<Popover>
<PopoverTrigger aria-label={inbox.texts.buttonLabel ?? 'Notifications'}>
<BellIcon />
{inbox.unreadCount > 0 && <Badge>{inbox.unreadCount}</Badge>}
</PopoverTrigger>
<PopoverContent>
{inbox.notifications.map((notification) => (
<a key={notification.id} href={notification.link ?? undefined} onClick={() => inbox.markRead(notification.id)}>
{notification.title}
</a>
))}
</PopoverContent>
</Popover>
)
}
```
Fall back to your own wording only where the builder left a text empty. Show `allTabEmptyMessage`, or `unreadTabEmptyMessage` while `unreadOnly`, when the list is empty.
The mark functions reject when Softr refuses. Catch them and tell the visitor, so a click never fails in silence.
Wrong:
```ts
await createRecord({ table: 'Notifications', fields: { user, message } })
```
Correct:
```tsx
const inbox = useNotifications()
```
Softr stores the notifications a Workflow sends and knows who each one is for. A table of the app's own misses every one of them, and leaks one visitor's notifications to another unless its rules are right.
Source: packages/sdk/src/notifications.ts
Wrong:
```tsx
const inbox = useNotifications()
return <BellIcon />
```
Correct:
```tsx
const inbox = useNotifications()
if (inbox.status !== 'ready') return null
return <BellIcon />
```
The hook says `unavailable` when in-app notifications are off in Studio or the visitor is signed out. A bell that skips the check shows an inbox that never fills.
Source: packages/sdk/src/notifications.ts
- [SKILL.md](../SKILL.md): the entrypoints and which one each export lives on
- [push-notifications/SKILL.md](../push-notifications/SKILL.md): push notifications, sent by the same Workflow action