-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
66 lines (55 loc) · 1.59 KB
/
worker.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Service Worker script
*/
// プッシュ通知を受け取った時の処理
self.addEventListener("push", function (event) {
console.log("push event");
// GET request
var promise = self.fetch("http://api.openweathermap.org/data/2.5/weather?q=Sapporo,JP")
.then(function (res) {
var err;
if (res.status !== 200) {
err = new Error(res.status);
err.status = res.status;
throw err;
}
// get response body (json)
return res.json();
}).then(function (data) {
return self.registration.showNotification("天気情報", {
body: [
"都市: " + data.name.split("-")[0],
"天気: " + data.weather[0].main,
"気温: " + Math.round(data.main.temp - 273.15) + "℃"
].join("\n"),
icon: "html5.png",
tag: "weather"
});
}).catch(function (err) {
if (err.status === 404) {
return console.log(err);
}
console.error(err);
});
// 非同期処理が実行し終わるのを待つ
event.waitUntil(promise);
});
// 通知がクリックされた時の処理
self.addEventListener("notificationclick", function (event) {
console.log("notification click event:", event.notification.tag);
event.notification.close();
var promise = clients.matchAll({
type: "window"
}).then(function (client_list) {
client_list = client_list.filter(function (client) {
return client.url === "/" && "focus" in client;
});
if (client_list[0]) {
client_list[0].focus();
} else {
clients.openWindow("/");
}
console.log(client_list);
});
event.waitUntil(promise);
});