Some users use ad blockers that block the domain name of the OneSignal Web Push.
In this case, these users will not receive notifications and service worker not init on site (critical if you use additional service worker).
This worker proxy requests from WEB users to OneSignal Web Push SDK backend via Cloudflare Workers and your domain name.
index.js
is the content of the proxy script.
wrangler.toml
is the content of the proxy configuration.
✅ You need to have Cloudflare registration
✅ You need to have OneSignal registration
Example configuration for domain https://yourdomain.com and proxy setup https://onesignal.username.workers.dev don't forget change them, to your real domain name.
Create a Worker
✅ Edit name:
onesignal-proxy
✅ Remove existing code from Script field
✅ Copy code from index.js
and paste to Script field
✅ Uncomment line 2 and edit it with the worker domain that Cloudflare provides you with
const MYSUBDOMAIN = 'https://onesignal-proxy.username.workers.dev'
<head>
<script
src="https://onesignal-proxy.username.workers.dev/sdks/OneSignalSDK.js"
async=""
></script>
<script>
var OneSignal = window.OneSignal || []
OneSignal.push(function() {
OneSignal.init({
appId: 'YOUR_APP_ID',
notifyButton: {
enable: true,
},
})
OneSignal.showNativePrompt()
})
</script>
</head>
importScripts('https://onesignal-proxy.username.workers.dev/sdks/OneSignalSDK.js')
🎉 DONE!
https://dash.cloudflare.com/?account=workers
Install wrangler
npm i @cloudflare/wrangler -g
wrangler config
Edit file wrangler.toml
Setup account_id and zone_id from Cloudflare dashboard
Setup route and vars: with your domain:
route = "https://onesignal.example.com/*"
vars = { MYDOMAIN = "https://onesignal.example.com"}
wrangler publish
🎉 DONE!