27 lines
862 B
JavaScript
27 lines
862 B
JavaScript
|
|
self.addEventListener('push', function (event) {
|
||
|
|
var data = {};
|
||
|
|
try { data = event.data.json(); } catch (e) { data = { title: '沈晏', body: event.data ? event.data.text() : '' }; }
|
||
|
|
event.waitUntil(
|
||
|
|
self.registration.showNotification(data.title || '沈晏', {
|
||
|
|
body: data.body || '',
|
||
|
|
icon: '/icon-192.png',
|
||
|
|
badge: '/icon-192.png',
|
||
|
|
vibrate: [200, 100, 200],
|
||
|
|
tag: 'shenyan-push',
|
||
|
|
renotify: true,
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('notificationclick', function (event) {
|
||
|
|
event.notification.close();
|
||
|
|
event.waitUntil(
|
||
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
|
||
|
|
for (var i = 0; i < list.length; i++) {
|
||
|
|
if (list[i].url && 'focus' in list[i]) return list[i].focus();
|
||
|
|
}
|
||
|
|
if (clients.openWindow) return clients.openWindow('/');
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|