diff --git a/lib/utils/push-alerts.js b/lib/utils/push-alerts.js --- a/lib/utils/push-alerts.js +++ b/lib/utils/push-alerts.js @@ -12,7 +12,19 @@ const recordNotifPermissionAlertActionType = 'RECORD_NOTIF_PERMISSION_ALERT'; +const msInDay = 24 * 60 * 60 * 1000; +const shouldSkipPushPermissionAlert = ( + alertInfo: NotifPermissionAlertInfo, +): boolean => + (alertInfo.totalAlerts > 3 && + alertInfo.lastAlertTime > Date.now() - msInDay) || + (alertInfo.totalAlerts > 6 && + alertInfo.lastAlertTime > Date.now() - msInDay * 3) || + (alertInfo.totalAlerts > 9 && + alertInfo.lastAlertTime > Date.now() - msInDay * 7); + export { defaultNotifPermissionAlertInfo, recordNotifPermissionAlertActionType, + shouldSkipPushPermissionAlert, }; diff --git a/native/push/push-handler.react.js b/native/push/push-handler.react.js --- a/native/push/push-handler.react.js +++ b/native/push/push-handler.react.js @@ -29,6 +29,7 @@ import { type NotifPermissionAlertInfo, recordNotifPermissionAlertActionType, + shouldSkipPushPermissionAlert, } from 'lib/utils/push-alerts.js'; import { @@ -72,8 +73,6 @@ 'ForceTouchGestureHandler is not available', ]); -const msInDay = 24 * 60 * 60 * 1000; - type BaseProps = { +navigation: RootNavigationProp<'App'>, }; @@ -390,14 +389,7 @@ showNotifAlertOnAndroid() { const alertInfo = this.props.notifPermissionAlertInfo; - if ( - (alertInfo.totalAlerts > 3 && - alertInfo.lastAlertTime > Date.now() - msInDay) || - (alertInfo.totalAlerts > 6 && - alertInfo.lastAlertTime > Date.now() - msInDay * 3) || - (alertInfo.totalAlerts > 9 && - alertInfo.lastAlertTime > Date.now() - msInDay * 7) - ) { + if (shouldSkipPushPermissionAlert(alertInfo)) { return; } this.props.dispatch({ diff --git a/web/push-notif/push-notifs-handler.js b/web/push-notif/push-notifs-handler.js --- a/web/push-notif/push-notifs-handler.js +++ b/web/push-notif/push-notifs-handler.js @@ -13,6 +13,10 @@ useDispatchActionPromise, useServerCall, } from 'lib/utils/action-utils.js'; +import { + shouldSkipPushPermissionAlert, + recordNotifPermissionAlertActionType, +} from 'lib/utils/push-alerts.js'; import electron from '../electron.js'; import PushNotifModal from '../modals/push-notif-modal.react.js'; @@ -83,6 +87,10 @@ useCreateDesktopPushSubscription(); const createPushSubscription = useCreatePushSubscription(); + const notifPermissionAlertInfo = useSelector( + state => state.notifPermissionAlertInfo, + ); + const modalContext = useModalContext(); const loggedIn = useSelector(isLoggedIn); @@ -99,9 +107,17 @@ if (Notification.permission === 'granted') { // Make sure the subscription is current if we have the permissions await createPushSubscription(); - } else if (Notification.permission === 'default' && loggedIn) { + } else if ( + Notification.permission === 'default' && + loggedIn && + !shouldSkipPushPermissionAlert(notifPermissionAlertInfo) + ) { // Ask existing users that are already logged in for permission modalContext.pushModal(); + dispatch({ + type: recordNotifPermissionAlertActionType, + payload: { time: Date.now() }, + }); } })(); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -117,12 +133,26 @@ if (!prevLoggedIn.current && loggedIn) { if (Notification.permission === 'granted') { createPushSubscription(); - } else if (Notification.permission === 'default') { + } else if ( + Notification.permission === 'default' && + !shouldSkipPushPermissionAlert(notifPermissionAlertInfo) + ) { modalContext.pushModal(); + dispatch({ + type: recordNotifPermissionAlertActionType, + payload: { time: Date.now() }, + }); } } prevLoggedIn.current = loggedIn; - }, [createPushSubscription, loggedIn, modalContext, prevLoggedIn]); + }, [ + createPushSubscription, + dispatch, + loggedIn, + modalContext, + notifPermissionAlertInfo, + prevLoggedIn, + ]); // Redirect to thread on notification click React.useEffect(() => { diff --git a/web/root.js b/web/root.js --- a/web/root.js +++ b/web/root.js @@ -41,7 +41,13 @@ const persistConfig = { key: 'root', storage, - whitelist: ['enabledApps', 'deviceID', 'draftStore', 'cryptoStore'], + whitelist: [ + 'enabledApps', + 'deviceID', + 'draftStore', + 'cryptoStore', + 'notifPermissionAlertInfo', + ], migrate: (createMigrate(migrations, { debug: isDev }): any), version: 1, };