Step-by-step guide to adding OneSignal Web Push Notifications to your website. Supports all major browsers including Chrome, Firefox, Safari, and Edge. Learn how to configure the Web SDK, service worker, and user tracking.
Activate the web platform in your OneSignal settings
Web settings in the OneSignal dashboard
https://yourdomain.com
. Avoid using www.
if your site isnβt configured that way.
If you have multiple origins:
Localhost configuration
http://localhost
https://localhost:3000
http://127.0.0.1
https://127.0.0.1:5000
http://localhost
and http://127.0.0.1
as secure origins, allowing HTTPS integrations even on HTTP. This is why you cannot test other non-standard origins on HTTPS localhost.Local testing in the OneSignal dashboard
allowLocalhostAsSecureOrigin
to your OneSignal init
optionsallowLocalhostAsSecureOrigin: true,
to your OneSignal init
options.Additionally, if youβre testing localhost on HTTPS with a self-signed certificate, you may have to ask Chrome to ignore invalid certificates for testing with: --allow-insecure-localhost
. Firefox and Safari provide built-in mechanisms to add exceptions for security certificates.POST
certain web push events to a URL of your choosing.
Web Push Webhooks are a separate implementation from Event Webhooks and cannot be used interchangeably.
OneSignalSDKWorker.js
service worker file.
Our web SDK defaults to look for this file in the root of your site. If you want to change our service worker file location, name, and/or scope, this is where you can update those settings.
OneSignalSDKWorker.js
or if you want to rename this file. Must use .js
file extension.Service worker configuration
https://yourdomain.com/push/onesignal/OneSignalSDKWorker.js
example.com/product
) matches a tab that the browser already has open, the browser will navigate to the notificationβs URL in that tab.example.com
) matches a tab that the browser already has open, the browser will navigate to the notificationβs URL in that tab.example.com/product
) matches a tab that the browser already has open, the browser will focus on that tab, but not refresh the page.example.com
) matches a tab that the browser already has open, the browser will focus on that tab, but not refresh the page.Safari Web .p12 Push Certificate
and password.
Safari certificate configuration
OneSignalSDKWorker.js
Service Worker file to your site.
Upload service worker file step
OneSignalSDKWorker.js
file in your siteβs top-level root. Otherwise, you will need to place it in the directory you set.
OneSignalSDKWorker.js
and make it public.importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");
Verify the location
https://yourdomain.com/OneSignalSDKWorker.js
https://yourdomain.com/some-subdirectory/OneSignalSDKWorker.js
It must be publicly accessible on your origin
OneSignalSDKWorker.js
file must be publicly accessible and available on your origin. It cannot be hosted via a CDN or placed on a different origin with redirect.When you visit the URL to the file, you should see the code.It must be served with a content-type: application/javascript
<head>
tags. Our dashboard will provide the following code but includes your OneSignal app ID.
manifest.json
file properties to your site.Launch your site on a test device.
Web push native permission prompt
Check your OneSignal dashboard
Dashboard showing subscription with 'Subscribed' status
Add to Test Subscriptions.
Adding a device to Test Subscriptions
Name your subscription.
Dashboard showing the 'Name your subscription' field
Create a test users segment.
Name the segment.
Test Users
(the name is important because it will be used later).Add the Test Users filter and click Create Segment.
Creating a 'Test Users' segment with the Test Users filter
Get your App API Key and App ID.
Update the provided code.
YOUR_APP_API_KEY
and YOUR_APP_ID
in the code below with your actual keys. This code uses the Test Users
segment we created earlier.Run the code.
Check images and confirmed delivery.
Expanded push notification with image on Chrome macOS
Check for confirmed delivery.
Delivery stats showing confirmed delivery
Confirmed delivery at the device level in Audience Activity
support@onesignal.com
with the following information:.txt
filelogin
method each time they are identified by your app.
username
, role
, or preferences) and events (like purchase_date
, game_level
, or user interactions). Tags power advanced Message Personalization and Segmentation allowing for more advanced use cases.
Set tags with our SDK addTag
and addTags
methods as events occur in your app.
In this example, the user reached level 6 identifiable by the tag called current_level
set to a value of 6
.
A user profile in OneSignal with a tag called "current_level" set to "6"
Segment editor showing a segment targeting users with a current_level value of greater than 4 and less than 10
Screenshot showing a push notification targeting the Level 5-10 segment with a personalized message
addEmail
method to create email subscriptions.addSms
method to create SMS subscriptions.A user profile with push, email, and SMS subscriptions unified by External ID
setConsentRequired(true)
: Prevents data collection until consent is given.setConsentGiven(true)
: Enables data collection once consent is granted.support@onesignal.com
Please include: