Page MenuHomePhabricator

D6819.diff
No OneTemporary

D6819.diff

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
@@ -2,7 +2,45 @@
import * as React from 'react';
+import {
+ setDeviceToken,
+ setDeviceTokenActionTypes,
+} from 'lib/actions/device-actions.js';
+import {
+ useDispatchActionPromise,
+ useServerCall,
+} from 'lib/utils/action-utils.js';
+
import electron from '../electron.js';
+import { useSelector } from '../redux/redux-utils.js';
+
+function useCreatePushSubscription(): () => Promise<void> {
+ const publicKey = useSelector(state => state.pushApiPublicKey);
+
+ const dispatchActionPromise = useDispatchActionPromise();
+ const callSetDeviceToken = useServerCall(setDeviceToken);
+
+ return React.useCallback(async () => {
+ if (!publicKey) {
+ return;
+ }
+
+ const workerRegistration = await navigator.serviceWorker?.ready;
+ if (!workerRegistration) {
+ return;
+ }
+
+ const subscription = await workerRegistration.pushManager.subscribe({
+ userVisibleOnly: true,
+ applicationServerKey: publicKey,
+ });
+
+ dispatchActionPromise(
+ setDeviceTokenActionTypes,
+ callSetDeviceToken(JSON.stringify(subscription)),
+ );
+ }, [callSetDeviceToken, dispatchActionPromise, publicKey]);
+}
function PushNotificationsHandler(): React.Node {
React.useEffect(() => {
@@ -18,4 +56,4 @@
return null;
}
-export { PushNotificationsHandler };
+export { PushNotificationsHandler, useCreatePushSubscription };

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 22, 7:48 AM (12 h, 55 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2690593
Default Alt Text
D6819.diff (1 KB)

Event Timeline