Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3509738
D6819.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D6819.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 22, 7:48 AM (12 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2690593
Default Alt Text
D6819.diff (1 KB)
Attached To
Mode
D6819: [web] Add hook for creating push subscription
Attached
Detach File
Event Timeline
Log In to Comment