Revert "Notifications: Request permission after user gesture (#3132)"
This reverts commit 19eee89a6de0ba14fd7257df08bbb5001ae9fe05.
This commit is contained in:
parent
44505f22a6
commit
be74d53e83
@ -6,7 +6,7 @@ import { getActions, withGlobal } from '../../../global';
|
|||||||
|
|
||||||
import useLang from '../../../hooks/useLang';
|
import useLang from '../../../hooks/useLang';
|
||||||
import useHistoryBack from '../../../hooks/useHistoryBack';
|
import useHistoryBack from '../../../hooks/useHistoryBack';
|
||||||
import { playNotifySound, checkIfNotificationsSupported } from '../../../util/notifications';
|
import { playNotifySound } from '../../../util/notifications';
|
||||||
|
|
||||||
import Checkbox from '../../ui/Checkbox';
|
import Checkbox from '../../ui/Checkbox';
|
||||||
import RangeSlider from '../../ui/RangeSlider';
|
import RangeSlider from '../../ui/RangeSlider';
|
||||||
@ -56,8 +56,6 @@ const SettingsNotifications: FC<OwnProps & StateProps> = ({
|
|||||||
|
|
||||||
const runDebounced = useRunDebounced(500, true);
|
const runDebounced = useRunDebounced(500, true);
|
||||||
|
|
||||||
const areNotificationsSupported = checkIfNotificationsSupported();
|
|
||||||
|
|
||||||
const handleSettingsChange = useCallback((
|
const handleSettingsChange = useCallback((
|
||||||
e: ChangeEvent<HTMLInputElement>,
|
e: ChangeEvent<HTMLInputElement>,
|
||||||
peerType: 'contact' | 'group' | 'broadcast',
|
peerType: 'contact' | 'group' | 'broadcast',
|
||||||
@ -149,12 +147,11 @@ const SettingsNotifications: FC<OwnProps & StateProps> = ({
|
|||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
subLabel={lang(hasWebNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')}
|
subLabel={lang(hasWebNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')}
|
||||||
checked={hasWebNotifications}
|
checked={hasWebNotifications}
|
||||||
disabled={!areNotificationsSupported}
|
|
||||||
onChange={handleWebNotificationsChange}
|
onChange={handleWebNotificationsChange}
|
||||||
/>
|
/>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
label="Offline notifications"
|
label="Offline notifications"
|
||||||
disabled={!hasWebNotifications || !areNotificationsSupported}
|
disabled={!hasWebNotifications}
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
subLabel={lang(hasPushNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')}
|
subLabel={lang(hasPushNotifications ? 'UserInfo.NotificationsEnabled' : 'UserInfo.NotificationsDisabled')}
|
||||||
checked={hasPushNotifications}
|
checked={hasPushNotifications}
|
||||||
@ -165,7 +162,6 @@ const SettingsNotifications: FC<OwnProps & StateProps> = ({
|
|||||||
label="Sound"
|
label="Sound"
|
||||||
min={0}
|
min={0}
|
||||||
max={10}
|
max={10}
|
||||||
disabled={!areNotificationsSupported}
|
|
||||||
value={notificationSoundVolume}
|
value={notificationSoundVolume}
|
||||||
onChange={handleVolumeChange}
|
onChange={handleVolumeChange}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -86,17 +86,7 @@ addActionHandler('initShared', (): ActionReturnType => {
|
|||||||
addActionHandler('initMain', (global): ActionReturnType => {
|
addActionHandler('initMain', (global): ActionReturnType => {
|
||||||
const { hasWebNotifications, hasPushNotifications } = selectNotifySettings(global);
|
const { hasWebNotifications, hasPushNotifications } = selectNotifySettings(global);
|
||||||
if (hasWebNotifications && hasPushNotifications) {
|
if (hasWebNotifications && hasPushNotifications) {
|
||||||
// Most of the browsers only show the notifications permission prompt after the first user gesture
|
void subscribe();
|
||||||
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 });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -73,7 +73,7 @@ function checkIfPushSupported() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkIfNotificationsSupported() {
|
function checkIfNotificationsSupported() {
|
||||||
// Let's check if the browser supports notifications
|
// Let's check if the browser supports notifications
|
||||||
if (!('Notification' in window)) {
|
if (!('Notification' in window)) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@ -137,27 +137,20 @@ function checkIfShouldResubscribe(subscription: PushSubscription | null) {
|
|||||||
|
|
||||||
async function requestPermission() {
|
async function requestPermission() {
|
||||||
if (!('Notification' in window)) return;
|
if (!('Notification' in window)) return;
|
||||||
let permission = Notification.permission;
|
if (!['granted', 'denied'].includes(Notification.permission)) {
|
||||||
if (!['granted', 'denied'].includes(permission)) {
|
await Notification.requestPermission();
|
||||||
permission = await Notification.requestPermission();
|
|
||||||
}
|
}
|
||||||
const isGranted = permission === 'granted';
|
|
||||||
const { updateWebNotificationSettings } = getActions();
|
|
||||||
updateWebNotificationSettings({
|
|
||||||
hasWebNotifications: isGranted,
|
|
||||||
hasPushNotifications: isGranted,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unsubscribeFromPush(subscription: PushSubscription | null) {
|
async function unsubscribeFromPush(subscription: PushSubscription | null) {
|
||||||
const global = getGlobal();
|
const global = getGlobal();
|
||||||
const { deleteDeviceToken } = getActions();
|
const dispatch = getActions();
|
||||||
if (subscription) {
|
if (subscription) {
|
||||||
try {
|
try {
|
||||||
const deviceToken = getDeviceToken(subscription);
|
const deviceToken = getDeviceToken(subscription);
|
||||||
await callApi('unregisterDevice', deviceToken);
|
await callApi('unregisterDevice', deviceToken);
|
||||||
await subscription.unsubscribe();
|
await subscription.unsubscribe();
|
||||||
deleteDeviceToken();
|
dispatch.deleteDeviceToken();
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@ -168,7 +161,7 @@ async function unsubscribeFromPush(subscription: PushSubscription | null) {
|
|||||||
}
|
}
|
||||||
if (global.push) {
|
if (global.push) {
|
||||||
await callApi('unregisterDevice', global.push.deviceToken);
|
await callApi('unregisterDevice', global.push.deviceToken);
|
||||||
deleteDeviceToken();
|
dispatch.deleteDeviceToken();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,7 +226,6 @@ export async function subscribe() {
|
|||||||
let subscription = await serviceWorkerRegistration.pushManager.getSubscription();
|
let subscription = await serviceWorkerRegistration.pushManager.getSubscription();
|
||||||
if (!checkIfShouldResubscribe(subscription)) return;
|
if (!checkIfShouldResubscribe(subscription)) return;
|
||||||
await unsubscribeFromPush(subscription);
|
await unsubscribeFromPush(subscription);
|
||||||
const { setDeviceToken, updateWebNotificationSettings } = getActions();
|
|
||||||
try {
|
try {
|
||||||
subscription = await serviceWorkerRegistration.pushManager.subscribe({
|
subscription = await serviceWorkerRegistration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
@ -244,13 +236,10 @@ export async function subscribe() {
|
|||||||
console.log('[PUSH] Received push subscription: ', deviceToken);
|
console.log('[PUSH] Received push subscription: ', deviceToken);
|
||||||
}
|
}
|
||||||
await callApi('registerDevice', deviceToken);
|
await callApi('registerDevice', deviceToken);
|
||||||
setDeviceToken(deviceToken);
|
getActions()
|
||||||
updateWebNotificationSettings({
|
.setDeviceToken(deviceToken);
|
||||||
hasWebNotifications: true,
|
|
||||||
hasPushNotifications: true,
|
|
||||||
});
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (Notification.permission === 'denied') {
|
if (Notification.permission === 'denied' as NotificationPermission) {
|
||||||
// The user denied the notification permission which
|
// The user denied the notification permission which
|
||||||
// means we failed to subscribe and the user will need
|
// means we failed to subscribe and the user will need
|
||||||
// to manually change the notification permission to
|
// to manually change the notification permission to
|
||||||
@ -272,10 +261,6 @@ export async function subscribe() {
|
|||||||
await requestPermission();
|
await requestPermission();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateWebNotificationSettings({
|
|
||||||
hasWebNotifications: false,
|
|
||||||
hasPushNotifications: false,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user