fix(webpush): rework web push notification status verification logic

Signed-off-by: 0xsysr3ll <0xsysr3ll@pm.me>
This commit is contained in:
0xsysr3ll
2025-12-09 00:04:28 +01:00
parent 9d41ecfecc
commit 275d6aaf08

View File

@@ -170,10 +170,30 @@ const UserWebPushSettings = () => {
useEffect(() => {
const verifyWebPush = async () => {
const enabled = await verifyPushSubscription(user?.id, currentSettings);
setWebPushEnabled(enabled);
let isEnabled = enabled;
if (!enabled && 'serviceWorker' in navigator) {
const { subscription } = await getPushSubscription();
if (subscription) {
isEnabled = true;
}
}
if (!isEnabled && dataDevices && dataDevices.length > 0) {
const currentUserAgent = navigator.userAgent;
const hasMatchingDevice = dataDevices.some(
(device) => device.userAgent === currentUserAgent
);
if (hasMatchingDevice || dataDevices.length === 1) {
isEnabled = true;
}
}
setWebPushEnabled(isEnabled);
localStorage.setItem(
'pushNotificationsEnabled',
enabled ? 'true' : 'false'
isEnabled ? 'true' : 'false'
);
};