fix service worker

This commit is contained in:
jxxghp
2024-06-05 22:21:27 +08:00
parent e649be58a2
commit d559e1717c
2 changed files with 14 additions and 6 deletions

View File

@@ -119,10 +119,10 @@ async function subscribeForPushNotifications() {
async function afterLogin() {
// 生效主题配置
await setTheme()
// 订阅推送通知
await subscribeForPushNotifications()
// 跳转到首页或回原始页面
router.push(store.state.auth.originalPath ?? '/')
// 订阅推送通知
await subscribeForPushNotifications()
}
// 登录获取token事件

View File

@@ -18,12 +18,20 @@ const options = {
}
// 监听 push 事件,显示通知
self.addEventListener('push', function (e) {
if (!e.data) {
self.addEventListener('push', function (event) {
if (!event.data) {
return
}
// 解析获取推送消息
let payload = e.data.json()
let payload
try {
payload = event.data?.json()
} catch (err) {
console.log(err)
payload = {
title: event.data?.text(),
}
}
// 根据推送消息生成桌面通知并展现出来
let promise = self.registration.showNotification(payload.title, {
body: payload.body,
@@ -32,7 +40,7 @@ self.addEventListener('push', function (e) {
url: payload.url,
},
})
e.waitUntil(promise)
event.waitUntil(promise)
})
self.skipWaiting()