ca211de2b4612195e5f9ccf80e1d91b1.json 13.1 KB
{"ast":null,"code":"var _interopRequireDefault=require(\"@babel/runtime/helpers/interopRequireDefault\");Object.defineProperty(exports,\"__esModule\",{value:true});exports.default=void 0;var _toConsumableArray2=_interopRequireDefault(require(\"@babel/runtime/helpers/toConsumableArray\"));var _regenerator=_interopRequireDefault(require(\"@babel/runtime/regenerator\"));var _defineProperty2=_interopRequireDefault(require(\"@babel/runtime/helpers/defineProperty\"));var _objectWithoutProperties2=_interopRequireDefault(require(\"@babel/runtime/helpers/objectWithoutProperties\"));var _uuidJs=_interopRequireDefault(require(\"uuid-js\"));var _ExponentNotificationsHelper=require(\"./ExponentNotificationsHelper.web\");require(\"./ExponentNotifications.fx.web\");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}function transformLocalNotification(notification,tag){var _notification$web=notification.web,web=_notification$web===void 0?{}:_notification$web,abstractNotification=(0,_objectWithoutProperties2.default)(notification,[\"web\"]);tag=web.tag||tag;var nativeNotification=_objectSpread({},abstractNotification,{tag:tag},web,{_isLocal:true});return[nativeNotification.title,nativeNotification];}function generateID(){return _uuidJs.default.create().toString();}function getRegistrationAsync(){var registration;return _regenerator.default.async(function getRegistrationAsync$(_context){while(1){switch(_context.prev=_context.next){case 0:(0,_ExponentNotificationsHelper.guardPermission)();_context.next=3;return _regenerator.default.awrap(navigator.serviceWorker.getRegistration());case 3:registration=_context.sent;if(registration){_context.next=6;break;}throw new Error('Failed to get notification registration!');case 6:return _context.abrupt(\"return\",registration);case 7:case\"end\":return _context.stop();}}});}function getNotificationsAsync(tag){var registration,notifications;return _regenerator.default.async(function getNotificationsAsync$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_context2.next=2;return _regenerator.default.awrap(getRegistrationAsync());case 2:registration=_context2.sent;_context2.next=5;return _regenerator.default.awrap(registration.getNotifications(tag?{tag:tag}:undefined));case 5:notifications=_context2.sent;return _context2.abrupt(\"return\",notifications);case 7:case\"end\":return _context2.stop();}}});}var _default={presentLocalNotification:function presentLocalNotification(notification){var registration,tag;return _regenerator.default.async(function presentLocalNotification$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:_context3.next=2;return _regenerator.default.awrap(getRegistrationAsync());case 2:registration=_context3.sent;tag=generateID();registration.showNotification.apply(registration,(0,_toConsumableArray2.default)(transformLocalNotification(notification,tag)));return _context3.abrupt(\"return\",tag);case 6:case\"end\":return _context3.stop();}}});},scheduleLocalNotification:function scheduleLocalNotification(notification){var options,registration,tag,intervalMs,_args4=arguments;return _regenerator.default.async(function scheduleLocalNotification$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:options=_args4.length>1&&_args4[1]!==undefined?_args4[1]:{};if(!options.intervalMs){_context4.next=10;break;}_context4.next=4;return _regenerator.default.awrap(getRegistrationAsync());case 4:registration=_context4.sent;tag=generateID();setTimeout(function(){registration.showNotification.apply(registration,(0,_toConsumableArray2.default)(transformLocalNotification(notification,tag)));},options.intervalMs);return _context4.abrupt(\"return\",tag);case 10:if(!options.time){_context4.next=15;break;}intervalMs=options.time-Date.now();if(!(intervalMs<0)){_context4.next=14;break;}throw new Error('Expo.Notifications.scheduleLocalNotification(): options.time must be some time in the future.');case 14:return _context4.abrupt(\"return\",this.scheduleLocalNotification(notification,{intervalMs:intervalMs}));case 15:throw new Error(\"Expo.Notifications.scheduleLocalNotification() options \"+JSON.stringify(options,null,2)+\" are not supported yet.\");case 16:case\"end\":return _context4.stop();}}},null,this);},dismissNotification:function dismissNotification(notificationId){var notifications,_iterator,_isArray,_i,_ref,_notification;return _regenerator.default.async(function dismissNotification$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:_context5.next=2;return _regenerator.default.awrap(getNotificationsAsync(notificationId));case 2:notifications=_context5.sent;_iterator=notifications,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[typeof Symbol===\"function\"?typeof Symbol===\"function\"?Symbol.iterator:\"@@iterator\":\"@@iterator\"]();case 4:if(!_isArray){_context5.next=10;break;}if(!(_i>=_iterator.length)){_context5.next=7;break;}return _context5.abrupt(\"break\",18);case 7:_ref=_iterator[_i++];_context5.next=14;break;case 10:_i=_iterator.next();if(!_i.done){_context5.next=13;break;}return _context5.abrupt(\"break\",18);case 13:_ref=_i.value;case 14:_notification=_ref;_notification.close();case 16:_context5.next=4;break;case 18:case\"end\":return _context5.stop();}}});},dismissAllNotifications:function dismissAllNotifications(){return _regenerator.default.async(function dismissAllNotifications$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:this.dismissNotification();case 1:case\"end\":return _context6.stop();}}},null,this);},cancelScheduledNotificationAsync:function cancelScheduledNotificationAsync(notificationId){return _regenerator.default.async(function cancelScheduledNotificationAsync$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:this.dismissNotification(notificationId);case 1:case\"end\":return _context7.stop();}}},null,this);},cancelAllScheduledNotificationsAsync:function cancelAllScheduledNotificationsAsync(){return _regenerator.default.async(function cancelAllScheduledNotificationsAsync$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:this.dismissNotification();case 1:case\"end\":return _context8.stop();}}},null,this);},getExponentPushTokenAsync:function getExponentPushTokenAsync(){return _regenerator.default.async(function getExponentPushTokenAsync$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:_context9.next=2;return _regenerator.default.awrap((0,_ExponentNotificationsHelper.getExponentPushTokenAsync)());case 2:return _context9.abrupt(\"return\",_context9.sent);case 3:case\"end\":return _context9.stop();}}});},getDevicePushTokenAsync:function getDevicePushTokenAsync(){return _regenerator.default.async(function getDevicePushTokenAsync$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:_context10.next=2;return _regenerator.default.awrap((0,_ExponentNotificationsHelper.getDevicePushTokenAsync)());case 2:return _context10.abrupt(\"return\",_context10.sent);case 3:case\"end\":return _context10.stop();}}});}};exports.default=_default;","map":{"version":3,"sources":["../../src/Notifications/ExponentNotifications.web.ts"],"names":[],"mappings":"oiBAAA,uDAGA,8EAOA,0C,yxBAEA,QAAS,CAAA,0BAAT,CACE,YADF,CAEE,GAFF,CAEa,uBAEmC,YAFnC,CAEH,GAFG,CAEH,GAFG,4BAEG,EAFH,mBAEU,oBAFV,uCAEmC,YAFnC,UAGX,GAAG,CAAG,GAAG,CAAC,GAAJ,EAAW,GAAjB,CACA,GAAM,CAAA,kBAAkB,kBACnB,oBADmB,EAEtB,GAAG,CAAH,GAFsB,EAGnB,GAHmB,EAKtB,QAAQ,CAAE,IALY,EAAxB,CAOA,MAAO,CAAC,kBAAkB,CAAC,KAApB,CAA2B,kBAA3B,CAAP,CACD,CAED,QAAS,CAAA,UAAT,EAAmB,CACjB,MAAO,iBAAK,MAAL,GAAc,QAAd,EAAP,CACD,CAED,QAAe,CAAA,oBAAf,mJACE,mDADF,kDAE6B,SAAS,CAAC,aAAV,CAAwB,eAAxB,EAF7B,SAEQ,YAFR,kBAGO,YAHP,8BAIU,IAAI,CAAA,KAAJ,CAAU,0CAAV,CAJV,wCAMS,YANT,gDASA,QAAe,CAAA,qBAAf,CAAqC,GAArC,uNAC6B,oBAAoB,EADjD,SACQ,YADR,mEAE8B,YAAY,CAAC,gBAAb,CAA8B,GAAG,CAAG,CAAE,GAAG,CAAH,GAAF,CAAH,CAAa,SAA9C,CAF9B,SAEQ,aAFR,iDAGS,aAHT,iD,aAMe,CACP,wBADO,mCACkB,YADlB,gNAEgB,oBAAoB,EAFpC,SAEL,YAFK,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cesContent":["import UUID from 'uuid-js';\n\nimport { LocalNotification, LocalNotificationId } from './Notifications.types';\nimport {\n  guardPermission,\n  getExponentPushTokenAsync,\n  getDevicePushTokenAsync,\n} from './ExponentNotificationsHelper.web';\n\n// Register `message`'s event listener (side-effect)\nimport './ExponentNotifications.fx.web';\n\nfunction transformLocalNotification(\n  notification: LocalNotification,\n  tag: string\n): [string, NotificationOptions] {\n  const { web = {}, ...abstractNotification } = notification;\n  tag = web.tag || tag;\n  const nativeNotification = {\n    ...abstractNotification,\n    tag,\n    ...web,\n    // Show that this notification is a local notification\n    _isLocal: true,\n  };\n  return [nativeNotification.title, nativeNotification];\n}\n\nfunction generateID(): string {\n  return UUID.create().toString();\n}\n\nasync function getRegistrationAsync(): Promise<ServiceWorkerRegistration> {\n  guardPermission();\n  const registration = await navigator.serviceWorker.getRegistration();\n  if (!registration) {\n    throw new Error('Failed to get notification registration!');\n  }\n  return registration;\n}\n\nasync function getNotificationsAsync(tag?: string): Promise<Notification[]> {\n  const registration = await getRegistrationAsync();\n  const notifications = await registration.getNotifications(tag ? { tag } : undefined);\n  return notifications;\n}\n\nexport default {\n  async presentLocalNotification(notification: LocalNotification): Promise<LocalNotificationId> {\n    const registration = await getRegistrationAsync();\n    const tag = generateID();\n    registration.showNotification(...transformLocalNotification(notification, tag));\n    return tag;\n  },\n  async scheduleLocalNotification(\n    notification: any,\n    options: {\n      time?: Date | number;\n      repeat?: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';\n      intervalMs?: number;\n    } = {}\n  ): Promise<string> {\n    if (options.intervalMs) {\n      const registration = await getRegistrationAsync();\n      const tag = generateID();\n      setTimeout(() => {\n        registration.showNotification(...transformLocalNotification(notification, tag));\n      }, options.intervalMs);\n      return tag;\n    } else if (options.time) {\n      const intervalMs = (options.time as number) - Date.now();\n      if (intervalMs < 0) {\n        throw new Error(\n          'Expo.Notifications.scheduleLocalNotification(): options.time must be some time in the future.'\n        );\n      }\n      return this.scheduleLocalNotification(notification, {\n        intervalMs,\n      });\n    }\n    throw new Error(\n      `Expo.Notifications.scheduleLocalNotification() options ${JSON.stringify(\n        options,\n        null,\n        2\n      )} are not supported yet.`\n    );\n  },\n\n  async dismissNotification(notificationId?: string): Promise<void> {\n    const notifications = await getNotificationsAsync(notificationId);\n    for (const notification of notifications) {\n      notification.close();\n    }\n  },\n  async dismissAllNotifications(): Promise<void> {\n    this.dismissNotification();\n  },\n  async cancelScheduledNotificationAsync(notificationId: string): Promise<void> {\n    this.dismissNotification(notificationId);\n  },\n  async cancelAllScheduledNotificationsAsync(): Promise<void> {\n    this.dismissNotification();\n  },\n\n  async getExponentPushTokenAsync(): Promise<string> {\n    return await getExponentPushTokenAsync();\n  },\n\n  async getDevicePushTokenAsync(): Promise<{ type: string; data: Object }> {\n    return await getDevicePushTokenAsync();\n  },\n};\n"],"sourceRoot":""},"metadata":{},"sourceType":"script"}