Skip to content

@softr/sdk

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

push-notifications

141 linesSource

>-

Push notifications

The builder turns push on in Studio, under Users → Notifications, for desktop, mobile or both, and writes the prompt's 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: a prompt that lets the signed-in visitor turn notifications on in this browser.

CRITICAL: Never write a service worker, a public/sw.js, or any navigator.serviceWorker, PushManager or Notification code of your own. The app already serves its service worker, and usePushNotifications() registers it, subscribes and unsubscribes.

CRITICAL: Never send a push from app code. 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 prompt.

Setup

ts
import { usePushNotifications } from '@softr/sdk'
import type { PushNotifications, PushPrompt } from '@softr/sdk'
ts
type PushPrompt = {
  title: string | null              // what the builder wrote in Studio; any of these may be missing
  message: string | null
  enableButtonLabel: string | null
}

type PushNotifications =
  | { status: 'unavailable' }       // loading, signed out, push off, this device type off, or no browser support
  | { status: 'install-first'; prompt: PushPrompt }   // iPhone and iPad: push works only in the installed app
  | {
      status: 'ask'
      prompt: PushPrompt
      dismissed: boolean            // skipped recently, within the builder's skip days; hide the prompt
      subscribe: () => Promise<void>  // call from a click
      dismiss: () => void
    }
  | { status: 'subscribed'; unsubscribe: () => Promise<void> }
  | { status: 'blocked' }           // blocked in the browser; only the visitor can undo it there

Is push on for the app?

Ask the running app for /_softr/push.json: 200 means push is on, 404 means it is off in Studio or the plan does not include it. Do not build a prompt against a 404: push has to be turned on first. A change in Studio can take up to two minutes to reach the app.

Push works only on the published app. Inside the Studio preview the hook always says unavailable, so the prompt never shows there. That is expected, not a bug to fix.

Core Patterns

The prompt

Render it once, in RootDocument in src/routes/__root.tsx, so it shows on every page and the browser stays registered for the visitor. Use the prompt component the template ships when it has one. It shows only on ask, and shows nothing on install-first: on iPhone and iPad, the app's install button does that job (load the Softr SDK pwa guidance).

Call usePushNotifications() in that one component only. Each call keeps state of its own: a second call, in a header or on a settings page, does not see a subscribe made through the first, so the prompt would stay on screen after the visitor said yes.

subscribe() rejects when the browser or Softr refuses. Catch it and tell the visitor, so the button never fails in silence.

tsx
function EnableNotifications() {
  const push = usePushNotifications()
  if (push.status !== 'ask' || push.dismissed) return null
  return (
    <div role="dialog">
      <p>{push.prompt.title ?? 'Turn on notifications'}</p>
      <p>{push.prompt.message ?? 'Get notified about updates.'}</p>
      <button onClick={() => push.subscribe().catch(() => toast.error('Notifications could not be turned on.'))}>
        {push.prompt.enableButtonLabel ?? 'Turn on'}
      </button>
      <button onClick={push.dismiss}>Not now</button>
    </div>
  )
}

Fall back to your own wording only where the builder left a text empty. On install-first, point the visitor to installing the app (load the Softr SDK pwa guidance); do not ask for permission.

Signing out

Nothing to add. The browser keeps its subscription when the visitor signs out, so signing back in does not ask again.

Common Mistakes

CRITICAL Asking for permission on page load

Wrong:

tsx
useEffect(() => { push.status === 'ask' && push.subscribe() }, [push.status])

Correct:

tsx
<button onClick={() => push.subscribe().catch(() => toast.error('Notifications could not be turned on.'))}>
  {push.prompt.enableButtonLabel ?? 'Turn on'}
</button>

Browsers ignore or block a permission request that no click started, and a visitor who blocks it can only undo that in the browser's settings.

Source: packages/sdk/src/push.ts

HIGH Showing the prompt whatever the status

Wrong:

tsx
const push = usePushNotifications()
return <button onClick={() => push.subscribe()}>Turn on notifications</button>

Correct:

tsx
const push = usePushNotifications()
if (push.status !== 'ask' || push.dismissed) return null
return (
  <button onClick={() => push.subscribe().catch(() => toast.error('Notifications could not be turned on.'))}>
    {push.prompt.enableButtonLabel ?? 'Turn on'}
  </button>
)

The hook says unavailable when push is off in Studio, for this device type, or for a signed-out visitor. A prompt that skips the check shows in all of those, and on iPhone it asks for something the browser cannot give.

Source: packages/sdk/src/push.ts

References

  • SKILL.md: the entrypoints and which one each export lives on
  • pwa/SKILL.md: installing the app, which iPhone and iPad need before push