-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
49 lines (45 loc) · 1.71 KB
/
sw.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
//Service Worker安装后请求首页并将首页缓存
self.addEventListener('install', function (event) {
var indexPage = new Request('index.html');
event.waitUntil(
fetch(indexPage).then(function (response) {
return caches.open('cache-homePage').then(function (cache) {
return cache.put(indexPage, response);
});
}));
});
//如果请求失败则先从缓存请求
self.addEventListener('fetch', function (event) {
var updateCache = function (request) {
return caches.open('cache-homePage').then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
};
event.waitUntil(updateCache(event.request));
event.respondWith(
fetch(event.request).catch(function (error) {
//查看缓存中是否存在
return caches.open('cache-homePage').then(function (cache) {
return cache.match(event.request).then(function (matching) {
var report = !matching || matching.status == 404 ? Promise.reject('no-match') : matching;
return report
});
});
})
);
})
//监听推送
self.addEventListener('push', function (e) {
var options = {
body: 'Here is a notification body!', //主体内容
icon: 'images/example.png', //通知图标
vibrate: [100, 50, 100], //震动,先100ms然后暂停50ms最后在震动100ms
data: { //通知数据,用于人机交互
dateOfArrival: Date.now(),
primaryKey: 1
}
};
self.registration.showNotification('Hello world!', options);
});