improve push notifications code

This commit is contained in:
Egor Kislitsyn 2018-12-09 19:25:43 +07:00
parent 11716a7a53
commit 73b17d70ec
3 changed files with 48 additions and 65 deletions

View File

@ -12,16 +12,23 @@ const subscribe = {
setVapidPublicKey (state, vapidPublicKey) { setVapidPublicKey (state, vapidPublicKey) {
state.vapidPublicKey = vapidPublicKey state.vapidPublicKey = vapidPublicKey
} }
}, },
actions: { actions: {
setInstanceOption (store, { name, value }) { setInstanceOption (store, { name, value }) {
store.commit('setVapidPublicKey', value) if (name === 'vapidPublicKey') {
if (store.state.token) registerPushNotifications(this) store.commit('setVapidPublicKey', value)
if (store.state.token) {
registerPushNotifications(store.rootState.config.webPushNotifications, value, store.state.token)
}
}
}, },
setCurrentUser (store, user) { setCurrentUser (store, user) {
store.commit('setApiToken', user.credentials) store.commit('setApiToken', user.credentials)
if (store.state.vapidPublicKey) registerPushNotifications(this)
if (store.state.vapidPublicKey) {
registerPushNotifications(store.rootState.config.webPushNotifications, store.state.vapidPublicKey, user.credentials)
}
} }
} }
} }

View File

@ -14,58 +14,42 @@ function isPushSupported () {
function registerServiceWorker () { function registerServiceWorker () {
return navigator.serviceWorker.register('/static/sw.js') return navigator.serviceWorker.register('/static/sw.js')
.then(function (registration) { .catch((err) => console.error('Unable to register service worker.', err))
console.log('Service worker successfully registered.')
return registration
})
.catch(function (err) {
console.error('Unable to register service worker.', err)
})
} }
function askPermission () { function askPermission () {
return new Promise(function (resolve, reject) { return new Promise((resolve, reject) => {
if (!window.Notification) return reject(new Error('Notifications disabled')) const Notification = window.Notification
if (window.Notification.permission !== 'default') { if (!Notification) return reject(new Error('Notifications disabled'))
return resolve(window.Notification.permission) if (Notification.permission !== 'default') return resolve(Notification.permission)
}
const permissionResult = window.Notification.requestPermission(function (result) { const permissionResult = Notification.requestPermission(resolve)
resolve(result)
})
if (permissionResult) permissionResult.then(resolve, reject) if (permissionResult) permissionResult.then(resolve, reject)
}).then(function (permissionResult) { }).then((permissionResult) => {
if (permissionResult !== 'granted') { if (permissionResult !== 'granted') throw new Error('We weren\'t granted permission.')
throw new Error('We weren\'t granted permission.')
}
return permissionResult return permissionResult
}) })
} }
function subscribe (registration, store) { function subscribe (registration, isEnabled, vapidPublicKey) {
if (!store.rootState.config.webPushNotifications) { if (!isEnabled) return Promise.reject(new Error('Web Push is disabled in config'))
return Promise.reject(new Error('Web Push is disabled in config')) if (!vapidPublicKey) return Promise.reject(new Error('VAPID public key is not found'))
}
if (!store.rootState.instance.vapidPublicKey) {
return Promise.reject(new Error('VAPID public key is not found'))
}
const subscribeOptions = { const subscribeOptions = {
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(store.rootState.instance.vapidPublicKey) applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
} }
return registration.pushManager.subscribe(subscribeOptions) return registration.pushManager.subscribe(subscribeOptions)
} }
function sendSubscriptionToBackEnd (subscription, store) { function sendSubscriptionToBackEnd (subscription, token) {
return window.fetch('/api/v1/push/subscription/', { return window.fetch('/api/v1/push/subscription/', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${store.rootState.oauth.token}` 'Authorization': `Bearer ${token}`
}, },
body: JSON.stringify({ body: JSON.stringify({
subscription, subscription,
@ -79,28 +63,23 @@ function sendSubscriptionToBackEnd (subscription, store) {
} }
}) })
}) })
.then(function (response) { .then((response) => {
if (!response.ok) { if (!response.ok) throw new Error('Bad status code from server.')
throw new Error('Bad status code from server.')
}
return response.json() return response.json()
}) })
.then(function (responseData) { .then((responseData) => {
if (!responseData.id) { if (!responseData.id) throw new Error('Bad response from server.')
throw new Error('Bad response from server.')
}
return responseData return responseData
}) })
} }
export default function registerPushNotifications (store) { export default function registerPushNotifications (isEnabled, vapidPublicKey, token) {
if (isPushSupported()) { if (isPushSupported()) {
registerServiceWorker() registerServiceWorker()
.then(function (registration) { .then((registration) => {
return askPermission() return askPermission()
.then(() => subscribe(registration, store)) .then(() => subscribe(registration, isEnabled, vapidPublicKey))
.then((subscription) => sendSubscriptionToBackEnd(subscription, store)) .then((subscription) => sendSubscriptionToBackEnd(subscription, token))
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`)) .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
}) })
} }

View File

@ -1,32 +1,29 @@
/* eslint-env serviceworker */ /* eslint-env serviceworker */
self.addEventListener('push', function (event) {
function getWindowClients () {
return clients.matchAll({ includeUncontrolled: true })
.then((clientList) => clientList.filter(({ type }) => type === 'window'))
}
self.addEventListener('push', (event) => {
if (event.data) { if (event.data) {
const data = event.data.json() event.waitUntil(getWindowClients().then((list) => {
const data = event.data.json()
const promiseChain = clients.matchAll({ if (list.length === 0) return self.registration.showNotification(data.title, data)
includeUncontrolled: true }))
}).then(function (clientList) {
const list = clientList.filter((item) => item.type === 'window')
if (list.length) return
return self.registration.showNotification(data.title, data)
})
event.waitUntil(promiseChain)
} }
}) })
self.addEventListener('notificationclick', function (event) { self.addEventListener('notificationclick', (event) => {
event.notification.close() event.notification.close()
event.waitUntil(clients.matchAll({ event.waitUntil(getWindowClients().then((list) => {
includeUncontrolled: true
}).then(function (clientList) {
const list = clientList.filter((item) => item.type === 'window')
for (var i = 0; i < list.length; i++) { for (var i = 0; i < list.length; i++) {
var client = list[i] var client = list[i]
if (client.url === '/' && 'focus' in client) { return client.focus() } if (client.url === '/' && 'focus' in client) { return client.focus() }
} }
if (clients.openWindow) { return clients.openWindow('/') }
if (clients.openWindow) return clients.openWindow('/')
})) }))
}) })