接入群消息系统通知和本机开关

This commit is contained in:
xhgit committed 2026-11-15 22:11:23 +08:00
1 parent fef421bd4b
commit 5c882ac855
7 files changed
+179 -6

No files matched your search

+82 -4
View File
@@ -1,15 +1,81 @@
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useSession } from '../session'
const session = useSession()
const route = useRoute()
const router = useRouter()
const error = ref('')
const busy = ref(false)
const browser = window.xhchat?.openBrowser
const notice = ref<string>()
let noticeTimer: ReturnType<typeof setTimeout> | undefined
const systemNotices = ref(false)
const noticeSettingStatus = ref('')
let webNotice: Notification | undefined
let lastSystemNotice = 0
function noticeSettingKey() {
return 'xhchat-group-notifications:' + (window.xhchat?.apiBase ?? location.origin) + '/' + session.identity?.device.id
}
function closeSystemNotice() {
webNotice?.close()
webNotice = undefined
void window.xhchat?.closeGroupNotification().catch(() => {})
}
function readNoticeSetting() {
try { systemNotices.value = localStorage.getItem(noticeSettingKey()) === 'enabled' }
catch { systemNotices.value = false }
if (!systemNotices.value) closeSystemNotice()
}
function storageChanged(event: StorageEvent) {
if (event.key === noticeSettingKey()) readNoticeSetting()
}
async function toggleSystemNotices() {
noticeSettingStatus.value = ''
const enabled = !systemNotices.value
if (enabled) {
if (window.xhchat) {
if (!await window.xhchat.groupNotificationsSupported()) throw new Error('当前系统不支持桌面通知')
} else {
if (!('Notification' in window)) throw new Error('当前浏览器不支持系统通知')
if (await Notification.requestPermission() !== 'granted') throw new Error('请在浏览器站点设置中允许通知')
}
}
if (!session.identity) return
localStorage.setItem(noticeSettingKey(), enabled ? 'enabled' : 'disabled')
systemNotices.value = enabled
if (!enabled) closeSystemNotice()
noticeSettingStatus.value = enabled ? '已在本设备启用群消息系统通知' : '已在本设备关闭群消息系统通知'
}
function showSystemNotice(group: string) {
if (!systemNotices.value) return
if (window.xhchat) {
void window.xhchat.showGroupNotification().catch(() => {})
return
}
if (!('Notification' in window) || Notification.permission !== 'granted' ||
(document.visibilityState === 'visible' && document.hasFocus()) || Date.now() - lastSystemNotice < 2500) return
lastSystemNotice = Date.now()
try {
webNotice?.close()
webNotice = new Notification('XHChat', { body: '有新的群消息', icon: '/brand/xhchat.png', tag: 'xhchat-group' })
webNotice.onclick = () => {
if (!session.identity) return
window.focus()
void router.push('/groups/' + group)
closeSystemNotice()
}
} catch {
// The application notification remains available in browsers without desktop delivery.
}
}
function dismissNotice() {
clearTimeout(noticeTimer)
@@ -17,13 +83,23 @@ function dismissNotice() {
}
watch(() => session.groupChange, (event) => {
if (!event?.notify || route.params.id === event.groupId) return
if (!event?.notify) return
showSystemNotice(event.groupId)
if (route.params.id === event.groupId) return
dismissNotice()
notice.value = event.groupId
noticeTimer = setTimeout(dismissNotice, 8000)
})
watch(() => route.params.id, (id) => { if (id === notice.value) dismissNotice() })
onBeforeUnmount(dismissNotice)
onMounted(() => {
readNoticeSetting()
window.addEventListener('storage', storageChanged)
})
onBeforeUnmount(() => {
dismissNotice()
closeSystemNotice()
window.removeEventListener('storage', storageChanged)
})
async function act(action: () => Promise<void>) {
busy.value = true
@@ -62,6 +138,8 @@ async function act(action: () => Promise<void>) {
<div class="account-footer">
<p aria-live="polite"><span class="dot" :class="{ online: session.connection === '实时连接已建立' }"></span>{{ session.connection }}</p>
<p v-if="error" class="error" role="alert">{{ error }}</p>
<button class="plain" type="button" :aria-pressed="systemNotices" :disabled="busy" @click="act(toggleSystemNotices)">群消息系统通知</button>
<small v-if="noticeSettingStatus" role="status">{{ noticeSettingStatus }}</small>
<button class="plain" :disabled="busy" @click="act(session.logout)">退出登录</button>
</div>
</aside>