Custom Code Setup
Complete guide for setting up OneSignal Web Push notifications using custom code integration. Configure JavaScript SDK, service workers, and Safari certificates for Chrome, Firefox, Safari, and other web browsers.
Only use this Custom Code setup if you need advanced configuration or programmatic control. For most users, we recommend the:
Prerequisites
- A OneSignal account
- A website with HTTPS (required for web push notifications)
- Access to modify your website’s HTML and upload files to your server
- Basic understanding of JavaScript (helpful but not required)
Creating Your OneSignal App
If this is not your first app with OneSignal, click New App/Website. Otherwise, you’ll proceed directly to the setup.
OneSignal dashboard showing the initial app creation screen
Name your app something descriptive (matching your website name is recommended), then select Web from the platform list. Click Next: Configure Your Platform.
Note: You can add additional platforms (iOS, Android, etc.) later in your app’s settings.
Platform selection screen showing Web option highlighted
1. Choose Integration
Select Custom Code. This enables full programmatic control over prompts, timing, and other settings using our JavaScript SDK.
When to choose Custom Code:
- Need to customize notification prompts
- Want to control when users are prompted
- Require advanced segmentation or targeting
- Building a single-page application (SPA)
Web configuration options with Custom Code selected
2. Site Setup
Configure your basic site information. These settings affect how notifications appear to users.
Site setup form with required fields highlighted
Field | Description | Requirements |
---|---|---|
SITE NAME | Display name shown in push notifications | Keep it concise and recognizable |
SITE URL | Your complete website URL | Must include https:// and match exactly (include www if used) |
AUTO RESUBSCRIBE | Automatically resubscribes returning users | Recommended: Helps maintain subscriber count |
DEFAULT ICON URL | Icon for prompts and notifications | Square 256x256 pixels, .png/.jpg/.gif format, HTTPS URL required |
Common Gotchas:
- Site URL must match exactly (with or without
www
) - Icons must be served over HTTPS
- For local testing, see Local Testing Setup
3. Advanced Push Settings
Safari Web Push Certificate (Optional)
OneSignal provides Safari certificates automatically at no cost. Only enable this if you have existing Safari Web Push certificates you need to use.
Safari certificate upload option for existing certificates
If enabled, upload your Safari Web .p12 Push Certificate
and enter the password.
Click Save to continue.
4. Upload Service Worker Files
The OneSignal Service Worker is required for push notifications to function. You have two options:
Option 1: Create File Manually (Recommended)
- Create a new file named
OneSignalSDKWorker.js
- Add this single line of code:
- Upload the file to your website’s root directory
Option 2: Download and Upload
- Download the OneSignal SDK files
- Unzip the download
- Upload
OneSignalSDKWorker.js
to your server
File Hosting Requirements
Default Location: The service worker must be accessible at https://yoursite.com/OneSignalSDKWorker.js
Custom Location: If you need to place the file in a subfolder, see our OneSignal Service Worker Guide for detailed instructions.
Need a custom location? For subfolder placement or migrating from another push provider, follow our OneSignal Service Worker Guide.
Service worker file download interface
Legacy Setup Alert: If your site was set up before November 22, 2021, and you’re currently using two service worker files (OneSignalSDKWorker.js
and OneSignalSDKUpdaterWorker.js
), continue hosting both files to prevent service worker issues.
New setups only require the single OneSignalSDKWorker.js
file.
5. Add Code to Your Website
Basic Implementation
Add this code to your website’s <head>
section. Replace YOUR_ONESIGNAL_APP_ID
with your actual App ID from the OneSignal dashboard.
Framework-Specific Setup
If you’re using a JavaScript framework, follow these specialized guides:
Next Steps After Code Installation
- Initialize the SDK: The code above handles basic initialization
- Configure Prompting: Set up when and how users are prompted - see Permission Requests
- Add Custom Logic: Use the Web SDK Reference for advanced features
Common Integration Points:
- Custom prompt timing based on user behavior
- Segmentation and user tagging
- Event tracking and analytics
- A/B testing different notification strategies
Test Your Setup
Verify Installation
- Visit your website - you should see the OneSignal bell icon (if enabled)
- Click the bell to trigger the browser’s native permission prompt
- Subscribe to notifications
- Check browser console for any JavaScript errors
Send Test Notification
- Find your test subscription: Follow Find & Set Test Subscriptions
- Send a test push: Use our guide to send yourself a Push Notification
- Verify delivery across different browsers and devices
Troubleshooting Common Issues
Service Worker Problems:
- Ensure
OneSignalSDKWorker.js
is accessible at the correct URL - Check that the file contains the correct import statement
- Verify HTTPS is working properly
No Bell Icon Appearing:
- Confirm the JavaScript code is in the
<head>
section - Check browser console for errors
- Verify your App ID is correct
Notifications Not Received:
- Test in an incognito/private browser window
- Ensure notifications are enabled in browser settings
- Check that your site is served over HTTPS
Need Help? If you encounter issues, check our Web Push Troubleshooting Guide or contact [email protected]
for assistance.
What’s Next
After successful setup, consider these advanced features:
- Audience Segmentation for targeted messaging
- A/B Testing your notification strategies
- Analytics and Insights to track performance