Skip to main content
Use the Sendrealm React SDK to receive Web Push notifications in React, Vite, and Next.js apps. This SDK runs in the browser and uses a Sendrealm app ID. It does not use a Sendrealm API key.
Never put a Sendrealm API key in browser JavaScript. Use @sendrealm/react for browser push registration and @sendrealm/sdk from trusted backend code to send notifications.

Requirements

  • React 18 or newer.
  • A Sendrealm Push App with the Web provider active.
  • HTTPS in production.
  • The Sendrealm service worker served from the same origin as your app.
  • A notification permission prompt triggered from a user action.
iOS Web Push only works for installed Home Screen web apps with a valid web app manifest.

Install

Copy the service worker into your public root:
Web Push service workers must be served from the same origin as the page. The default SDK path is /sendrealm-service-worker.js with scope /. If you prefer to copy it manually:

Initialize In React Or Vite

Call init() from browser code:

Initialize In Next.js

Put initialization in a Client Component:
Render <SendrealmInit /> once near your app shell, and put sendrealm-service-worker.js in public/.

Initialization Options

Service Worker Diagnostics

The SDK checks the configured service worker path before subscribing the browser. Diagnostics include a serviceWorkerCheck object that reports common setup issues such as missing files, HTML fallbacks, cross-origin worker URLs, or an old worker file after an SDK upgrade.
Run the setup command again after package upgrades:
You cannot register a GitHub or CDN URL directly as the Web Push service worker. Browser service-worker registration requires the worker script to be served from your application origin. You can use GitHub Releases as a download source for the file, but deploy it into your app’s public directory.

Permission And Subscription UI

Browsers expect notification prompts to happen after a user action. Show your own explanation first, then call optIn() from a button click.
Call logout when the user signs out:

Tags And Events

Use tags for client-observed preferences, state, and behavior:
Track app events:
Use backend-owned contact properties for authoritative account, billing, compliance, and verified profile data.

Notification Events

Use listeners when your app needs to react to notification opens or actions:
Read the notification that opened the page:

Send To Web Devices

Once a browser is registered, send web push from trusted backend code with the JavaScript SDK:

Diagnostics

Use diagnostics to confirm browser support, device ID, permission status, subscription state, service worker path, and the latest SDK error.

Hooks

Troubleshooting