Docs / Websites

Web SDKAvailable

Browser notifications with one script tag: subscription, friendly permission prompt, events and an offline queue.

Before you start

  1. In the dashboard, under Channels and credentials → Web, generate the VAPID keys.
  2. Authorize your site's domain (e.g. https://shop.com) and verify it with the file under /.well-known/ or a DNS TXT record. localhost is verified automatically.
  3. Copy the app's public key (vp_pub_…).
Until the domain is verified, VivaPush refuses browser calls and the SDK logs the reason in the console.

1. Publish the service worker

Browsers only deliver push to a service worker on the same domain. Download /sdk/vivapush-sw.js and serve it from your site's root, as https://your-site/vivapush-sw.js.

If the site already has a service worker (PWA, Workbox, Firebase), don't add another: import VivaPush's into yours and pass its path at initialization.

sw.js
// in your service worker
importScripts('/vivapush-sw.js');

2. Add the script

HTML
<script src="https://vivapush.com/sdk/vivapush.js"></script>
<script>
  VivaPush.initialize("vp_pub_YOUR_KEY");
  VivaPush.showSoftPrompt();
</script>

The soft prompt explains first and opens the browser prompt only when the person clicks Allow: browsers penalize prompts without a user gesture, and Safari requires the click.

3. Users and attributes

JavaScript
VivaPush.login("customer-42");             // after sign-in on your site
VivaPush.setAttributes({ plan: "premium", points: 1240 });
VivaPush.removeAttributes(["plan"]);
VivaPush.setMarketingOptIn(false);         // no marketing
VivaPush.logout();

Everything can be called before permission: the SDK stores it and sends it with the first registration.

4. Clicks and notifications while the page is open

JavaScript
VivaPush.onNotificationOpened((n) => console.log(n.title, n.url, n.data));
VivaPush.onForegroundNotification((n) => showToast(n.title));
const opened = await VivaPush.initialNotification(); // the click that opened this tab
VivaPush.setUrlHandler((url) => router.push(new URL(url).pathname));

Options

OptionDefaultFor
baseUrlhttps://vivapush.comYour VivaPush address
serviceWorkerPath/vivapush-sw.jsPath of your service worker
autoPromptfalseShows the soft prompt on its own ({ type: 'slidedown' | 'bell', delayMs })
showInForegroundtrueShows notifications while the page is focused
logLevelerrornone, error, info or debug

Accepted, but nothing appeared

If the send shows the provider accepted it and nothing appeared on screen, the operating system is almost always holding the notification:

  1. macOS: System Settings → Notifications → (your browser, e.g. Google Chrome): turn on Allow notifications and pick the Banners or Alerts style.
  2. macOS: check that no Focus (Do Not Disturb) is on in Control Center.
  3. Windows: Settings → System → Notifications: turn notifications on for your browser and turn off Do not disturb.
  4. In the browser, check that the site may show notifications (icon next to the address).