fix: системные уведомления через Web Notification API из renderer
Electron Notification из main-процесса молча игнорируется системой в dev-режиме (нет .desktop-регистрации). Исходный код использовал window.Notification из renderer — и он работал. - Убраны Notification/nativeImage/app из account-manager.ts (Electron-нативная нотификация из main-процесса удалена) - В NotificationContainer.vue восстановлена showOsNotification: Web Notification API, отправитель — title, тема — body; клик по уведомлению открывает письмо прямо из обработчика - Разрешение запрашивается при старте приложения превентивно - Удалён неиспользуемый канал notification:click из IpcEventMap Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
aaf701425b
commit
27775603e2
3 changed files with 31 additions and 36 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import { BrowserWindow, Notification, nativeImage, app } from 'electron'
|
||||
import { join } from 'node:path'
|
||||
import { BrowserWindow } from 'electron'
|
||||
import type { AccountConfig, ImapCredentials } from '@shared/types/account'
|
||||
import { AccountType } from '@shared/types/account'
|
||||
import type { IEmailProvider, FolderListResult, MessageListResult } from '@shared/types/provider'
|
||||
|
|
@ -392,31 +391,6 @@ export class AccountManager {
|
|||
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
|
||||
this.mainWindow.webContents.send('mail:new', { accountId, folderId, envelope })
|
||||
}
|
||||
|
||||
if (Notification.isSupported()) {
|
||||
const from = envelope.from.name || envelope.from.email || 'Новое письмо'
|
||||
const subject = envelope.subject || '(без темы)'
|
||||
|
||||
const iconPath = app.isPackaged
|
||||
? join(process.resourcesPath, 'icon.png')
|
||||
: join(app.getAppPath(), 'resources/icon.png')
|
||||
const icon = nativeImage.createFromPath(iconPath)
|
||||
|
||||
const n = new Notification({
|
||||
title: from,
|
||||
body: subject,
|
||||
icon: icon.isEmpty() ? undefined : icon,
|
||||
silent: false,
|
||||
})
|
||||
n.on('click', () => {
|
||||
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
|
||||
this.mainWindow.show()
|
||||
this.mainWindow.focus()
|
||||
this.mainWindow.webContents.send('notification:click', { accountId, folderId, messageId: envelope.id })
|
||||
}
|
||||
})
|
||||
n.show()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,20 +47,42 @@ function handleClick(id: string): void {
|
|||
removeToast(id)
|
||||
}
|
||||
|
||||
function showOsNotification(from: string, subject: string, onClick: () => void): void {
|
||||
if (!('Notification' in window)) return
|
||||
const send = (): void => {
|
||||
const n = new window.Notification(from, { body: subject, silent: false })
|
||||
n.onclick = () => { onClick(); n.close() }
|
||||
}
|
||||
if (Notification.permission === 'granted') {
|
||||
send()
|
||||
} else if (Notification.permission !== 'denied') {
|
||||
Notification.requestPermission().then(p => { if (p === 'granted') send() })
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
// Запрашиваем разрешение заранее, чтобы не задерживать первое уведомление
|
||||
if ('Notification' in window && Notification.permission === 'default') {
|
||||
Notification.requestPermission()
|
||||
}
|
||||
|
||||
on('mail:new', ({ accountId, folderId, envelope }) => {
|
||||
const account = accountsStore.accounts.find(a => a.id === accountId)
|
||||
if (!account) return
|
||||
if (notificationsStore.isMuted(accountId, folderId)) return
|
||||
const from = envelope.from.name || envelope.from.email
|
||||
const subject = envelope.subject || '(без темы)'
|
||||
addToast({ from, subject, accountEmail: account.email, accountId, folderId, messageId: envelope.id })
|
||||
})
|
||||
|
||||
on('notification:click', ({ accountId, folderId, messageId }) => {
|
||||
accountsStore.setActive(accountId)
|
||||
foldersStore.setActiveFolder(folderId)
|
||||
messagesStore.selectMessage(messageId)
|
||||
const from = envelope.from.name || envelope.from.email || 'Новое письмо'
|
||||
const subject = envelope.subject || '(без темы)'
|
||||
|
||||
// Системное уведомление — показываем всегда; клик фокусирует окно и открывает письмо
|
||||
showOsNotification(from, subject, () => {
|
||||
accountsStore.setActive(accountId)
|
||||
foldersStore.setActiveFolder(folderId)
|
||||
messagesStore.selectMessage(envelope.id)
|
||||
})
|
||||
|
||||
// In-app тост — дополнительная индикация внутри приложения
|
||||
addToast({ from, subject, accountEmail: account.email, accountId, folderId, messageId: envelope.id })
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -64,7 +64,6 @@ export interface IpcEventMap {
|
|||
'mail:new': { accountId: string; folderId: string; envelope: MessageEnvelope }
|
||||
'account:status': { accountId: string; connected: boolean; error?: string }
|
||||
'attachment:progress': { attachmentId: string; stage: 'downloading' | 'saving' | 'done' | 'error'; progress: number; error?: string }
|
||||
'notification:click': { accountId: string; folderId: string; messageId: string }
|
||||
}
|
||||
|
||||
export type IpcChannel = keyof IpcChannelMap
|
||||
|
|
|
|||
Loading…
Reference in a new issue