- add: createPushSubscription :

'Subscribed to FreePlanet.app!',
  'You can now receive Notification and Messages.'
This commit is contained in:
Paolo Arena
2019-02-05 18:17:36 +01:00
parent d8e0ff0bc3
commit cb941568e2
11 changed files with 242 additions and 157 deletions

View File

@@ -1,9 +1,16 @@
import { IGlobalState } from 'model'
import { storeBuilder } from './Store/Store'
import Vue from 'vue'
import urlBase64ToUint8Array from '../../js/utility'
import messages from '../../statics/i18n'
import { UserStore } from "@store"
const state: IGlobalState = {
conta: 0,
isSubscribed: false,
isLoginPage: false,
layoutNeeded: true,
mobileMode: false,
@@ -70,8 +77,109 @@ namespace Actions {
Mutations.mutations.setConta(num)
}
function createPushSubscription(context) {
if (!('serviceWorker' in navigator)) {
return
}
console.log('createPushSubscription')
let reg
const mykey = process.env.PUBLICKEY_PUSH
const mystate = state
navigator.serviceWorker.ready
.then(function (swreg) {
reg = swreg
return swreg.pushManager.getSubscription()
})
.then(function (subscription) {
mystate.isSubscribed = !(subscription === null)
if (mystate.isSubscribed) {
console.log('User is already Subscribed!')
} else {
// Create a new subscription
let convertedVapidPublicKey = urlBase64ToUint8Array(mykey)
return reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: convertedVapidPublicKey
})
}
})
.then(function (newSub) {
if (newSub) {
saveNewSubscriptionToServer(context, newSub)
mystate.isSubscribed = true;
}
return null
})
.catch(function (err) {
console.log(err)
})
}
// Calling the Server to Save in the MongoDB the Subscriber
function saveNewSubscriptionToServer(context, newSub) {
console.log('saveSubscriptionToServer: ', newSub)
console.log('context', context)
const options = {
title: t('notification.title_subscribed'),
content: t('notification.subscribed'),
openUrl: '/'
}
let myres = {
options: { ...options },
subs: newSub
}
return fetch(process.env.MONGODB_HOST + '/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(myres)
})
}
function t(params) {
let msg = params.split('.')
let lang = UserStore.state.lang
let stringa = messages[lang]
let ris = stringa
msg.forEach(param => {
ris = ris[param]
})
return ris
}
function prova(context) {
// console.log('prova')
// let msg = t('notification.title_subscribed')
// console.log('msg', msg)
}
function loadAfterLogin (context) {
}
export const actions = {
setConta: b.dispatch(setConta)
setConta: b.dispatch(setConta),
createPushSubscription: b.dispatch(createPushSubscription),
loadAfterLogin: b.dispatch(loadAfterLogin),
prova: b.dispatch(prova)
}
}

View File

@@ -496,6 +496,8 @@ namespace Actions {
GlobalStore.mutations.setleftDrawerOpen(localStorage.getItem(rescodes.localStorage.leftDrawerOpen) === 'true')
GlobalStore.mutations.setCategorySel(localStorage.getItem(rescodes.localStorage.categorySel))
GlobalStore.actions.loadAfterLogin()
Todos.actions.dbLoadTodo(true)