improve web push notifications

This commit is contained in:
Egor Kislitsyn 2018-12-07 14:57:35 +07:00
parent 09147cacea
commit ebe2a95140
2 changed files with 10 additions and 7 deletions

View file

@ -1,9 +1,8 @@
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
import registerPushNotifications from '../services/push/push.js'
import { compact, map, each, merge } from 'lodash'
import { set } from 'vue'
import registerPushNotifications from '../services/push/push.js'
// TODO: Unify with mergeOrAdd in statuses.js
export const mergeOrAdd = (arr, obj, item) => {
if (!item) { return false }
@ -120,10 +119,6 @@ const users = {
store.commit('addNewUsers', mutedUsers)
})
if ('Notification' in window && window.Notification.permission === 'default') {
window.Notification.requestPermission()
}
// Fetch our friends
store.rootState.api.backendInteractor.fetchFriends({id: user.id})
.then((friends) => commit('addNewUsers', friends))

View file

@ -26,7 +26,11 @@ function registerServiceWorker () {
function askPermission () {
return new Promise(function (resolve, reject) {
if (!window.Notification) return resolve('Notifications disabled')
if (!window.Notification) return reject(new Error('Notifications disabled'))
if (window.Notification.permission !== 'default') {
return resolve(window.Notification.permission)
}
const permissionResult = window.Notification.requestPermission(function (result) {
resolve(result)
@ -42,6 +46,10 @@ function askPermission () {
}
function subscribe (registration, store) {
if (!store.rootState.instance.vapidPublicKey) {
return Promise.reject(new Error('VAPID publick key is not found'))
}
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(store.rootState.instance.vapidPublicKey)