-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
48 lines (44 loc) · 1.07 KB
/
main.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
getPermission(function() {
var drone = new ScaleDrone('KtJ2qzn3CF3svSFe');
drone.on('open', function(error) {
if (error) return console.error(error);
// Join a room called notifications
var room = drone.subscribe('notifications');
room.on('open', function(error) {
if (error) {
console.error(error);
}
});
room.on('data', function(data) {
Push.create(data.title, {
body: data.body,
icon: data.icon,
timeout: 4000,
onClick: function() {
this.close();
}
});
});
});
drone.on('close', function(event) {
console.log('Connection was closed', event);
});
drone.on('error', function(error) {
console.error(error);
});
});
/**
* Ask the user for desktop notification permissions (if needed)
*/
function getPermission(onGranted) {
if (!Notification) {
alert('Notifications are not supported');
}
if (Push.Permission.has()) {
onGranted();
} else {
Push.Permission.request(onGranted, function() {
alert('Permission denied');
});
}
}