forked from MAP-NCU2015/Lesson-06-WebAPIBasics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarm.js
More file actions
79 lines (52 loc) · 2.01 KB
/
alarm.js
File metadata and controls
79 lines (52 loc) · 2.01 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
(function(exports) {
var Alarm = function() {
this.alarmHour=document.getElementById('alarmHour');
this.alarmMinute=document.getElementById('alarmMinute');
this.alarmSecond=document.getElementById('alarmSecond');
this.alarmStartButton=document.getElementById('alarmStartButton');
};
Alarm.prototype = {
start: function()
{
this.alarmStartButton.addEventListener('click', function(event) {
this.alarmTime=new Date();
this.alarmTime.setHours(this.alarmTime.getHours()+parseInt(this.alarmHour.value));
this.alarmTime.setMinutes(this.alarmTime.getMinutes()+parseInt(this.alarmMinute.value));
this.alarmTime.setSeconds(this.alarmTime.getSeconds()+parseInt(this.alarmSecond.value));
var request = navigator.mozAlarms.add(this.alarmTime, "honorTimezone","time's up");
request.onsuccess = function () {
console.log("The alarm has been scheduled");
};
request.onerror = function () {
console.log("An error occurred: " + this.error.name);
};
navigator.mozSetMessageHandler("alarm", function (mozAlarm) {
//alert("alarm fired: " + JSON.stringify(mozAlarm.data));
this.notify(JSON.stringify(mozAlarm.data));
}.bind(this));
}.bind(this));
},
notify: function(text)
{
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}
// Let's check whether notification permissions have alredy been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification(text);
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
};
exports.Alarm = Alarm;
})(window);