Browser extensions - Repeating 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 today's article, we'll transform our already excellent popup browser extension to be a little more personal. We are going to give the user the option to colorize the popup. To maintain what the user has picked, we'll leverage chrome's storage capa...
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 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 user to start the flow and get a message every hour to remind them that they are awesome.
I'll be working with the previous article as the starting point. You can use this GitHub branch if you like to follow along.
To achieve the repeating notifications, we have to leverage the alarm API. This allows us to create alarms that can go off at a specified time.
To gain access to this API, we must register it in our manifest.json file.
{
"permissions": [
"alarms",
"notifications"
]
}
While we have this file open, we also need to introduce a background worker. Since we are working with V3 of the manifest, we should register it as a service worker like this.
{
"background": {
"service_worker": "background.js"
},
}
This background worker will act as the operation's brains and send out the notifications.
Let's first modify our App.jsx so we can trigger the right things.
The first thing I did was add another button, so the user always has the option to stop the repeating alarm.
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}
>
Motivate me π
</button>
<br />
<button
className='bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 text-2xl px-4 rounded'
onClick={stopNotification}
>
Stop motivating me π
</button>
</div>
);
}
Now let's go ahead and create the two actions.
The first one will be createNotification. This should add a repeating alarm.
We can use the alarms API and the periodInMinutes option to achieve that. With this option set, the alarm will repeat itself every minute. (to the value you use).
const createNotification = () => {
chrome.alarms.create({
periodInMinutes: 60,
});
window.close();
};
Optional you can use the delayInMinutes to add a custom delay.
Note: When testing, you can set it to 1 minute.
Then we need to add the function that will stop the alarms when the user requests this.
const stopNotification = () => {
chrome.alarms.clearAll();
window.close();
};
And that's it. Our alarms are now managed. However, we won't see any notifications yet.
This is where the background script comes in.
Create a background.js file inside your public folder.
In there, we need to add a listener to the chrome alarms. Each time the alarm goes off, we want to send a notification as we did before.
chrome.alarms.onAlarm.addListener(() => {
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,
});
});
Our code is working, but at the moment, it would trigger on every alarm, this could mean other plugins also set the alarm, and our script would trigger.
To help with that, we can set a specific name for our alarm.
Modify the App.jsx script to set the alarm with a name.
chrome.alarms.create('motivation_alarm', {
periodInMinutes: 1,
});
Then we need to change our background.js script to only fire notifications if this alarm went off.
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'motivation_alarm') {
chrome.notifications.create({
...
});
}
});
And that's it. Our alarms now only go off for our extension.
You can find the complete code example on this GitHub branch.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter