{"ast":null,"code":"import _regeneratorRuntime from \"@babel/runtime/regenerator\";\nimport { CodedError } from '@unimodules/core';\nimport Constants from 'expo-constants';\nimport Platform from \"react-native-web/dist/exports/Platform\";\nimport getInstallationIdAsync from \"../environment/getInstallationIdAsync\";\nexport function guardPermission() {\n  if (!('Notification' in window)) {\n    throw new Error('The Notification API is not available on this device.');\n  }\n\n  if (!navigator.serviceWorker) {\n    throw new Error('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('Cannot use Notifications without permissions. Please request permissions with `expo-permissions`');\n  }\n}\nexport function getExponentPushTokenAsync() {\n  var data, experienceId, tokenArguments, response;\n  return _regeneratorRuntime.async(function getExponentPushTokenAsync$(_context) {\n    while (1) {\n      switch (_context.prev = _context.next) {\n        case 0:\n          if (!(!Constants.manifest.owner || !Constants.manifest.slug)) {\n            _context.next = 2;\n            break;\n          }\n\n          throw new CodedError('E_NOTIFICATIONS_PUSH_WEB_MISSING_CONFIG', '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        case 2:\n          _context.next = 4;\n          return _regeneratorRuntime.awrap(_subscribeUserToPushAsync());\n\n        case 4:\n          data = _context.sent;\n          experienceId = \"@\" + Constants.manifest.owner + \"/\" + Constants.manifest.slug;\n          _context.next = 8;\n          return _regeneratorRuntime.awrap(getInstallationIdAsync());\n\n        case 8:\n          _context.t0 = _context.sent;\n          _context.t1 = experienceId;\n          _context.t2 = experienceId;\n          _context.t3 = JSON.stringify(data);\n          tokenArguments = {\n            deviceId: _context.t0,\n            experienceId: _context.t1,\n            appId: _context.t2,\n            deviceToken: _context.t3,\n            type: 'web'\n          };\n          _context.next = 15;\n          return _regeneratorRuntime.awrap(fetch('https://exp.host/--/api/v2/push/getExpoPushToken', {\n            method: 'POST',\n            body: JSON.stringify(tokenArguments)\n          }).then(function (response) {\n            if (!response.ok) {\n              throw new Error(response.statusText);\n            }\n\n            return response;\n          }).then(function (response) {\n            return response.json();\n          }).catch(function (error) {\n            throw new CodedError('E_NOTIFICATIONS_TOKEN_REGISTRATION_FAILED', 'The device was unable to register for remote notifications with Expo. (' + error + ')');\n          }));\n\n        case 15:\n          response = _context.sent;\n          return _context.abrupt(\"return\", response.data.expoPushToken);\n\n        case 17:\n        case \"end\":\n          return _context.stop();\n      }\n    }\n  }, null, null, null, Promise);\n}\nexport function getDevicePushTokenAsync() {\n  var data;\n  return _regeneratorRuntime.async(function getDevicePushTokenAsync$(_context2) {\n    while (1) {\n      switch (_context2.prev = _context2.next) {\n        case 0:\n          _context2.next = 2;\n          return _regeneratorRuntime.awrap(_subscribeUserToPushAsync());\n\n        case 2:\n          data = _context2.sent;\n          return _context2.abrupt(\"return\", {\n            type: Platform.OS,\n            data: data\n          });\n\n        case 4:\n        case \"end\":\n          return _context2.stop();\n      }\n    }\n  }, null, null, null, Promise);\n}\n\nfunction _subscribeUserToPushAsync() {\n  var registration, subscribeOptions, pushSubscription, pushSubscriptionJson, subscriptionObject, notificationIcon;\n  return _regeneratorRuntime.async(function _subscribeUserToPushAsync$(_context3) {\n    while (1) {\n      switch (_context3.prev = _context3.next) {\n        case 0:\n          if (!(!Constants.manifest.notification || !Constants.manifest.notification.vapidPublicKey)) {\n            _context3.next = 2;\n            break;\n          }\n\n          throw new CodedError('E_NOTIFICATIONS_PUSH_WEB_MISSING_CONFIG', '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        case 2:\n          guardPermission();\n          _context3.next = 5;\n          return _regeneratorRuntime.awrap(navigator.serviceWorker.register('/expo-service-worker.js'));\n\n        case 5:\n          registration = _context3.sent;\n          _context3.next = 8;\n          return _regeneratorRuntime.awrap(navigator.serviceWorker.ready);\n\n        case 8:\n          if (registration.active) {\n            _context3.next = 10;\n            break;\n          }\n\n          throw new Error('Notifications might not be working because the service worker API is not active.');\n\n        case 10:\n          subscribeOptions = {\n            userVisibleOnly: true,\n            applicationServerKey: _urlBase64ToUint8Array(Constants.manifest.notification.vapidPublicKey)\n          };\n          _context3.next = 13;\n          return _regeneratorRuntime.awrap(registration.pushManager.subscribe(subscribeOptions).catch(function (error) {\n            throw new CodedError('E_NOTIFICATIONS_PUSH_WEB_TOKEN_REGISTRATION_FAILED', 'The device was unable to register for remote notifications with the browser endpoint. (' + error + ')');\n          }));\n\n        case 13:\n          pushSubscription = _context3.sent;\n          pushSubscriptionJson = pushSubscription.toJSON();\n          subscriptionObject = {\n            endpoint: pushSubscriptionJson.endpoint,\n            keys: {\n              p256dh: pushSubscriptionJson.keys.p256dh,\n              auth: pushSubscriptionJson.keys.auth\n            }\n          };\n          notificationIcon = (Constants.manifest.notification || {}).icon;\n          _context3.next = 19;\n          return _regeneratorRuntime.awrap(registration.active.postMessage(JSON.stringify({\n            fromExpoWebClient: {\n              notificationIcon: notificationIcon\n            }\n          })));\n\n        case 19:\n          return _context3.abrupt(\"return\", subscriptionObject);\n\n        case 20:\n        case \"end\":\n          return _context3.stop();\n      }\n    }\n  }, null, null, null, Promise);\n}\n\nfunction _urlBase64ToUint8Array(base64String) {\n  var padding = '='.repeat((4 - base64String.length % 4) % 4);\n  var base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');\n  var rawData = window.atob(base64);\n  var outputArray = new Uint8Array(rawData.length);\n\n  for (var i = 0; i < rawData.length; ++i) {\n    outputArray[i] = rawData.charCodeAt(i);\n  }\n\n  return outputArray;\n}","map":{"version":3,"sources":["../../src/Notifications/ExponentNotificationsHelper.web.ts"],"names":[],"mappings":";AAAA,SAAS,UAAT,QAA2B,kBAA3B;AACA,OAAO,SAAP,MAAsB,gBAAtB;;AAGA,OAAO,sBAAP;AAEA,OAAM,SAAU,eAAV,GAAyB;AAC7B,MAAI,EAAE,kBAAkB,MAApB,CAAJ,EAAiC;AAC/B,UAAM,IAAI,KAAJ,CAAU,uDAAV,CAAN;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AM,WAAW,GAAG,IAAI,UAAJ,CAAe,OAAO,CAAC,MAAvB,CAApB;;AAEA,OAAK,IAAI,CAAC,GAAG,CAAb,EAAgB,CAAC,GAAG,OAAO,CAAC,MAA5B,EAAoC,EAAE,CAAtC,EAAyC;AACvC,IAAA,WAAW,CAAC,CAAD,CAAX,GAAiB,OAAO,CAAC,UAAR,CAAmB,CAAnB,CAAjB;AACD;;AACD,SAAO,WAAP;AACD","sourcesContent":["import { CodedError } from '@unimodules/core';\nimport Constants from 'expo-constants';\nimport { Platform } from 'react-native';\n\nimport getInstallationIdAsync from '../environment/getInstallationIdAsync';\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: await getInstallationIdAsync(),\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"],"sourceRoot":""},"metadata":{},"sourceType":"module"}