Keep Reading
If you enjoyed the post you just read, we have more to say!
Web push notifications are powered by service workers. A service worker is a script that your browser runs in the background. Using a service worker, you can send web push notifications to your users even when they don't open their browser.
There are a few differences in how browsers implement this feature. At MagicBell, we have created abstractions to hide the complexity so you can enable web push notifications without breaking a sweat.
Before getting our hands dirty on code, we must ensure that "web push" is enabled in our project delivery settings. Please go to the MagicBell dashboard and navigate to "Settings" > "Smart Delivery". On the Smart Delivery page, ensure that "Deliver to Web push" is listed in the delivery sequence, or enable it by dragging it from the "Add Items" panel.
Websites can only have a single service worker. If you already have a service worker defined, add the following import. If you do not, create a new file with the following contents:
Make sure that this file is available at https://your-domain.com/sw.js
.
A few notes here,
assets.example.com
while serving your site from example.com
. Let's create a "subscribe button" using our headless webpush library. You can use it to register the service worker, request the user for permissions and store subscriptions to send browser push notifications.
We're using React in our example, but porting this to any other framework should be trivial.
And that's it! You have enabled web push notifications for every modern browser, Chrome, Firefox, Safari, and many others. Try creating a notification through our API or CLI, and let the magic begin.
magicbell notifications create --recipients 'you@example.com' --title 'test web push!'
Provide the serviceWorkerPath
to subscribe
when your (existing) service worker is not stored as sw.js
.
await subscribe({ ...token, serviceWorkerPath: './service-worker.js' });
Modern Safari supports web push notifications. For Safari mobile (iOS), you'll need to have an installed PWA to be able to ask for notification permissions and send notifications. You can test this experience using http://webpushtest.com.
When using our hosted solution, a PWA is provided out of the box with the subscribe dialog. We offer white labeling and custom domains as well. Please reach out to us if that piques your interest.
Related articles to web push notifications: