sgeraDocs
Push notifications

Web Push

Add the service worker and embed script to receive browser push notifications.

Web Push works across Chrome, Edge, Firefox, and Safari (macOS & iOS 16.4+ home-screen web apps). VAPID keys are generated automatically when you create a Push App—no external credentials are required.

1. Configure allowed websites

In the dashboard, open your Push App → Settings and add your website origin (for example, https://example.com) to Allowed websites. Browsers require HTTPS (or http://localhost during development) for service workers.

2. Host the service worker

Create a file named msgera-sw.js at the root of your public web directory so it is served from https://your-domain.com/msgera-sw.js:

importScripts("https://api.msgera.io/push-sw.js");

3. Add the embed script

Paste the script tag before </body> on your pages, replacing YOUR_PUBLIC_ID with your app's publicId:

<script
  src="https://api.msgera.io/push.js"
  data-app-id="YOUR_PUBLIC_ID"
  data-worker="/msgera-sw.js"
  async
></script>
Script attributes
FieldTypeDescription
data-app-idrequiredstringYour push app's publicId from the dashboard.
data-workerstringPath to your service worker file. Defaults to /msgera-sw.js.
data-apistringOptional API base URL override. Defaults to the script origin or https://api.msgera.io.
data-promptstringSet to "off" to disable the built-in opt-in bar and trigger permission from your own UI.
data-prompt-textstringCustom message shown in the opt-in prompt bar.

4. JavaScript API

The script exposes window.MsgeraPush for triggering permission from a button and linking the browser subscription to a signed-in user:

// Request browser notification permission from a button click
await window.MsgeraPush.requestPermission();

// Check if the current browser is subscribed
const subscribed = window.MsgeraPush.isSubscribed();

// Associate this browser with your logged-in user ID
await window.MsgeraPush.login("user_123", userHashFromBackend);

// Unlink the user ID on sign-out
await window.MsgeraPush.logout();

Automatic token refresh

Once permission is granted, push.js refreshes the subscription once every 24 hours to keep lastSeenAt current and pick up rotated browser endpoints automatically.