mirror of
https://github.com/sct/overseerr.git
synced 2025-12-25 08:14:29 +01:00
fix: add flag in local storage for notifications (#4180)
* fix: add flag in local storage for notifications refactor: update comment * fix: always resubscribe if subscription is not valid
This commit is contained in:
@@ -19,23 +19,32 @@ const ServiceWorkerSetup = () => {
|
||||
registration.scope
|
||||
);
|
||||
|
||||
// Do not prompt for permissions as we will handle this in the settings.
|
||||
if (Notification.permission !== 'granted') {
|
||||
const pushNotificationsEnabled =
|
||||
localStorage.getItem('pushNotificationsEnabled') === 'true';
|
||||
|
||||
// Reset the notifications flag if permissions were revoked
|
||||
if (
|
||||
Notification.permission !== 'granted' &&
|
||||
pushNotificationsEnabled
|
||||
) {
|
||||
localStorage.setItem('pushNotificationsEnabled', 'false');
|
||||
console.warn(
|
||||
'[SW] Push permissions not granted — skipping resubscribe'
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Bypass resubscribing if we have manually disabled push notifications
|
||||
if (!pushNotificationsEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const subscription = await registration.pushManager.getSubscription();
|
||||
// Bypass resubscribing if we have manually disabled the subscription
|
||||
if (!subscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
'[SW] Existing push subscription:',
|
||||
subscription.endpoint
|
||||
subscription?.endpoint
|
||||
);
|
||||
|
||||
const verified = await verifyAndResubscribePushSubscription(
|
||||
|
||||
@@ -82,6 +82,7 @@ const UserWebPushSettings = () => {
|
||||
);
|
||||
|
||||
if (isSubscribed) {
|
||||
localStorage.setItem('pushNotificationsEnabled', 'true');
|
||||
setWebPushEnabled(true);
|
||||
addToast(intl.formatMessage(messages.webpushhasbeenenabled), {
|
||||
appearance: 'success',
|
||||
@@ -106,6 +107,7 @@ const UserWebPushSettings = () => {
|
||||
try {
|
||||
await unsubscribeToPushNotifications(user?.id, endpoint);
|
||||
|
||||
localStorage.setItem('pushNotificationsEnabled', 'false');
|
||||
setWebPushEnabled(false);
|
||||
addToast(intl.formatMessage(messages.webpushhasbeendisabled), {
|
||||
autoDismiss: true,
|
||||
|
||||
@@ -74,12 +74,10 @@ export const verifyAndResubscribePushSubscription = async (
|
||||
if (currentSettings.enablePushRegistration) {
|
||||
try {
|
||||
// Unsubscribe from the backend to clear the existing push subscription (keys and endpoint)
|
||||
const hasUnsubscribed = await unsubscribeToPushNotifications(userId);
|
||||
await unsubscribeToPushNotifications(userId);
|
||||
|
||||
// Subscribe again to generate a fresh push subscription with updated keys and endpoint
|
||||
if (hasUnsubscribed) {
|
||||
await subscribeToPushNotifications(userId, currentSettings);
|
||||
}
|
||||
await subscribeToPushNotifications(userId, currentSettings);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user