Illustration of a web push click URL, a cursor clicking a notification that opens a specific web page Illustration of a web push click URL, a cursor clicking a notification that opens a specific web page

How to Open a Specific Page When Someone Clicks a Web Push Notification

Set the web push click URL with the data field and a notificationclick handler. Code to focus or open tabs and route action buttons.

To open a specific page when someone clicks a web push notification, put the URL in the notification’s data field, then handle the notificationclick event in your service worker. Close the notification, check for an open tab with clients.matchAll(), and focus it, or call clients.openWindow(url) to open a new tab.

A notification that opens the homepage instead of the article it promised loses the click. I see this a lot on custom web push setups, usually because the click handler was copied from a tutorial and never passed the link through. Here’s how the web push click URL works, the code that handles it properly, and how to send people to different pages from action buttons.

Key facts at a glance:

  • Clicks are handled in the service worker, not on the page, through the notificationclick event.
  • The browser doesn’t open a link for you. Unless you call clients.openWindow(), a click just closes the notification (or does nothing).
  • Pass the URL in data, because the Notification object has no built-in link property in the classic Push API.
  • Up to two action buttons can each open a different page in Chrome and Edge; event.action tells you which one was clicked.
  • Safari’s Declarative Web Push (iOS and iPadOS 18.4 and later) adds a navigate URL in the payload itself, described on the WebKit blog.

How does a web push click open a page?

A click on a web push notification fires the notificationclick event in the service worker that showed it. Your code reads the target URL from the notification and decides whether to open a new tab or reuse an existing one. Nothing opens automatically.

The full path looks like this:

  1. Your server sends a push message whose payload includes a URL.
  2. The service worker’s push handler calls showNotification() and stores the URL in data.
  3. The subscriber clicks the notification or an action button.
  4. The notificationclick handler closes the notification and opens or focuses the right page.

The MDN notificationclick reference and the web.dev guide to notification behaviour both document this pattern.

How do you pass the URL in the notification?

Send the URL in your push payload, then copy it into the data option when you show the notification. The data value is stored with the notification, so the click handler can read it later, even if the service worker restarted in between.

self.addEventListener('push', (event) => {
  const msg = event.data ? event.data.json() : {};
  event.waitUntil(
    self.registration.showNotification(msg.title || 'New post', {
      body: msg.body,
      icon: msg.icon,
      data: { url: msg.url || '/' },
      actions: [
        { action: 'read', title: 'Read now' },
        { action: 'deals', title: 'See deals' }
      ]
    })
  );
});

Keep the URL absolute, for example https://example.com/blog/new-post/. It’s also the place to add UTM parameters, so the visit shows up correctly in analytics. My post on UTM parameters on web push links explains the naming I use.

How do you handle the notificationclick event?

Handle notificationclick by closing the notification, then focusing an existing tab with the same URL or opening a new one. Wrap the work in event.waitUntil() so the browser keeps the service worker alive until the tab opens.

self.addEventListener('notificationclick', (event) => {
  event.notification.close();

  const base = event.notification.data?.url || '/';
  const target = event.action === 'deals' ? '/deals/' : base;
  const url = new URL(target, self.location.origin).href;

  event.waitUntil((async () => {
    const tabs = await clients.matchAll({ type: 'window', includeUncontrolled: true });
    for (const tab of tabs) {
      if (tab.url === url && 'focus' in tab) return tab.focus();
    }
    return clients.openWindow(url);
  })());
});

Three details matter here. First, clients.openWindow() only works inside the click event, because browsers allow new windows only after a user gesture. Second, includeUncontrolled: true finds tabs that your service worker doesn’t control yet. Third, comparing full URLs avoids focusing the homepage when the subscriber expected an article.

Should you open a new tab or focus an existing one?

Focus an existing tab when the subscriber already has the exact page open, and open a new tab otherwise. That avoids duplicate tabs for the same page, which feels sloppy. For chat or dashboard apps, you may prefer to focus any open tab of your site and navigate it.

Click behaviourBest forMethod
Open a new tabBlog posts, product pages, offersclients.openWindow(url)
Focus the tab if that page is openAny site; avoids duplicatesclient.focus() after clients.matchAll()
Focus any site tab and change its pageWeb apps, inboxes, dashboardsclient.focus() then client.navigate(url)
Do nothing except closePure information alertsevent.notification.close() only

Note that client.navigate() only works on tabs your service worker controls. If it fails, fall back to openWindow().

How do action buttons open different pages?

Each action button has an action ID, and the click handler reads event.action to see which one was clicked. An empty value means the subscriber clicked the notification body. You then map each ID to its own URL, as in the code above.

Chrome and Edge show up to two action buttons on most desktop systems; Notification.maxActions reports the limit at runtime. Safari and Firefox on desktop don’t show web push action buttons, so the notification body link must always work on its own. My post on push notification CTA buttons covers button copy and support in more detail.

Why does clicking a web push notification open the wrong page?

Usually it’s because the URL never made it into data, the handler hard-codes a default, or openWindow() ran outside event.waitUntil(). Old service worker code can also linger after you deploy a fix.

Check these in order:

  1. Log the payload in the push handler and confirm the URL is there.
  2. Confirm data.url is set in showNotification().
  3. Make sure the click handler reads event.notification.data, not a global variable.
  4. Wrap the tab logic in event.waitUntil().
  5. Update the service worker and test again; my guide to debugging web push in Chrome DevTools shows how to force the new version.

Where LaraPush fits

If you’d rather not write click handlers, a push panel does this for you. LaraPush is a self-hosted web push panel that you run on your own VPS, sold as a one-time licence from $499 (check LaraPush pricing). Each campaign has a link field, plus up to two CTA buttons with separate click tracking, and Quick Push fetches the page metadata from a URL you paste. The features page lists the rest. The trade-offs are no free plan and a server you maintain. For other tools, see my roundup of the best push notification services and tools.

Frequently asked questions

Why does nothing happen when I click my web push notification?

Your service worker probably has no notificationclick handler, or the handler doesn’t call clients.openWindow(). Browsers don’t open a link automatically for classic web push. Add a click handler that closes the notification and opens the URL stored in its data field, wrapped in event.waitUntil().

Can a web push notification open a URL on another domain?

Yes. clients.openWindow() can open a cross-origin URL, such as an affiliate or partner page, as long as it runs inside the click event. However, you can’t focus or navigate tabs on another origin, so a new tab opens every time.

Can I open the page without a new tab?

Yes, if a tab of your site is already open. Use clients.matchAll() to find it, call focus(), then navigate() to change its page. navigate() only works on tabs your service worker controls, so keep openWindow() as a fallback.

How do I track clicks on web push notifications?

Add UTM parameters to the URL in the payload so analytics tools attribute the visit to push. For counts at the notification level, send a request to your own server from the notificationclick handler, or use a push panel that reports clicks per campaign and per button.

Do action buttons work on iPhone web push?

Safari on iPhone and iPad doesn’t show web push action buttons, so only the notification body is clickable. Always make the main notification link lead somewhere useful. On Chrome and Edge for desktop and Android, up to two buttons are typically available.

Leave a Reply

Your email address will not be published. Required fields are marked *