From be74d53e838681aa9cdd40dc1071087a311e3952 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 2 May 2023 15:45:09 +0400 Subject: [PATCH] Revert "Notifications: Request permission after user gesture (#3132)" This reverts commit 19eee89a6de0ba14fd7257df08bbb5001ae9fe05. --- .../left/settings/SettingsNotifications.tsx | 8 ++--- src/global/actions/ui/initial.ts | 12 +------ src/util/notifications.ts | 33 +++++-------------- 3 files changed, 12 insertions(+), 41 deletions(-) diff --git a/src/components/left/settings/SettingsNotifications.tsx b/src/components/left/settings/SettingsNotifications.tsx index 64c75f652..cf5b5fd5b 100644 --- a/src/components/left/settings/SettingsNotifications.tsx +++ b/src/components/left/settings/SettingsNotifications.tsx @@ -6,7 +6,7 @@ import { getActions, withGlobal } from '../../../global'; import useLang from '../../../hooks/useLang'; import useHistoryBack from '../../../hooks/useHistoryBack'; -import { playNotifySound, checkIfNotificationsSupported } from '../../../util/notifications'; +import { playNotifySound } from '../../../util/notifications'; import Checkbox from '../../ui/Checkbox'; import RangeSlider from '../../ui/RangeSlider'; @@ -56,8 +56,6 @@ const SettingsNotifications: FC = ({ const runDebounced = useRunDebounced(500, true); - const areNotificationsSupported = checkIfNotificationsSupported(); - const handleSettingsChange = useCallback(( e: ChangeEvent, peerType: 'contact' | 'group' | 'broadcast', @@ -149,12 +147,11 @@ const SettingsNotifications: FC = ({ // eslint-disable-next-line max-len subLabel={lang(hasWebNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')} checked={hasWebNotifications} - disabled={!areNotificationsSupported} onChange={handleWebNotificationsChange} /> = ({ label="Sound" min={0} max={10} - disabled={!areNotificationsSupported} value={notificationSoundVolume} onChange={handleVolumeChange} /> diff --git a/src/global/actions/ui/initial.ts b/src/global/actions/ui/initial.ts index 34f2b26b2..cde157898 100644 --- a/src/global/actions/ui/initial.ts +++ b/src/global/actions/ui/initial.ts @@ -86,17 +86,7 @@ addActionHandler('initShared', (): ActionReturnType => { addActionHandler('initMain', (global): ActionReturnType => { const { hasWebNotifications, hasPushNotifications } = selectNotifySettings(global); if (hasWebNotifications && hasPushNotifications) { - // Most of the browsers only show the notifications permission prompt after the first user gesture - const events = ['click', 'ontouchstart', 'keypress']; - const subscribeAfterUserGesture = () => { - void subscribe(); - events.forEach((event) => { - document.removeEventListener(event, subscribeAfterUserGesture); - }); - }; - events.forEach((event) => { - document.addEventListener(event, subscribeAfterUserGesture, { once: true }); - }); + void subscribe(); } }); diff --git a/src/util/notifications.ts b/src/util/notifications.ts index c1cd5709e..36c85fad2 100644 --- a/src/util/notifications.ts +++ b/src/util/notifications.ts @@ -73,7 +73,7 @@ function checkIfPushSupported() { return true; } -export function checkIfNotificationsSupported() { +function checkIfNotificationsSupported() { // Let's check if the browser supports notifications if (!('Notification' in window)) { if (DEBUG) { @@ -137,27 +137,20 @@ function checkIfShouldResubscribe(subscription: PushSubscription | null) { async function requestPermission() { if (!('Notification' in window)) return; - let permission = Notification.permission; - if (!['granted', 'denied'].includes(permission)) { - permission = await Notification.requestPermission(); + if (!['granted', 'denied'].includes(Notification.permission)) { + await Notification.requestPermission(); } - const isGranted = permission === 'granted'; - const { updateWebNotificationSettings } = getActions(); - updateWebNotificationSettings({ - hasWebNotifications: isGranted, - hasPushNotifications: isGranted, - }); } async function unsubscribeFromPush(subscription: PushSubscription | null) { const global = getGlobal(); - const { deleteDeviceToken } = getActions(); + const dispatch = getActions(); if (subscription) { try { const deviceToken = getDeviceToken(subscription); await callApi('unregisterDevice', deviceToken); await subscription.unsubscribe(); - deleteDeviceToken(); + dispatch.deleteDeviceToken(); return; } catch (error) { if (DEBUG) { @@ -168,7 +161,7 @@ async function unsubscribeFromPush(subscription: PushSubscription | null) { } if (global.push) { await callApi('unregisterDevice', global.push.deviceToken); - deleteDeviceToken(); + dispatch.deleteDeviceToken(); } } @@ -233,7 +226,6 @@ export async function subscribe() { let subscription = await serviceWorkerRegistration.pushManager.getSubscription(); if (!checkIfShouldResubscribe(subscription)) return; await unsubscribeFromPush(subscription); - const { setDeviceToken, updateWebNotificationSettings } = getActions(); try { subscription = await serviceWorkerRegistration.pushManager.subscribe({ userVisibleOnly: true, @@ -244,13 +236,10 @@ export async function subscribe() { console.log('[PUSH] Received push subscription: ', deviceToken); } await callApi('registerDevice', deviceToken); - setDeviceToken(deviceToken); - updateWebNotificationSettings({ - hasWebNotifications: true, - hasPushNotifications: true, - }); + getActions() + .setDeviceToken(deviceToken); } catch (error: any) { - if (Notification.permission === 'denied') { + if (Notification.permission === 'denied' as NotificationPermission) { // The user denied the notification permission which // means we failed to subscribe and the user will need // to manually change the notification permission to @@ -272,10 +261,6 @@ export async function subscribe() { await requestPermission(); } } - updateWebNotificationSettings({ - hasWebNotifications: false, - hasPushNotifications: false, - }); } }