-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
45 lines (39 loc) · 1.18 KB
/
app.js
File metadata and controls
45 lines (39 loc) · 1.18 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
/**
* Service Workers Push API hands-on
*/
$(function () {
// http -> https redirect
if (location.hostname !== "localhost" && location.protocol === "http:") {
location.assign("https" + location.href.slice(4));
return;
}
navigator.serviceWorker.register("worker.js")
.then(function (reg) {
if (window.Notification.permission === "denied") {
throw new Error("The user has blocked notification");
}
return reg.pushManager.getSubscription();
}).then(function (sub) {
if (! sub) {
// enable subscription button
$("#btn").prop("disabled", false);
return console.log("Unsubscribed.");
}
console.log("subscription:", sub.subscriptionId);
}).catch(function (err) {
console.error("register:", err);
alert("Failed.");
});
$("#btn").click(function () {
navigator.serviceWorker.ready.then(function (reg) {
reg.pushManager.subscribe().then(function (sub) {
// disable subscription button
$("#btn").prop("disabled", true);
console.log("subscribe:", sub.subscriptionId);
}).catch(function (err) {
console.error("subscribe:", err);
alert("Failed.");
});
});
});
});