-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
49 lines (43 loc) · 1.36 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let reminderInterval = 60; // Default reminder interval in minutes
// Load the reminder interval from storage if available
chrome.storage.sync.get("interval", function (data) {
if (data.interval) {
reminderInterval = data.interval;
}
// Create the alarm with the loaded or default interval
createAlarm();
});
function createAlarm() {
// Clear any existing alarm with the same name
chrome.alarms.clear("hydrateAlarm");
// Create a new alarm with the updated interval
chrome.alarms.create("hydrateAlarm", { delayInMinutes: reminderInterval, periodInMinutes: reminderInterval });
}
chrome.alarms.onAlarm.addListener(function (alarm) {
if (alarm.name === "hydrateAlarm") {
showNotification();
}
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.updateAlarm) {
// Update the reminder interval and create the alarm
chrome.storage.sync.get("interval", function (data) {
if (data.interval) {
reminderInterval = data.interval;
createAlarm();
}
});
} else if (message.testReminder) {
// Trigger a test reminder immediately
showNotification();
}
});
function showNotification() {
const options = {
type: "basic",
iconUrl: "icons/icon128.png",
title: "H2O Reminder",
message: "It's time to drink some water!",
};
chrome.notifications.create(options);
}