UNPKG

8.47 kBSource Map (JSON)View Raw
1{"version":3,"file":"ExponentNotificationsHelper.web.js","sourceRoot":"","sources":["../../src/Notifications/ExponentNotificationsHelper.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,MAAM,UAAU,eAAe;IAC7B,IAAI,CAAC,CAAC,cAAc,IAAI,MAAM,CAAC,EAAE;QAC/B,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;KAC1E;IACD,IAAI,CAAC,SAAS,CAAC,aAAa,EAAE;QAC5B,MAAM,IAAI,KAAK,CACb,mKAAmK,CACpK,CAAC;KACH;IACD,IAAI,YAAY,CAAC,UAAU,KAAK,SAAS,EAAE;QACzC,MAAM,IAAI,KAAK,CACb,kGAAkG,CACnG,CAAC;KACH;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,yBAAyB;IAC7C,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE;QACzD,MAAM,IAAI,UAAU,CAClB,yCAAyC,EACzC,2JAA2J,CAC5J,CAAC;KACH;IAED,MAAM,IAAI,GAAG,MAAM,yBAAyB,EAAE,CAAC;IAC/C,MAAM,YAAY,GAAG,IAAI,SAAS,CAAC,QAAQ,CAAC,KAAK,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC/E,MAAM,cAAc,GAA8B;QAChD,QAAQ,EAAE,SAAS,CAAC,cAAc;QAClC,YAAY;QACZ,2EAA2E;QAC3E,KAAK,EAAE,YAAY;QACnB,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;QACjC,IAAI,EAAE,KAAK;KACZ,CAAC;IAEF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,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I,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAE9E,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,WAAW,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE;QACvC,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;KACxC;IACD,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import { CodedError } from '@unimodules/core';\nimport Constants from 'expo-constants';\nimport { Platform } from 'react-native';\n\nexport function guardPermission() {\n if (!('Notification' in window)) {\n throw new Error('The Notification API is not available on this device.');\n }\n if (!navigator.serviceWorker) {\n throw new Error(\n 'Notifications cannot be used because the service worker API is not supported on this device. This might also happen because your web page does not support HTTPS.'\n );\n }\n if (Notification.permission !== 'granted') {\n throw new Error(\n 'Cannot use Notifications without permissions. Please request permissions with `expo-permissions`'\n );\n }\n}\n\nexport async function getExponentPushTokenAsync(): Promise<string> {\n if (!Constants.manifest.owner || !Constants.manifest.slug) {\n throw new CodedError(\n 'E_NOTIFICATIONS_PUSH_WEB_MISSING_CONFIG',\n 'You must provide `owner` and `slug` in `app.json` to use push notifications on web. Learn more: https://docs.expo.io/versions/latest/guides/using-vapid/.'\n );\n }\n\n const data = await _subscribeUserToPushAsync();\n const experienceId = `@${Constants.manifest.owner}/${Constants.manifest.slug}`;\n const tokenArguments: { [key: string]: string } = {\n deviceId: Constants.installationId,\n experienceId,\n // Also uses `experienceId` for `appId` because there's no `appId` for web.\n appId: experienceId,\n deviceToken: JSON.stringify(data),\n type: 'web',\n };\n\n const response = await fetch('https://exp.host/--/api/v2/push/getExpoPushToken', {\n method: 'POST',\n body: JSON.stringify(tokenArguments),\n })\n .then(response => {\n if (!response.ok) {\n throw new Error(response.statusText);\n }\n return response;\n })\n .then(response => response.json())\n .catch(error => {\n throw new CodedError(\n 'E_NOTIFICATIONS_TOKEN_REGISTRATION_FAILED',\n 'The device was unable to register for remote notifications with Expo. (' + error + ')'\n );\n });\n\n return response.data.expoPushToken;\n}\n\nexport async function getDevicePushTokenAsync(): Promise<{ type: string; data: object }> {\n const data = await _subscribeUserToPushAsync();\n return { type: Platform.OS, data };\n}\n\nasync function _subscribeUserToPushAsync(): Promise<object> {\n if (!Constants.manifest.notification || !Constants.manifest.notification.vapidPublicKey) {\n throw new CodedError(\n 'E_NOTIFICATIONS_PUSH_WEB_MISSING_CONFIG',\n 'You must provide `notification.vapidPublicKey` in `app.json` to use push notifications on web. Learn more: https://docs.expo.io/versions/latest/guides/using-vapid/.'\n );\n }\n guardPermission();\n\n const registration = await navigator.serviceWorker.register('/expo-service-worker.js');\n await navigator.serviceWorker.ready;\n\n if (!registration.active) {\n throw new Error(\n 'Notifications might not be working because the service worker API is not active.'\n );\n }\n\n const subscribeOptions = {\n userVisibleOnly: true,\n applicationServerKey: _urlBase64ToUint8Array(Constants.manifest.notification.vapidPublicKey),\n };\n const pushSubscription = await registration.pushManager\n .subscribe(subscribeOptions)\n .catch(error => {\n throw new CodedError(\n 'E_NOTIFICATIONS_PUSH_WEB_TOKEN_REGISTRATION_FAILED',\n 'The device was unable to register for remote notifications with the browser endpoint. (' +\n error +\n ')'\n );\n });\n const pushSubscriptionJson = pushSubscription.toJSON();\n\n const subscriptionObject = {\n endpoint: pushSubscriptionJson.endpoint,\n keys: {\n p256dh: pushSubscriptionJson.keys!.p256dh,\n auth: pushSubscriptionJson.keys!.auth,\n },\n };\n\n // Store notification icon string in service worker.\n // This message is received by `/expo-service-worker.js`.\n // We wrap it with `fromExpoWebClient` to make sure other message\n // will not override content such as `notificationIcon`.\n // https://stackoverflow.com/a/35729334/2603230\n const notificationIcon = (Constants.manifest.notification || {}).icon;\n await registration.active.postMessage(\n JSON.stringify({ fromExpoWebClient: { notificationIcon } })\n );\n\n return subscriptionObject;\n}\n\n// https://github.com/web-push-libs/web-push#using-vapid-key-for-applicationserverkey\nfunction _urlBase64ToUint8Array(base64String: string): Uint8Array {\n const padding = '='.repeat((4 - (base64String.length % 4)) % 4);\n const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');\n\n const rawData = window.atob(base64);\n const outputArray = new Uint8Array(rawData.length);\n\n for (let i = 0; i < rawData.length; ++i) {\n outputArray[i] = rawData.charCodeAt(i);\n }\n return outputArray;\n}\n"]}
\No newline at end of file