-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
52 lines (51 loc) · 2 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
50
51
52
COMMON.loginUser(function (err, data) {
if (err) {
var message = "Seems like webmail is down. Please try again later !!";
if (data) {
if (data.code === 1 || data.code === 99) return; // student ID or password weren't there, or no internet
message = "Login Failed. Please check ID and password and try logging in again !!";
}
// post message to popup to show error
chrome.notifications.create(null, {
type: 'basic',
title: 'DAIICT Webmail',
message: message,
iconUrl: 'images/da-logo-128.png'
});
} else {
setUnreadInBadge();
}
});
function setUnreadInBadge() {
COMMON.getUnreadEmails(function (err, data) {
console.log("Unread emails", err, data);
if (data && data.length) {
var newEmails = data.length;
chrome.storage.sync.get('lastNotification', function (storageData) {
if (storageData.lastNotification) {
newEmails = 0;
data.map(function (msg) {
if (parseInt(msg.$.d) > storageData.lastNotification) {
newEmails++;
}
})
}
if (newEmails) {
chrome.notifications.create(null, {
type: 'basic',
title: 'DAIICT Webmail',
message: 'You have ' + newEmails + ' new ' + (newEmails > 1 ? 'emails' : 'email'),
iconUrl: 'images/da-logo-128.png'
});
var notificationSound = new Audio('./audio/notification.ogg');
notificationSound.play();
chrome.storage.sync.set({ lastNotification: Date.now() });
}
});
}
// TODO: emit event so popup can show latest emails
setTimeout(function () {
setUnreadInBadge();
}, 30000)
});
}