Web Push

Web Push Notifications

Send push notifications from Bento to visitors on your website in Chrome, Edge, Firefox and Safari, including iPhone and iPad web apps on the Home Screen. Bento creates the keys, so you don't need an account with any other service.

Read the push notifications overview for how campaigns, automations and events fit together. Building a native app instead? See the iOS guide or the Android and Firebase guide.

Before you start

Check you have everything on this list:

  • Bento Chat on your plan. Push runs on Bento Chat, a $30/month add-on.
  • An approved Bento account.
  • Administrator access. Only administrators can open Push in Bento.
  • A site served over https. http://localhost works while you build.
  • A way to add files at the root of your site, next to your home page.
  • Bento.js on the pages where visitors turn on notifications. See the Bento.js docs.

In Bento, go to Marketing > Push > Apps and choose Set up push. The setup guide for a website has five steps: Platform (pick Website), Prepare your site, Add your website, Connect your site, and Send a test. This page covers the same steps in more detail.

The in-app setup guide (Marketing > Push > Apps > Set up push) shows every snippet on this page with your site UUID and Bento.js script already filled in. Copy from there to skip replacing YOUR_SITE_UUID and https://app.bentonow.com/YOUR_SCRIPT.js by hand.

Step 1: Add your website in Bento

  1. Go to Marketing > Push > Apps and choose Set up push.
  2. Pick Website.
  3. Choose Add website keys.
  4. In the dialog, fill in:
    • Name: a label for this website. Only your team sees it.
    • Site address (optional): see below.
    • Contact for push services: an email, or an https page on your site. Browser push services use it if there's a problem with your notifications.
    • Keys: leave Create new keys selected (the default). Choose Import existing keys only if you're moving from another provider (see Moving from another push provider).
  5. Click Add website.

Leave Site address empty to use the keys on any of your site's domains: its address in Settings > General, its subdomains, and other domains set up for tracking. www. makes no difference. To give one address its own keys, such as https://shop.acme.com, enter it there.

Your website now appears in the Apps list. Its status shows Working, Not checked, or Not working.

Step 2: Add Bento.js and the push helper

Add both scripts to every page, before </body>. Skip the first line if Bento.js is already on your site.

HTML
<script src="https://app.bentonow.com/YOUR_SCRIPT.js" defer></script>
<script src="https://app.bentonow.com/push/bento-push.js?site_uuid=YOUR_SITE_UUID" defer></script>
<script>
  window.addEventListener('bento:ready', function () {
    bento.view();
  });
</script>

The push helper adds bentoPush to the page (and bento.push when Bento.js is loaded) with isSupported(), subscribe(), unsubscribe() and isSubscribed().

Bento links each browser to the visitor Bento.js knows, so subscribing only works on pages that load Bento.js. To target someone by email, and to send yourself a test, Bento needs to know who the visitor is. Call bento.identify(email), or have them sign in or submit a form.

JavaScript
bento.identify('user@example.com');

Step 3: Add the service worker

Browsers deliver notifications to a service worker on your own domain. Create /sw.js at the root of your site with one line:

JavaScript
importScripts('https://app.bentonow.com/push/bento-sw.js?site_uuid=YOUR_SITE_UUID');

It shows Bento notifications, reports opens when someone clicks, and opens the notification's link or the button's link. Pushes from other services are left to your own handlers.

Already have a service worker? Add the line to the top of it, and pass its path when you subscribe:

JavaScript
bentoPush.subscribe({ serviceWorkerPath: '/service-worker.js' });

A worker in a subfolder only covers pages in that folder, so keep it at the root.

Step 4: Ask for permission at the right moment

Browsers block prompts that appear on page load, and stop showing them after a few dismissals. A visitor who clicks Block can't be asked again. Show your own short prompt after someone does something that makes notifications useful, and ask the browser only when they say yes.

HTML
<div id="push-prompt" hidden>
  <p>Want a notification when your order ships?</p>
  <button type="button" id="push-yes">Turn on notifications</button>
  <button type="button" id="push-no">Not now</button>
</div>
JavaScript
const prompt = document.getElementById('push-prompt');
const ASK_AGAIN_AFTER = 30 * 24 * 60 * 60 * 1000;

// Call this right after a useful moment, such as placing an order.
async function offerNotifications() {
  if (!window.bentoPush || !bentoPush.isSupported() || Notification.permission !== 'default') return;
  if (await bentoPush.isSubscribed()) return;
  if (Number(localStorage.getItem('push_prompt_dismissed_at')) > Date.now() - ASK_AGAIN_AFTER) return;
  prompt.hidden = false;
}

document.getElementById('push-no').addEventListener('click', () => {
  localStorage.setItem('push_prompt_dismissed_at', String(Date.now()));
  prompt.hidden = true;
});

// Safari only shows the browser's prompt from a click, so subscribe inside the click handler.
document.getElementById('push-yes').addEventListener('click', async () => {
  prompt.hidden = true;
  try {
    await bentoPush.subscribe();
  } catch (error) {
    console.warn('Notifications stayed off:', error);
  }
});

When someone subscribes, Bento records a $push_subscribed event on the subscriber. Clicking a notification records $push_opened, and turning notifications off records $push_unsubscribed. Use these events in segments and automations.

Step 5: Let people turn notifications off

Add this to a notification setting, and call it when someone signs out on a shared computer.

JavaScript
async function turnOffNotifications() {
  if (window.bentoPush && (await bentoPush.isSubscribed())) await bentoPush.unsubscribe();
}

Step 6: Support iPhone and iPad

iOS and iPadOS 16.4 and later show web notifications only for sites added to the Home Screen. Add a web app manifest so Share > Add to Home Screen opens your site as an app:

JSON
{
  "name": "Acme",
  "short_name": "Acme",
  "start_url": "/",
  "display": "standalone",
  "icons": [
    { "src": "/icon-192.png", "sizes": "192x192", "type": "image/png" },
    { "src": "/icon-512.png", "sizes": "512x512", "type": "image/png" }
  ]
}

Link it from every page:

HTML
<link rel="manifest" href="/manifest.json">

Then show your prompt inside the Home Screen app. Safari in a normal tab on iPhone can't subscribe.

Step 7: Check it works

  1. Open your site in a browser, sign in or call bento.identify() with your email, and turn on notifications.
  2. In Bento, send a test from the Send a test step of the setup guide, or from Send a test in any push campaign.
  3. Confirm the notification arrives, and that clicking it opens the right page.

The test finds the subscriber by email, so that browser needs to be signed in, or to have called bento.identify().

Each browser keeps its own subscription, so turn on notifications in every browser you want to test.

BrowserWhat to check
ChromeOn a Mac, also allow Chrome in System Settings > Notifications.
EdgeOn Windows, Focus assist or Do not disturb can hide notifications.
FirefoxFirefox only shows the prompt after a click.
Safari on MacSafari 16 or later on macOS 13 or later. Images don't show.
iPhone and iPadiOS 16.4 or later, opened from the Home Screen icon.

Moving from another push provider

Browser subscriptions only work with the keys they were created with. To keep your current subscribers, choose Import existing keys when you add your website and paste your existing VAPID public and private keys (base64url). Then send your existing subscriptions to Bento from your server (see Register subscriptions from your server).

Otherwise Bento creates new keys.

Replacing the keys later stops notifications for everyone who's subscribed, and they need to turn them on again.

Register subscriptions from your server

If your server already stores browser subscriptions, for example from another provider, send them to Bento with your secret key. Bento adds the subscriber if the email is new. See the Push API reference for every field.

Shell
curl -X POST "https://app.bentonow.com/api/v1/push/devices?site_uuid=YOUR_SITE_UUID" \
  -u "YOUR_PUBLISHABLE_KEY:YOUR_SECRET_KEY" \
  -H "User-Agent: MyApp/1.0" \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","credential_id":12,"platform":"web","permission":"granted","browser":"chrome","subscription":{"endpoint":"https://fcm.googleapis.com/fcm/send/…","keys":{"p256dh":"B…","auth":"…"}}}'

To remove one, send DELETE to the same path with credential_id and endpoint in the query string.

Troubleshooting

ProblemCauseFix
Notifications are blockedThe visitor clicked Block, or dismissed the prompt several times.They can allow notifications from the lock icon next to your address. Use your own prompt first so fewer people block you.
403 "Push subscriptions must come from your website."The page isn't on your site's address, a subdomain, or another domain set up for tracking. Staging and preview domains are the usual cause.Subscribe from your live site, or set that domain up for tracking. A website with its own address must match the page exactly.
422 "Load Bento.js on this page before subscribing to push."The page subscribed before Bento.js knew the visitor.Add Bento.js and call bentoPush.subscribe() after bento:ready.
The service worker misses some pagessw.js is in a subfolder or on another domain.Serve sw.js from the root of the same domain, or pass serviceWorkerPath.
bentoPush.isSupported() is falseThe page isn't https, it's a private window, or the browser is too old.Use https (or http://localhost), a normal window and a supported browser.
Safari doesn't ask or show anythingSafari needs a click, macOS 13 with Safari 16, or a Home Screen web app on iOS 16.4 and later.Subscribe inside a click handler, add a manifest, and test from the Home Screen on iOS.
Browsers stopped getting notifications after new keysSubscriptions only work with the keys they were created with.Visitors need to turn notifications on again. Import your current keys when moving providers.
Subscription expiredThe visitor cleared browser data or turned notifications off.Bento stops sending to that browser. It subscribes again next time they turn notifications on.
The browser's push service isn't supportedA few browsers send through services Bento can't deliver to.Use Chrome, Edge, Firefox or Safari.

Learn more