Custom Code Setup
OneSignal Web Push Setup Guide for websites. Works with all Web Push Supported Browsers (Chrome, Firefox, Safari, and more!)
For Advanced Users Only
You should only use custom code if you absolutely need it. Otherwise, we recommend using the Web Push Typical Setup or if using WordPress, see our WordPress Setup.
If you haven't already, sign up for a free account on onesignal.com.
If this is not your first app with OneSignal, click New App/Website. Otherwise, you will see the next page.
Name your app something similar to your site, then select Web from the platform list. Click Next: Configure Your Platform.
Note: If you want to set up more platforms, you will be able to do so later within the app's settings page.
1. Choose Integration
Select Custom Code. This will enable you to configure the prompts and other settings programmatically with our Javascript SDK.
If you are using our WordPress plugin, see our WordPress Setup Guide.
2. Site Setup
Input your default site information.
Field | Description |
---|---|
SITE NAME | The default name for your site. Shows in push notifications. |
SITE URL | Your website URL (e.g. https://example.com , https://www.example.com ).Must include: - https:// - If your site uses www make sure to include it.To test locally, see Local Testing Setup |
AUTO RESUBSCRIBE (HTTPS ONLY) | Recommended: Allows push subscribers to automatically resubscribe upon returning to your site without being prompted if they clear their browser data or if you are transferring to OneSignal from another push provider. |
DEFAULT ICON URL | Icon used for Prompting and the default for push notifications. Enter an https icon URL or upload file that is square 256x256 pixels.The file must be .png , .jpg , or .gif . |
3. Advanced Push Settings
Safari - Custom Certificate (Optional)
OneSignal automatically provides the necessary certificates to work with Safari Browsers at no additional cost. If you already have your own Safari Web Push Certificates, toggle on this option to upload your Safari Web .p12 Push Certificate
and password.
Click Save to continue.
4. Upload Files
Download OneSignal SDK file which is the Service Worker file. You must upload this file to your server.
Option 1. Create file and copy code
-
Create a new file
OneSignalSDKWorker.js
and make it public. -
Copy-paste the following import statement into the file:
importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");
Option 2. Download zip folder and upload
-
Click Download OneSignal SDK files (you can also download the file here).
-
Unzip the download. There should be a single file you need to host.
Hosting the file
The OneSignal SDK file must be publicly accessible and can be placed at the top-level root of your site. The OneSignal SDK will look for the file at https://yoursite.com/OneSignalSDKWorker.js
. If you want to place them in a subfolder, you can! Please see our OneSignal Service Worker Guide for instructions.
Subfolders or Migrating from another Push Provider
If you want to place the
OneSignalSDKWorker.js
file in a subfolder or if you are migrating to OneSignal from another provider, please see our OneSignal Service Worker Guide for instructions.
Note regarding Service Worker file(s)
If you are revisiting this page for an existing site set up prior to November 22, 2021, and are currently using two service worker files:
OneSignalSDKWorker.js
OneSignalSDKUpdaterWorker.js
you should continue hosting both files. This is to prevent the Service Worker from going stale if a user doesn't return to your site in some time.
On November 22, 2021 the OneSignal Web SDK was updated to require only a single service worker file. New setups will now only require a single file.
5. Add Code to Site
To enable OneSignal on your site, you will need to paste the provided code into your website's <head>
. If you do not have access to modify your site's code, you will need to contact a developer to help you.
The OneSignal JavaScript SDK code should work on any site, but if your site is setup using Angular, React JS, or Vue JS then follow these links.
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(function(OneSignal) {
OneSignal.init({
appId: "YOUR_ONESIGNAL_APP_ID",
safari_web_id: "YOUR_SAFARI_WEB_ID",
notifyButton: {
enable: true,
},
});
});
</script>
To enable OneSignal on your site, you will need to write code to initialize the OneSignal SDK and setup Prompting. You may also add other code as you wish.
6. Subscribe and Send Yourself a Notification
Go to your website. If you copied the code provided, then you should see the OneSignal Bell Prompt. Click it to see the native browser prompt and subscribe to push. Then Find & Set Test Subscriptions and send yourself a Push Notification.
Setup Complete!
If you are having difficulty, please check Web Push Troubleshooting or contact [email protected] for assistance.
Updated about 1 year ago