-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw1.js
249 lines (221 loc) · 6.29 KB
/
sw1.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'use strict';
/*
importScripts('sw-toolbox.js');
toolbox.precache(["index.html","css/core.css"]);
toolbox.router.get("/images/*", toolbox.cacheFirst);
toolbox.router.get("/icons/*", toolbox.cacheFirst);
toolbox.router.get("/*", toolbox.networkFirst, {
networkTimeoutSeconds: 5});
*/
const VERSION = 0.01
let message_object = {}
self.addEventListener('install', function(event) {
self.skipWaiting();
event.waitUntil(
caches.open(VERSION).then(function(cache) {
return cache.addAll([
'/',
'/manifest.json',
'/css/core.css',
'/script/push.js'
]);
})
);
});
self.addEventListener('fetch', function(event) {
let request = event.request;
if (request.method !== 'GET') return;
event.respondWith(
caches.match(request).then(function(response) {
return response || fetch(request);
}).catch(function() {
return caches.match('/');
})
);
});
self.addEventListener('activate', (event) => {
if (self.clients && clients.claim) {
clients.claim();
}
event.waitUntil(
caches
.keys()
.then((keys) => {
return Promise.all(
keys
.filter((key) => {
return !key.startsWith(VERSION);
})
.map((key) => {
return caches.delete(key);
})
);
})
.then(() => {
console.log('new service worker version registered', VERSION);
subscribeUser()
}).catch((error) => {
console.error('error registering new service worker version', error);
})
);
});
self.addEventListener("push", (event) => {
message_object = JSON.parse(event.data.text());
console.log(message_object)
/*
if ( 'notification' in message_object){
return ;
}
if ('data' in message_object){
if ( navigator.userAgent.indexOf('Macintosh') != -1){
message_object.data.requireInteraction=false;
}
}
*/
let title = message_object.title;
let options = {
body: message_object.body,
icon: message_object.icon,
badge: "https://and07.github.io/images/2040077.png",
image: message_object.image || "https://and07.github.io/images/2040077.png",
sticky: !0,
noscreen: !1,
requireInteraction: !0,
data: {
url: message_object.link,
//onClick: () => alert(1)
},
actions: [
{
action: 'coffee-action',
title: 'Coffee',
icon: 'https://and07.github.io/icons/apple-icon-120x120.png'
},
{
action: 'doughnut-action',
title: 'Doughnut',
icon: 'https://and07.github.io/icons/apple-icon-120x120.png'
},
{
action: 'gramophone-action',
title: 'gramophone',
icon: 'https://and07.github.io/icons/apple-icon-120x120.png'
},
{
action: 'atom-action',
title: 'Atom',
icon: 'https://and07.github.io/icons/apple-icon-120x120.png'
}
]
}
console.log('Notification.maxActions ', Notification.maxActions)
const testDataObject = {
name: "waitUntil",
favorite_drink: "Fire Ball",
favorite_food: "Steak"
}
event.waitUntil(
self.registration.showNotification(title, options)
.then(postsendTestDataData(testDataObject))
)
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
console.log('self in click event Listener', self)
console.log('event', event)
if (!event.action) {
// Was a normal notification click
console.log('Notification Click.');
event.waitUntil(
clients.openWindow(event.notification.data.url)
);
return;
}
switch (event.action) {
case 'coffee-action':
console.log('User ❤️️\'s coffee.');
break;
case 'doughnut-action':
console.log('User ❤️️\'s doughnuts.');
break;
case 'gramophone-action':
console.log('User ❤️️\'s music.');
break;
case 'atom-action':
console.log('User ❤️️\'s science.');
break;
default:
console.log(`Unknown action clicked: '${event.action}'`);
break;
}
});
self.addEventListener('pushsubscriptionchange', (event) => {
console.log('pushsubscriptionchange!!!');
console.log(event);
subscribeUser()
});
function subscribeUser() {
const applicationServerKey = urlB64ToUint8Array('BJrbQTpQd72tDRmegu-HqrXPx9VyYqmnZAes0Y_IF6HrGTbGfk9_rByEOcXxpPm-A1YE5PlVYf5D9H3_vj21O8w');
const options = {
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}
return self.registration.pushManager.subscribe(options).then(function(subscription) {
console.log(subscription)
sendSubscriptionToBackEnd(subscription)
}).catch(function(err) {
console.log('Failed to subscribe the user: ', err);
});
}
// urlB64ToUint8Array is a magic function that will encode the base64 public key
// to Array buffer which is needed by the subscription option
const urlB64ToUint8Array = base64String => {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4)
const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/')
const rawData = atob(base64)
const outputArray = new Uint8Array(rawData.length)
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i)
}
return outputArray
}
function sendSubscriptionToBackEnd(subscription) {
let endPoint;
const local = location.origin.includes("127");
endPoint = local ? "http://localhost:3000/" : "https://and07-push-notifications.herokuapp.com/subscriptions"
return fetch(endPoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(subscription)
}).then(function(response) {
if (!response.ok) {
throw new Error('Bad status code from server.');
}
return response.json();
}).then(function(responseData) {
console.log(responseData)
/*
if (!(responseData.data && responseData.data.success)) {
throw new Error('Bad response from server.');
}
*/
});
}
function postsendTestDataData(data) {
let domain;
const local = location.origin.includes("127");
domain = local ? "http://localhost:3000/" : "https://and07-push-notifications.herokuapp.com/"
const url = `${domain}test-data`
console.log(url)
return fetch(url, {
body: JSON.stringify(data), // must match 'Content-Type' header
method: 'POST',
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
})
.then(response => console.log(response)) // parses response to JSON
}