Revert "Notifications: Request permission after user gesture (#3132)"

This reverts commit 19eee89a6de0ba14fd7257df08bbb5001ae9fe05.
This commit is contained in:
Alexander Zinchuk 2023-05-02 15:45:09 +04:00
parent 44505f22a6
commit be74d53e83
3 changed files with 12 additions and 41 deletions

View File

@ -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<OwnProps & StateProps> = ({
const runDebounced = useRunDebounced(500, true);
const areNotificationsSupported = checkIfNotificationsSupported();
const handleSettingsChange = useCallback((
e: ChangeEvent<HTMLInputElement>,
peerType: 'contact' | 'group' | 'broadcast',
@ -149,12 +147,11 @@ const SettingsNotifications: FC<OwnProps & StateProps> = ({
// eslint-disable-next-line max-len
subLabel={lang(hasWebNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')}
checked={hasWebNotifications}
disabled={!areNotificationsSupported}
onChange={handleWebNotificationsChange}
/>
<Checkbox
label="Offline notifications"
disabled={!hasWebNotifications || !areNotificationsSupported}
disabled={!hasWebNotifications}
// eslint-disable-next-line max-len
subLabel={lang(hasPushNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')}
checked={hasPushNotifications}
@ -165,7 +162,6 @@ const SettingsNotifications: FC<OwnProps & StateProps> = ({
label="Sound"
min={0}
max={10}
disabled={!areNotificationsSupported}
value={notificationSoundVolume}
onChange={handleVolumeChange}
/>

View File

@ -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();
}
});

View File

@ -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,
});
}
}