Browser extensions - Adding browser notifications

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
In the previous article, we had an introduction to browser notifications. We were able to send a notification when the user clicked the button. However, that's quite once-off and pointless. Let's see how we can make it a recurring action. We want the...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In this article, we'll be exploring how to add browser notifications to our browser extension.
As the starting point, I'll use our popup extension. If you want to follow along, use the following GitHub repo.
The result of this article is the following interaction.

Browser notifications are native browsers that add notifications, much like you are used to on your mobile devices.
However, not many people opt-in for them at this stage. Let's hope this changes in the future.
For this article, we'll be using the popup extension to trigger a browser notification.
The first thing we'll have to do is give the correct permissions to our application.
Open up your manifest.json file and add the following permissions.
{
"permissions": [
"notifications"
]
}
This will allow us to access the native notification layer.
Then we can open up our src/App.jsx file.
Let's add a button in the rendering part.
export function App() {
return (
<div className='flex flex-col items-center justify-center w-screen h-auto bg-indigo-400 p-4'>
<button
className='bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 text-2xl px-4 rounded'
onClick={createNotification}
>
Surprise me 🎉
</button>
</div>
);
}
You might have spotted the createNotification on the click handler. Let's quickly add that function to our file.
const createNotification = () => {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon-48.png',
title: 'Hi there 👋',
message: 'Just a reminder that you rock!',
buttons: [{ title: 'I know ☺️' }],
priority: 0,
});
};
This function calls the browser notification API and creates a new notification. The notification will be called instantly. We set a title, message, and custom button in our example.
Note: You can find all options in the documentation
Now let's build our app and see what happens. Follow the guide here to build your app.
You should now see the notification occur!

If you want to see the complete source code, I hosted it on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter