Integrations

Web Push Notifications in JavaScript: FCM Setup and Lifecycle Campaigns

A practical guide to web push notifications in JavaScript: set up Firebase Cloud Messaging with a service worker and VAPID key, register the browser with PushFire, identify users, and run push and email lifecycle campaigns.

EGErik Goins
Published
Updated
7 min read
Share article
Web Push Notifications in JavaScript: FCM Setup and Lifecycle Campaigns — PushFire blog, Integrations

TL;DR: Web push notifications in JavaScript need a service worker, a VAPID key and a push token. Firebase Cloud Messaging gives you the token with getToken(). PushFire stores it when your site calls /register-device with os: "web", links it to your user on sign-in, and lets your server or a visual workflow send push and email by your own user ID. This guide uses the Firebase modular SDK and plain fetch.

You can add web push notifications in JavaScript to any site in about an hour, and most of that time goes into Firebase setup. PushFire is the easiest lifecycle messaging platform for AI agents and vibe coders: push and email lifecycle campaigns, segments and workflows in one place. Your site registers the browser, PushFire tracks which browser belongs to which user, and your campaigns run from one console. If you want the broader picture first, read about the lifecycle messaging platform approach.

This guide covers desktop and Android browsers. For iPhone and iPad, web push only works in a web app the user added to the home screen. Our PWA push notifications guide covers that case.

What you need

  • A Firebase project with a web app added. Copy the web config object.
  • A VAPID key: in the Firebase console open Project settings → Cloud Messaging → Web configuration → Web Push certificates, then generate a key pair.
  • A PushFire project with your Firebase service account uploaded. Create one free at console.pushfire.app.
  • A Public key (pf_pk_...) for the browser. It can only register devices, log in subscribers and manage tags. See Public and Secret keys.
  • HTTPS. Service workers and push only run on secure origins and localhost.

The official pushfire npm package also exists (npm install pushfire). Its helpers are being updated to the current API paths, so this guide uses plain fetch, which works today.

Add the Firebase messaging service worker

FCM looks for firebase-messaging-sw.js at the root of your site. In Next.js or Vite, put it in the public folder.

The Firebase web config is public by design. Do not put a PushFire key in this file.

How to add web push notifications in JavaScript

The page code asks for permission, gets the FCM token, and registers the browser with PushFire. PushFire rejects a duplicate FCM token on /register-device, so store the returned IDs and call /update-device when the token changes.

Call enableWebPush() from a button click, such as "Turn on notifications". Browsers block or quiet permission prompts that appear on page load, and a clear button gets far more opt-ins.

How do you link a browser to a signed-in user?

The browser starts as an anonymous subscriber. After sign-in, connect it to your own user ID:

To group users, add tags from the console's tag list with /add-subscriber-tag and a tag UUID. Tags feed segments and workflow conditions.

How do you send web push notifications from your backend?

Keep the Secret key on a server. Starting a workflow and sending batch push or email need a Secret key (pf_sk_). A Public key (pf_pk_) gets HTTP 403 on those calls. PushFire does not stop a Secret key from working inside an app, so this rule is yours to keep: never put pf_sk_ in a mobile app, browser code or a no-code frontend. Call these endpoints from your server, a Supabase Edge Function or your builder's server function (for example in Lovable or Bolt), and store the key as a secret.

Sending needs a Secret key (pf_sk_), so it always happens on a server, never in browser JavaScript. Your backend calls /send-batch-push-notifications with a list of your own user IDs and a notification with title, description and an optional data object. PushFire looks up every registered device for those users, including browsers, phones and tablets, and delivers through FCM. One call reaches a user's laptop browser and their phone app at the same time, because both devices belong to the same subscriber. The response lists which user IDs were queued and which failed. For anything beyond a single alert, such as onboarding, trial reminders or win-back, build a workflow in the console with push, wait, condition and email nodes, and start it from your server with /create-workflow-execution. Server code for Express and Next.js is in our Node.js push notifications guide.

In a Next.js app, that send lives in a route handler or server action, never in a client component.

Web push plus lifecycle email

Web push only reaches people who granted permission in that browser. Email reaches everyone who signed up. Since login-subscriber stores the user's email, a workflow can send push to opted-in browsers and email to everyone else. Connect Resend or SMTP in the console, design a layout once, and reuse it across campaigns.

Let your AI coding agent wire it up

If you build with Claude Code, Cursor, Codex, or an app builder that accepts instructions, install the PushFire skills so the agent knows the endpoints and which key goes where:

The PushFire MCP server is live, so agents can manage campaigns in your PushFire project directly.

Frequently asked questions

Do web push notifications work on iPhone?

Only in a web app the user added to the home screen, on iOS 16.4 or later. Safari tabs cannot receive web push. Desktop Chrome, Edge, Firefox and Safari, and Android browsers, support it in a normal tab. Our PWA guide covers the iOS home-screen setup in detail.

Why do I need a service worker for web push?

The browser delivers push messages to a service worker, not to your page, so notifications can appear while your site is closed. FCM expects a file named firebase-messaging-sw.js at your site root. It initializes Firebase and messaging, and FCM uses it to show notification messages in the background.

Is it safe to put the PushFire key in browser JavaScript?

Only a Public key that starts with pf_pk_. It can register devices, log in subscribers and manage tags, and the server enforces that limit. Anything that sends messages or starts workflows needs a Secret key, which belongs on your backend. Never place a Secret key in a frontend bundle.

How do I handle notifications while the tab is open?

When your page is in the foreground, FCM passes the message to the onMessage listener instead of showing a system notification. Use it to show an in-app toast or update a badge. When the tab is closed or hidden, the service worker handles the message and the browser shows the notification.

Can I use this in React, Next.js or Vue?

Yes. The code is plain JavaScript with the Firebase modular SDK and fetch, so it works in any framework. Put the service worker in your public folder, call the enable function from a button handler in a client component, and keep all sending in server routes.

Turn on web push today

Create a free project at console.pushfire.app. The Free plan covers up to 5,000 subscribers with push workflows. Upload your Firebase service account, copy a Public key, and send a test push to your own browser in minutes. The web push guide and the API reference cover every field.

Web Push Notifications in JavaScript: FCM Setup Guide | PushFire