Skip to content

Commit

Permalink
[expo-notifications][web] Handle errors in SW registration better
Browse files Browse the repository at this point in the history
  • Loading branch information
sjchmiela committed Dec 20, 2019
1 parent 82e54ae commit 29572c7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
18 changes: 13 additions & 5 deletions packages/expo-notifications/build/getDevicePushTokenAsync.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions packages/expo-notifications/src/getDevicePushTokenAsync.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ async function _subscribeUserToPushAsync(): Promise<DevicePushToken['data']> {
}
guardPermission();

const registration = await navigator.serviceWorker.register(Constants.manifest.notification.serviceWorkerPath);
let registration: ServiceWorkerRegistration | null = null;
try {
registration = await navigator.serviceWorker.register(
Constants.manifest.notification.serviceWorkerPath
);
} catch (error) {
throw new Error(
`Notifications might not be working because the service worker (${Constants.manifest.notification.serviceWorkerPath}) couldn't be registered: ${error}`
);
}
await navigator.serviceWorker.ready;

if (!registration.active) {
Expand All @@ -52,16 +61,17 @@ async function _subscribeUserToPushAsync(): Promise<DevicePushToken['data']> {
userVisibleOnly: true,
applicationServerKey: _urlBase64ToUint8Array(Constants.manifest.notification.vapidPublicKey),
};
const pushSubscription = await registration.pushManager
.subscribe(subscribeOptions)
.catch(error => {
throw new CodedError(
'E_NOTIFICATIONS_PUSH_WEB_TOKEN_REGISTRATION_FAILED',
'The device was unable to register for remote notifications with the browser endpoint. (' +
error +
')'
);
});
let pushSubscription: PushSubscription | null = null;
try {
pushSubscription = await registration.pushManager.subscribe(subscribeOptions);
} catch (error) {
throw new CodedError(
'E_NOTIFICATIONS_PUSH_WEB_TOKEN_REGISTRATION_FAILED',
'The device was unable to register for remote notifications with the browser endpoint. (' +
error +
')'
);
}
const pushSubscriptionJson = pushSubscription.toJSON();

const subscriptionObject = {
Expand Down

0 comments on commit 29572c7

Please sign in to comment.