@metamask/notification-services-controller 0.21.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/CHANGELOG.md +34 -3
  2. package/README.md +35 -1
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs +75 -80
  4. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.d.cts +10 -34
  6. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  7. package/dist/NotificationServicesController/NotificationServicesController.d.mts +10 -34
  8. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  9. package/dist/NotificationServicesController/NotificationServicesController.mjs +76 -81
  10. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  11. package/dist/NotificationServicesController/index.cjs +2 -1
  12. package/dist/NotificationServicesController/index.cjs.map +1 -1
  13. package/dist/NotificationServicesController/index.d.cts +2 -1
  14. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  15. package/dist/NotificationServicesController/index.d.mts +2 -1
  16. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  17. package/dist/NotificationServicesController/index.mjs +2 -1
  18. package/dist/NotificationServicesController/index.mjs.map +1 -1
  19. package/dist/NotificationServicesController/mocks/index.cjs.map +1 -0
  20. package/dist/NotificationServicesController/mocks/index.d.cts.map +1 -0
  21. package/dist/NotificationServicesController/mocks/index.d.mts.map +1 -0
  22. package/dist/NotificationServicesController/mocks/index.mjs.map +1 -0
  23. package/dist/NotificationServicesController/mocks/mock-feature-announcements.cjs.map +1 -0
  24. package/dist/NotificationServicesController/mocks/mock-feature-announcements.d.cts.map +1 -0
  25. package/dist/NotificationServicesController/mocks/mock-feature-announcements.d.mts.map +1 -0
  26. package/dist/NotificationServicesController/mocks/mock-feature-announcements.mjs.map +1 -0
  27. package/dist/NotificationServicesController/mocks/mock-notification-trigger.cjs.map +1 -0
  28. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.cts.map +1 -0
  29. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.mts.map +1 -0
  30. package/dist/NotificationServicesController/mocks/mock-notification-trigger.mjs.map +1 -0
  31. package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.cjs +1 -1
  32. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.cjs.map +1 -0
  33. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.cts.map +1 -0
  34. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.mts.map +1 -0
  35. package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.mjs +1 -1
  36. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.mjs.map +1 -0
  37. package/dist/NotificationServicesController/mocks/mock-raw-notifications.cjs.map +1 -0
  38. package/dist/NotificationServicesController/mocks/mock-raw-notifications.d.cts.map +1 -0
  39. package/dist/NotificationServicesController/mocks/mock-raw-notifications.d.mts.map +1 -0
  40. package/dist/NotificationServicesController/mocks/mock-raw-notifications.mjs.map +1 -0
  41. package/dist/NotificationServicesController/mocks/mock-snap-notification.cjs.map +1 -0
  42. package/dist/NotificationServicesController/mocks/mock-snap-notification.d.cts.map +1 -0
  43. package/dist/NotificationServicesController/mocks/mock-snap-notification.d.mts.map +1 -0
  44. package/dist/NotificationServicesController/mocks/mock-snap-notification.mjs.map +1 -0
  45. package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -0
  46. package/dist/NotificationServicesController/mocks/mockResponses.d.cts.map +1 -0
  47. package/dist/NotificationServicesController/mocks/mockResponses.d.mts.map +1 -0
  48. package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -0
  49. package/dist/NotificationServicesController/services/onchain-notifications.cjs +1 -0
  50. package/dist/NotificationServicesController/services/onchain-notifications.cjs.map +1 -1
  51. package/dist/NotificationServicesController/services/onchain-notifications.d.cts.map +1 -1
  52. package/dist/NotificationServicesController/services/onchain-notifications.d.mts.map +1 -1
  53. package/dist/NotificationServicesController/services/onchain-notifications.mjs +2 -1
  54. package/dist/NotificationServicesController/services/onchain-notifications.mjs.map +1 -1
  55. package/dist/NotificationServicesController/utils/utils.cjs +28 -2
  56. package/dist/NotificationServicesController/utils/utils.cjs.map +1 -1
  57. package/dist/NotificationServicesController/utils/utils.d.cts +11 -1
  58. package/dist/NotificationServicesController/utils/utils.d.cts.map +1 -1
  59. package/dist/NotificationServicesController/utils/utils.d.mts +11 -1
  60. package/dist/NotificationServicesController/utils/utils.d.mts.map +1 -1
  61. package/dist/NotificationServicesController/utils/utils.mjs +26 -1
  62. package/dist/NotificationServicesController/utils/utils.mjs.map +1 -1
  63. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +99 -57
  64. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  65. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +21 -22
  66. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  67. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +21 -22
  68. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  69. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +100 -58
  70. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  71. package/dist/NotificationServicesPushController/index.cjs +1 -1
  72. package/dist/NotificationServicesPushController/index.cjs.map +1 -1
  73. package/dist/NotificationServicesPushController/index.d.cts +1 -1
  74. package/dist/NotificationServicesPushController/index.d.cts.map +1 -1
  75. package/dist/NotificationServicesPushController/index.d.mts +1 -1
  76. package/dist/NotificationServicesPushController/index.d.mts.map +1 -1
  77. package/dist/NotificationServicesPushController/index.mjs +1 -1
  78. package/dist/NotificationServicesPushController/index.mjs.map +1 -1
  79. package/dist/NotificationServicesPushController/mocks/index.cjs.map +1 -0
  80. package/dist/NotificationServicesPushController/mocks/index.d.cts.map +1 -0
  81. package/dist/NotificationServicesPushController/mocks/index.d.mts.map +1 -0
  82. package/dist/NotificationServicesPushController/mocks/index.mjs.map +1 -0
  83. package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.cjs +2 -2
  84. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs.map +1 -0
  85. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts.map +1 -0
  86. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts.map +1 -0
  87. package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.mjs +2 -2
  88. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -0
  89. package/dist/NotificationServicesPushController/services/services.cjs +21 -93
  90. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  91. package/dist/NotificationServicesPushController/services/services.d.cts +8 -29
  92. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  93. package/dist/NotificationServicesPushController/services/services.d.mts +8 -29
  94. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  95. package/dist/NotificationServicesPushController/services/services.mjs +20 -94
  96. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  97. package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
  98. package/dist/NotificationServicesPushController/types/index.d.cts +1 -0
  99. package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
  100. package/dist/NotificationServicesPushController/types/index.d.mts +1 -0
  101. package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
  102. package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
  103. package/dist/NotificationServicesPushController/{services/push/index.cjs → types/push-service-interface.cjs} +1 -1
  104. package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +1 -0
  105. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +30 -0
  106. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +1 -0
  107. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +30 -0
  108. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +1 -0
  109. package/dist/NotificationServicesPushController/types/push-service-interface.mjs +2 -0
  110. package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +1 -0
  111. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs +1 -19
  112. package/dist/NotificationServicesPushController/utils/get-notification-message.cjs.map +1 -1
  113. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts +0 -7
  114. package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts.map +1 -1
  115. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts +0 -7
  116. package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts.map +1 -1
  117. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +0 -17
  118. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs.map +1 -1
  119. package/dist/NotificationServicesPushController/web/index.cjs +8 -0
  120. package/dist/NotificationServicesPushController/web/index.cjs.map +1 -0
  121. package/dist/NotificationServicesPushController/web/index.d.cts +2 -0
  122. package/dist/NotificationServicesPushController/web/index.d.cts.map +1 -0
  123. package/dist/NotificationServicesPushController/web/index.d.mts +2 -0
  124. package/dist/NotificationServicesPushController/web/index.d.mts.map +1 -0
  125. package/dist/NotificationServicesPushController/web/index.mjs +2 -0
  126. package/dist/NotificationServicesPushController/web/index.mjs.map +1 -0
  127. package/dist/NotificationServicesPushController/web/push-utils.cjs +170 -0
  128. package/dist/NotificationServicesPushController/web/push-utils.cjs.map +1 -0
  129. package/dist/NotificationServicesPushController/web/push-utils.d.cts +37 -0
  130. package/dist/NotificationServicesPushController/web/push-utils.d.cts.map +1 -0
  131. package/dist/NotificationServicesPushController/web/push-utils.d.mts +37 -0
  132. package/dist/NotificationServicesPushController/web/push-utils.d.mts.map +1 -0
  133. package/dist/NotificationServicesPushController/web/push-utils.mjs +168 -0
  134. package/dist/NotificationServicesPushController/web/push-utils.mjs.map +1 -0
  135. package/dist/shared/index.cjs +19 -0
  136. package/dist/shared/index.cjs.map +1 -0
  137. package/dist/shared/index.d.cts +3 -0
  138. package/dist/shared/index.d.cts.map +1 -0
  139. package/dist/shared/index.d.mts +3 -0
  140. package/dist/shared/index.d.mts.map +1 -0
  141. package/dist/shared/index.mjs +3 -0
  142. package/dist/shared/index.mjs.map +1 -0
  143. package/dist/shared/is-onchain-notification.cjs +22 -0
  144. package/dist/shared/is-onchain-notification.cjs.map +1 -0
  145. package/dist/shared/is-onchain-notification.d.cts +9 -0
  146. package/dist/shared/is-onchain-notification.d.cts.map +1 -0
  147. package/dist/shared/is-onchain-notification.d.mts +9 -0
  148. package/dist/shared/is-onchain-notification.d.mts.map +1 -0
  149. package/dist/shared/is-onchain-notification.mjs +18 -0
  150. package/dist/shared/is-onchain-notification.mjs.map +1 -0
  151. package/notification-services/mocks/package.json +2 -2
  152. package/package.json +24 -14
  153. package/push-services/mocks/package.json +2 -2
  154. package/push-services/web/package.json +9 -0
  155. package/dist/NotificationServicesController/__fixtures__/index.cjs.map +0 -1
  156. package/dist/NotificationServicesController/__fixtures__/index.d.cts.map +0 -1
  157. package/dist/NotificationServicesController/__fixtures__/index.d.mts.map +0 -1
  158. package/dist/NotificationServicesController/__fixtures__/index.mjs.map +0 -1
  159. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.cjs.map +0 -1
  160. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.d.cts.map +0 -1
  161. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.d.mts.map +0 -1
  162. package/dist/NotificationServicesController/__fixtures__/mock-feature-announcements.mjs.map +0 -1
  163. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.cjs.map +0 -1
  164. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.d.cts.map +0 -1
  165. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.d.mts.map +0 -1
  166. package/dist/NotificationServicesController/__fixtures__/mock-notification-trigger.mjs.map +0 -1
  167. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.cjs.map +0 -1
  168. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.d.cts.map +0 -1
  169. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.d.mts.map +0 -1
  170. package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.mjs.map +0 -1
  171. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.cjs.map +0 -1
  172. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.d.cts.map +0 -1
  173. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.d.mts.map +0 -1
  174. package/dist/NotificationServicesController/__fixtures__/mock-raw-notifications.mjs.map +0 -1
  175. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.cjs.map +0 -1
  176. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.d.cts.map +0 -1
  177. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.d.mts.map +0 -1
  178. package/dist/NotificationServicesController/__fixtures__/mock-snap-notification.mjs.map +0 -1
  179. package/dist/NotificationServicesController/__fixtures__/mockResponses.cjs.map +0 -1
  180. package/dist/NotificationServicesController/__fixtures__/mockResponses.d.cts.map +0 -1
  181. package/dist/NotificationServicesController/__fixtures__/mockResponses.d.mts.map +0 -1
  182. package/dist/NotificationServicesController/__fixtures__/mockResponses.mjs.map +0 -1
  183. package/dist/NotificationServicesController/__fixtures__/mockServices.cjs +0 -56
  184. package/dist/NotificationServicesController/__fixtures__/mockServices.cjs.map +0 -1
  185. package/dist/NotificationServicesController/__fixtures__/mockServices.d.cts +0 -12
  186. package/dist/NotificationServicesController/__fixtures__/mockServices.d.cts.map +0 -1
  187. package/dist/NotificationServicesController/__fixtures__/mockServices.d.mts +0 -12
  188. package/dist/NotificationServicesController/__fixtures__/mockServices.d.mts.map +0 -1
  189. package/dist/NotificationServicesController/__fixtures__/mockServices.mjs +0 -52
  190. package/dist/NotificationServicesController/__fixtures__/mockServices.mjs.map +0 -1
  191. package/dist/NotificationServicesController/__fixtures__/test-utils.cjs +0 -31
  192. package/dist/NotificationServicesController/__fixtures__/test-utils.cjs.map +0 -1
  193. package/dist/NotificationServicesController/__fixtures__/test-utils.d.cts +0 -14
  194. package/dist/NotificationServicesController/__fixtures__/test-utils.d.cts.map +0 -1
  195. package/dist/NotificationServicesController/__fixtures__/test-utils.d.mts +0 -14
  196. package/dist/NotificationServicesController/__fixtures__/test-utils.d.mts.map +0 -1
  197. package/dist/NotificationServicesController/__fixtures__/test-utils.mjs +0 -27
  198. package/dist/NotificationServicesController/__fixtures__/test-utils.mjs.map +0 -1
  199. package/dist/NotificationServicesPushController/__fixtures__/index.cjs.map +0 -1
  200. package/dist/NotificationServicesPushController/__fixtures__/index.d.cts.map +0 -1
  201. package/dist/NotificationServicesPushController/__fixtures__/index.d.mts.map +0 -1
  202. package/dist/NotificationServicesPushController/__fixtures__/index.mjs.map +0 -1
  203. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.cjs.map +0 -1
  204. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.d.cts.map +0 -1
  205. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.d.mts.map +0 -1
  206. package/dist/NotificationServicesPushController/__fixtures__/mockResponse.mjs.map +0 -1
  207. package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs +0 -31
  208. package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs.map +0 -1
  209. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts +0 -9
  210. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts.map +0 -1
  211. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts +0 -9
  212. package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts.map +0 -1
  213. package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs +0 -30
  214. package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs.map +0 -1
  215. package/dist/NotificationServicesPushController/constants.cjs +0 -14
  216. package/dist/NotificationServicesPushController/constants.cjs.map +0 -1
  217. package/dist/NotificationServicesPushController/constants.d.cts +0 -11
  218. package/dist/NotificationServicesPushController/constants.d.cts.map +0 -1
  219. package/dist/NotificationServicesPushController/constants.d.mts +0 -11
  220. package/dist/NotificationServicesPushController/constants.d.mts.map +0 -1
  221. package/dist/NotificationServicesPushController/constants.mjs +0 -11
  222. package/dist/NotificationServicesPushController/constants.mjs.map +0 -1
  223. package/dist/NotificationServicesPushController/services/push/index.cjs.map +0 -1
  224. package/dist/NotificationServicesPushController/services/push/index.d.cts +0 -6
  225. package/dist/NotificationServicesPushController/services/push/index.d.cts.map +0 -1
  226. package/dist/NotificationServicesPushController/services/push/index.d.mts +0 -6
  227. package/dist/NotificationServicesPushController/services/push/index.d.mts.map +0 -1
  228. package/dist/NotificationServicesPushController/services/push/index.mjs +0 -2
  229. package/dist/NotificationServicesPushController/services/push/index.mjs.map +0 -1
  230. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.cjs +0 -0
  231. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.d.cts +0 -0
  232. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.d.mts +0 -0
  233. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/index.mjs +0 -0
  234. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.cjs +0 -0
  235. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.d.cts +0 -0
  236. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.d.mts +0 -0
  237. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-feature-announcements.mjs +0 -0
  238. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.cjs +0 -0
  239. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.d.cts +0 -0
  240. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.d.mts +0 -0
  241. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-trigger.mjs +0 -0
  242. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.d.cts +0 -0
  243. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-notification-user-storage.d.mts +0 -0
  244. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.cjs +0 -0
  245. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.d.cts +0 -0
  246. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.d.mts +0 -0
  247. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-raw-notifications.mjs +0 -0
  248. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.cjs +0 -0
  249. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.d.cts +0 -0
  250. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.d.mts +0 -0
  251. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mock-snap-notification.mjs +0 -0
  252. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.cjs +0 -0
  253. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.d.cts +0 -0
  254. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.d.mts +0 -0
  255. /package/dist/NotificationServicesController/{__fixtures__ → mocks}/mockResponses.mjs +0 -0
  256. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.cjs +0 -0
  257. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.d.cts +0 -0
  258. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.d.mts +0 -0
  259. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/index.mjs +0 -0
  260. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.d.cts +0 -0
  261. /package/dist/NotificationServicesPushController/{__fixtures__ → mocks}/mockResponse.d.mts +0 -0
@@ -13,22 +13,41 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
- var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_getAndAssertBearerToken;
16
+ var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_clearLoadingStates, _NotificationServicesPushController_getAndAssertBearerToken, _NotificationServicesPushController_updatePushState;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.defaultState = void 0;
19
19
  const base_controller_1 = require("@metamask/base-controller");
20
20
  const loglevel_1 = __importDefault(require("loglevel"));
21
- const push_web_1 = require("./services/push/push-web.cjs");
22
21
  const services_1 = require("./services/services.cjs");
23
22
  const controllerName = 'NotificationServicesPushController';
24
23
  exports.defaultState = {
24
+ isPushEnabled: true,
25
25
  fcmToken: '',
26
+ isUpdatingFCMToken: false,
26
27
  };
27
28
  const metadata = {
29
+ isPushEnabled: {
30
+ persist: true,
31
+ anonymous: true,
32
+ },
28
33
  fcmToken: {
29
34
  persist: true,
30
35
  anonymous: true,
31
36
  },
37
+ isUpdatingFCMToken: {
38
+ persist: false,
39
+ anonymous: true,
40
+ },
41
+ };
42
+ const defaultPushEnv = {
43
+ apiKey: '',
44
+ authDomain: '',
45
+ storageBucket: '',
46
+ projectId: '',
47
+ messagingSenderId: '',
48
+ appId: '',
49
+ measurementId: '',
50
+ vapidKey: '',
32
51
  };
33
52
  /**
34
53
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -49,29 +68,21 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
49
68
  _NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
50
69
  _NotificationServicesPushController_env.set(this, void 0);
51
70
  _NotificationServicesPushController_config.set(this, void 0);
52
- __classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
71
+ __classPrivateFieldSet(this, _NotificationServicesPushController_env, env ?? defaultPushEnv, "f");
53
72
  __classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
54
73
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
74
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_clearLoadingStates).call(this);
55
75
  }
56
76
  async subscribeToPushNotifications() {
77
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
78
+ return;
79
+ }
57
80
  if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
58
81
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
59
82
  __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
60
83
  }
61
84
  try {
62
- __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, await (0, services_1.listenToPushNotifications)({
63
- env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
64
- listenToPushReceived: async (n) => {
65
- this.messagingSystem.publish('NotificationServicesPushController:onNewNotifications', n);
66
- await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationReceived(n);
67
- },
68
- listenToPushClicked: (e, n) => {
69
- if (n) {
70
- this.messagingSystem.publish('NotificationServicesPushController:pushNotificationClicked', n);
71
- }
72
- __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationClicked(e, n);
73
- },
74
- }), "f");
85
+ __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, (await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.subscribeToPushNotifications(__classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"))) ?? undefined, "f");
75
86
  }
76
87
  catch {
77
88
  // Do nothing, we are silently failing if push notification registration fails
@@ -86,30 +97,30 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
86
97
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
87
98
  *
88
99
  * @param UUIDs - An array of UUIDs to enable push notifications for.
89
- * @param fcmToken - The optional FCM token to use for push notifications.
90
100
  */
91
- async enablePushNotifications(UUIDs, fcmToken) {
92
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
101
+ async enablePushNotifications(UUIDs) {
102
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
93
103
  return;
94
104
  }
105
+ this.update((state) => {
106
+ state.isUpdatingFCMToken = true;
107
+ });
95
108
  // Handle creating new reg token (if available)
96
109
  try {
97
110
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this).catch(() => null);
98
111
  // If there is a bearer token, lets try to refresh/create new reg token
99
112
  if (bearerToken) {
100
113
  // Activate Push Notifications
101
- const regToken = await (0, services_1.activatePushNotifications)({
114
+ const fcmToken = await (0, services_1.activatePushNotifications)({
102
115
  bearerToken,
103
116
  triggers: UUIDs,
104
117
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
105
- fcmToken,
106
- createRegToken: push_web_1.createRegToken,
118
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
107
119
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
120
+ locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
108
121
  }).catch(() => null);
109
- if (regToken) {
110
- this.update((state) => {
111
- state.fcmToken = regToken;
112
- });
122
+ if (fcmToken) {
123
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'enable', fcmToken });
113
124
  }
114
125
  }
115
126
  }
@@ -117,30 +128,32 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
117
128
  // Do nothing, we are silently failing
118
129
  }
119
130
  // New token created, (re)subscribe to push notifications
120
- await this.subscribeToPushNotifications();
131
+ try {
132
+ await this.subscribeToPushNotifications();
133
+ }
134
+ catch {
135
+ // Do nothing we are silently failing
136
+ }
137
+ this.update((state) => {
138
+ state.isUpdatingFCMToken = false;
139
+ });
121
140
  }
122
141
  /**
123
142
  * Disables push notifications for the application.
124
- * This method handles the process of disabling push notifications by:
125
- * 1. Unregistering the service worker to stop listening for messages.
126
- * 2. Sending a request to the server to unregister the device using the FCM token.
127
- * 3. Removing the FCM token from the state to complete the process.
128
- *
129
- * @param UUIDs - An array of UUIDs for which push notifications should be disabled.
143
+ * This removes the registration token on this device, and ensures we unsubscribe from any listeners
130
144
  */
131
- async disablePushNotifications(UUIDs) {
132
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
145
+ async disablePushNotifications() {
146
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
133
147
  return;
134
148
  }
135
- const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
136
- let isPushNotificationsDisabled;
149
+ this.update((state) => {
150
+ state.isUpdatingFCMToken = true;
151
+ });
137
152
  try {
138
153
  // Send a request to the server to unregister the token/device
139
- isPushNotificationsDisabled = await (0, services_1.deactivatePushNotifications)({
140
- bearerToken,
141
- triggers: UUIDs,
154
+ await (0, services_1.deactivatePushNotifications)({
142
155
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
143
- deleteRegToken: push_web_1.deleteRegToken,
156
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
144
157
  regToken: this.state.fcmToken,
145
158
  });
146
159
  }
@@ -149,18 +162,15 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
149
162
  loglevel_1.default.error(errorMessage);
150
163
  throw new Error(errorMessage);
151
164
  }
152
- // Remove the FCM token from the state
153
- if (!isPushNotificationsDisabled) {
154
- return;
165
+ finally {
166
+ this.update((state) => {
167
+ state.isUpdatingFCMToken = false;
168
+ });
155
169
  }
156
170
  // Unsubscribe from push notifications
157
171
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
158
172
  // Update State
159
- if (isPushNotificationsDisabled) {
160
- this.update((state) => {
161
- state.fcmToken = '';
162
- });
163
- }
173
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'disable' });
164
174
  }
165
175
  /**
166
176
  * Updates the triggers for push notifications.
@@ -170,25 +180,26 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
170
180
  * @param UUIDs - An array of UUIDs that should trigger push notifications.
171
181
  */
172
182
  async updateTriggerPushNotifications(UUIDs) {
173
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
183
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
174
184
  return;
175
185
  }
176
- const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
186
+ this.update((state) => {
187
+ state.isUpdatingFCMToken = true;
188
+ });
177
189
  try {
190
+ const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
178
191
  const { fcmToken } = await (0, services_1.updateTriggerPushNotifications)({
179
192
  bearerToken,
180
193
  triggers: UUIDs,
181
194
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
182
- createRegToken: push_web_1.createRegToken,
183
- deleteRegToken: push_web_1.deleteRegToken,
195
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
196
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
184
197
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
185
- regToken: this.state.fcmToken,
198
+ locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
186
199
  });
187
200
  // update the state with the new FCM token
188
201
  if (fcmToken) {
189
- this.update((state) => {
190
- state.fcmToken = fcmToken;
191
- });
202
+ __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'update', fcmToken });
192
203
  }
193
204
  }
194
205
  catch (error) {
@@ -196,6 +207,11 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
196
207
  loglevel_1.default.error(errorMessage);
197
208
  throw new Error(errorMessage);
198
209
  }
210
+ finally {
211
+ this.update((state) => {
212
+ state.isUpdatingFCMToken = false;
213
+ });
214
+ }
199
215
  }
200
216
  }
201
217
  _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _NotificationServicesPushController_env = new WeakMap(), _NotificationServicesPushController_config = new WeakMap(), _NotificationServicesPushController_instances = new WeakSet(), _NotificationServicesPushController_registerMessageHandlers = function _NotificationServicesPushController_registerMessageHandlers() {
@@ -203,6 +219,10 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
203
219
  this.messagingSystem.registerActionHandler('NotificationServicesPushController:disablePushNotifications', this.disablePushNotifications.bind(this));
204
220
  this.messagingSystem.registerActionHandler('NotificationServicesPushController:updateTriggerPushNotifications', this.updateTriggerPushNotifications.bind(this));
205
221
  this.messagingSystem.registerActionHandler('NotificationServicesPushController:subscribeToPushNotifications', this.subscribeToPushNotifications.bind(this));
222
+ }, _NotificationServicesPushController_clearLoadingStates = function _NotificationServicesPushController_clearLoadingStates() {
223
+ this.update((state) => {
224
+ state.isUpdatingFCMToken = false;
225
+ });
206
226
  }, _NotificationServicesPushController_getAndAssertBearerToken = async function _NotificationServicesPushController_getAndAssertBearerToken() {
207
227
  const bearerToken = await this.messagingSystem.call('AuthenticationController:getBearerToken');
208
228
  if (!bearerToken) {
@@ -210,6 +230,28 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
210
230
  throw new Error('BearerToken token is missing');
211
231
  }
212
232
  return bearerToken;
233
+ }, _NotificationServicesPushController_updatePushState = function _NotificationServicesPushController_updatePushState(command) {
234
+ if (command.type === 'enable') {
235
+ this.update((state) => {
236
+ state.isPushEnabled = true;
237
+ state.fcmToken = command.fcmToken;
238
+ state.isUpdatingFCMToken = false;
239
+ });
240
+ }
241
+ if (command.type === 'disable') {
242
+ this.update((state) => {
243
+ state.isPushEnabled = false;
244
+ state.fcmToken = '';
245
+ state.isUpdatingFCMToken = false;
246
+ });
247
+ }
248
+ if (command.type === 'update') {
249
+ this.update((state) => {
250
+ state.isPushEnabled = true;
251
+ state.fcmToken = command.fcmToken;
252
+ state.isUpdatingFCMToken = false;
253
+ });
254
+ }
213
255
  };
214
256
  exports.default = NotificationServicesPushController;
215
257
  //# sourceMappingURL=NotificationServicesPushController.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAE3B,2DAA0E;AAC1E,sDAK6B;AAI7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAuE/C,QAAA,YAAY,GAA4C;IACnE,QAAQ,EAAE,EAAE;CACb,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,QAAQ,EAAE;QACR,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AAgCF;;;;;;GAMG;AACH,MAAqB,kCAAmC,SAAQ,gCAI/D;IAOC,YAAY,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GAMP;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ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,QAAQ,GAAG,QAAQ,CAAC;gBAC5B,CAAC,CAAC,CAAC;aACJ;SACF;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,YAAY,GAAG,qDACnB,KACF,EAAE,CAAC;YACH,kBAAG,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;SAC/B;IACH,CAAC;CACF;;IAzMG,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,4DAA4D,EAC5D,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,6DAA6D,EAC7D,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,IAAI,CAAC,CACzC,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,mEAAmE,EACnE,IAAI,CAAC,8BAA8B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC/C,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,iEAAiE,EACjE,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC7C,CAAC;AACJ,CAAC,gEAED,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CACjD,yCAAyC,CAC1C,CAAC;IACF,IAAI,CAAC,WAAW,EAAE;QAChB,kBAAG,CAAC,KAAK,CACP,oEAAoE,CACrE,CAAC;QACF,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;kBAlEkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport { createRegToken, deleteRegToken } from './services/push/push-web';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n listenToPushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n fcmToken: '',\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n /**\n * Config to turn on/off push notifications.\n * This is currently linked to MV3 builds on extension.\n */\n isPushEnabled: boolean;\n\n /**\n * Must handle when a push notification is received.\n * You must call `registration.showNotification` or equivalent to show the notification on web/mobile\n */\n onPushNotificationReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n\n /**\n * Must handle when a push notification is clicked.\n * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.\n */\n onPushNotificationClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n};\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n env: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n async subscribeToPushNotifications() {\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe = await listenToPushNotifications({\n env: this.#env,\n listenToPushReceived: async (n) => {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:onNewNotifications',\n n,\n );\n await this.#config.onPushNotificationReceived(n);\n },\n listenToPushClicked: (e, n) => {\n if (n) {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n n,\n );\n }\n\n this.#config.onPushNotificationClicked(e, n);\n },\n });\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n * @param fcmToken - The optional FCM token to use for push notifications.\n */\n async enablePushNotifications(UUIDs: string[], fcmToken?: string) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const regToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n fcmToken,\n createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (regToken) {\n this.update((state) => {\n state.fcmToken = regToken;\n });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n await this.subscribeToPushNotifications();\n }\n\n /**\n * Disables push notifications for the application.\n * This method handles the process of disabling push notifications by:\n * 1. Unregistering the service worker to stop listening for messages.\n * 2. Sending a request to the server to unregister the device using the FCM token.\n * 3. Removing the FCM token from the state to complete the process.\n *\n * @param UUIDs - An array of UUIDs for which push notifications should be disabled.\n */\n async disablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n let isPushNotificationsDisabled: boolean;\n\n try {\n // Send a request to the server to unregister the token/device\n isPushNotificationsDisabled = await deactivatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n\n // Remove the FCM token from the state\n if (!isPushNotificationsDisabled) {\n return;\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n if (isPushNotificationsDisabled) {\n this.update((state) => {\n state.fcmToken = '';\n });\n }\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n deleteRegToken,\n platform: this.#config.platform,\n regToken: this.state.fcmToken,\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.update((state) => {\n state.fcmToken = fcmToken;\n });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAE3B,sDAI6B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAyE/C,QAAA,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;IACZ,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AACF,MAAM,QAAQ,GAA2D;IACvE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,QAAQ,EAAE;QACR,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,IAAI;KAChB;CACF,CAAC;AAEF,MAAM,cAAc,GAAwB;IAC1C,MAAM,EAAE,EAAE;IACV,UAAU,EAAE,EAAE;IACd,aAAa,EAAE,EAAE;IACjB,SAAS,EAAE,EAAE;IACb,iBAAiB,EAAE,EAAE;IACrB,KAAK,EAAE,EAAE;IACT,aAAa,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AC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;YAClC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACnC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;kBApGkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n isUpdatingFCMToken: boolean;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n isUpdatingFCMToken: false,\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n persist: true,\n anonymous: true,\n },\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n isUpdatingFCMToken: {\n persist: false,\n anonymous: true,\n },\n};\n\nconst defaultPushEnv: PushNotificationEnv = {\n apiKey: '',\n authDomain: '',\n storageBucket: '',\n projectId: '',\n messagingSenderId: '',\n appId: '',\n measurementId: '',\n vapidKey: '',\n};\n\nexport type ControllerConfig = {\n /**\n * User locale for server push notifications\n */\n getLocale?: () => string;\n\n /**\n * Global switch to determine to use push notifications\n * Allows us to control Builds on extension (MV2 vs MV3)\n */\n isPushFeatureEnabled?: boolean;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n\n /**\n * Push Service Interface\n * - create reg token\n * - delete reg token\n * - subscribe to push notifications\n */\n pushService: PushService;\n};\n\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n this.#clearLoadingStates();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n #clearLoadingStates(): void {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n state.isPushEnabled = false;\n state.fcmToken = '';\n state.isUpdatingFCMToken = false;\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n state.isUpdatingFCMToken = false;\n });\n }\n }\n\n public async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n */\n public async enablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const fcmToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n }).catch(() => null);\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n try {\n await this.subscribeToPushNotifications();\n } catch {\n // Do nothing we are silently failing\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n public async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n public async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n this.update((state) => {\n state.isUpdatingFCMToken = true;\n });\n\n try {\n const bearerToken = await this.#getAndAssertBearerToken();\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n platform: this.#config.platform,\n locale: this.#config.getLocale?.() ?? 'en',\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.#updatePushState({ type: 'update', fcmToken });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n } finally {\n this.update((state) => {\n state.isUpdatingFCMToken = false;\n });\n }\n }\n}\n"]}
@@ -2,10 +2,13 @@ import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChan
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
4
4
  import type { PushNotificationEnv } from "./types/index.cjs";
5
+ import type { PushService } from "./types/push-service-interface.cjs";
5
6
  import type { Types } from "../NotificationServicesController/index.cjs";
6
7
  declare const controllerName = "NotificationServicesPushController";
7
8
  export type NotificationServicesPushControllerState = {
9
+ isPushEnabled: boolean;
8
10
  fcmToken: string;
11
+ isUpdatingFCMToken: boolean;
9
12
  };
10
13
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
11
14
  export type NotificationServicesPushControllerEnablePushNotificationsAction = {
@@ -39,26 +42,27 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
39
42
  export type AllowedEvents = never;
40
43
  export type NotificationServicesPushControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
41
44
  export declare const defaultState: NotificationServicesPushControllerState;
42
- type ControllerConfig = {
43
- /**
44
- * Config to turn on/off push notifications.
45
- * This is currently linked to MV3 builds on extension.
46
- */
47
- isPushEnabled: boolean;
45
+ export type ControllerConfig = {
48
46
  /**
49
- * Must handle when a push notification is received.
50
- * You must call `registration.showNotification` or equivalent to show the notification on web/mobile
47
+ * User locale for server push notifications
51
48
  */
52
- onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
49
+ getLocale?: () => string;
53
50
  /**
54
- * Must handle when a push notification is clicked.
55
- * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.
51
+ * Global switch to determine to use push notifications
52
+ * Allows us to control Builds on extension (MV2 vs MV3)
56
53
  */
57
- onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
54
+ isPushFeatureEnabled?: boolean;
58
55
  /**
59
56
  * determine the config used for push notification services
60
57
  */
61
58
  platform: 'extension' | 'mobile';
59
+ /**
60
+ * Push Service Interface
61
+ * - create reg token
62
+ * - delete reg token
63
+ * - subscribe to push notifications
64
+ */
65
+ pushService: PushService;
62
66
  };
63
67
  /**
64
68
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -72,7 +76,8 @@ export default class NotificationServicesPushController extends BaseController<t
72
76
  constructor({ messenger, state, env, config, }: {
73
77
  messenger: NotificationServicesPushControllerMessenger;
74
78
  state: NotificationServicesPushControllerState;
75
- env: PushNotificationEnv;
79
+ /** Push Environment is only required for extension */
80
+ env?: PushNotificationEnv;
76
81
  config: ControllerConfig;
77
82
  });
78
83
  subscribeToPushNotifications(): Promise<void>;
@@ -85,19 +90,13 @@ export default class NotificationServicesPushController extends BaseController<t
85
90
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
86
91
  *
87
92
  * @param UUIDs - An array of UUIDs to enable push notifications for.
88
- * @param fcmToken - The optional FCM token to use for push notifications.
89
93
  */
90
- enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
94
+ enablePushNotifications(UUIDs: string[]): Promise<void>;
91
95
  /**
92
96
  * Disables push notifications for the application.
93
- * This method handles the process of disabling push notifications by:
94
- * 1. Unregistering the service worker to stop listening for messages.
95
- * 2. Sending a request to the server to unregister the device using the FCM token.
96
- * 3. Removing the FCM token from the state to complete the process.
97
- *
98
- * @param UUIDs - An array of UUIDs for which push notifications should be disabled.
97
+ * This removes the registration token on this device, and ensures we unsubscribe from any listeners
99
98
  */
100
- disablePushNotifications(UUIDs: string[]): Promise<void>;
99
+ disablePushNotifications(): Promise<void>;
101
100
  /**
102
101
  * Updates the triggers for push notifications.
103
102
  * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAUlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,GAAG,OAAO,cAAc,0BAA0B,CAAC;IACzD,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AACF,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,2BAA2B,CAAC;IAC1D,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AACF,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,GAAG,OAAO,cAAc,iCAAiC,CAAC;IAChE,OAAO,EAAE,kCAAkC,CAAC,gCAAgC,CAAC,CAAC;CAC/E,CAAC;AACJ,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,+BAA+B,CAAC;IAC9D,OAAO,EAAE,kCAAkC,CAAC,8BAA8B,CAAC,CAAC;CAC7E,CAAC;AAEF,MAAM,MAAM,OAAO,GACf,gDAAgD,GAChD,+DAA+D,GAC/D,gEAAgE,GAChE,sEAAsE,GACtE,g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AAmC,SAAQ,cAAc,CAC5E,OAAO,cAAc,EACrB,uCAAuC,EACvC,2CAA2C,CAC5C;;gBAOa,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GACP,EAAE;QACD,SAAS,EAAE,2CAA2C,CAAC;QACvD,KAAK,EAAE,uCAAuC,CAAC;QAC/C,GAAG,EAAE,mBAAmB,CAAC;QACzB,MAAM,EAAE,gBAAgB,CAAC;KAC1B;IA+CK,4BAA4B;IAgClC;;;;;;;;;;OAUG;IACG,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM;IAqChE;;;;;;;;OAQG;IACG,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE;IAyC9C;;;;;;OAMG;IACG,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CAgCrD"}
1
+ {"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAQlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,2CAAuC;AAClE,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,aAAa,EAAE,OAAO,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAAG;IA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MAAM,MAAM,2CAA2C,GAAG,mBAAmB,CAC3E,OAAO,cAAc,EACrB,OAAO,GAAG,cAAc,EACxB,MAAM,GAAG,aAAa,EACtB,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,uCAI1B,CAAC;AA2BF,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,MAAM,CAAC;IAEzB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAE/B;;OAEG;IACH,QAAQ,EAAE,WAAW,GAAG,QAAQ,CAAC;IAEjC;;;;;OAKG;IACH,WAAW,EAAE,WAAW,CAAC;CAC1B,CAAC;AAOF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,kCAAmC,SAAQ,cAAc,CAC5E,OAAO,cAAc,EACrB,uCAAuC,EACvC,2CAA2C,CAC5C;;gBAOa,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GACP,EAAE;QACD,SAAS,EAAE,2CAA2C,CAAC;QACvD,KAAK,EAAE,uCAAuC,CAAC;QAC/C,sDAAsD;QACtD,GAAG,CAAC,EAAE,mBAAmB,CAAC;QAC1B,MAAM,EAAE,gBAAgB,CAAC;KAC1B;IAgFY,4BAA4B;IAoBzC;;;;;;;;;OASG;IACU,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE;IA+CpD;;;OAGG;IACU,wBAAwB;IAmCrC;;;;;;OAMG;IACU,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CAqC5D"}
@@ -2,10 +2,13 @@ import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChan
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
4
4
  import type { PushNotificationEnv } from "./types/index.mjs";
5
+ import type { PushService } from "./types/push-service-interface.mjs";
5
6
  import type { Types } from "../NotificationServicesController/index.mjs";
6
7
  declare const controllerName = "NotificationServicesPushController";
7
8
  export type NotificationServicesPushControllerState = {
9
+ isPushEnabled: boolean;
8
10
  fcmToken: string;
11
+ isUpdatingFCMToken: boolean;
9
12
  };
10
13
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
11
14
  export type NotificationServicesPushControllerEnablePushNotificationsAction = {
@@ -39,26 +42,27 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
39
42
  export type AllowedEvents = never;
40
43
  export type NotificationServicesPushControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
41
44
  export declare const defaultState: NotificationServicesPushControllerState;
42
- type ControllerConfig = {
43
- /**
44
- * Config to turn on/off push notifications.
45
- * This is currently linked to MV3 builds on extension.
46
- */
47
- isPushEnabled: boolean;
45
+ export type ControllerConfig = {
48
46
  /**
49
- * Must handle when a push notification is received.
50
- * You must call `registration.showNotification` or equivalent to show the notification on web/mobile
47
+ * User locale for server push notifications
51
48
  */
52
- onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
49
+ getLocale?: () => string;
53
50
  /**
54
- * Must handle when a push notification is clicked.
55
- * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.
51
+ * Global switch to determine to use push notifications
52
+ * Allows us to control Builds on extension (MV2 vs MV3)
56
53
  */
57
- onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
54
+ isPushFeatureEnabled?: boolean;
58
55
  /**
59
56
  * determine the config used for push notification services
60
57
  */
61
58
  platform: 'extension' | 'mobile';
59
+ /**
60
+ * Push Service Interface
61
+ * - create reg token
62
+ * - delete reg token
63
+ * - subscribe to push notifications
64
+ */
65
+ pushService: PushService;
62
66
  };
63
67
  /**
64
68
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -72,7 +76,8 @@ export default class NotificationServicesPushController extends BaseController<t
72
76
  constructor({ messenger, state, env, config, }: {
73
77
  messenger: NotificationServicesPushControllerMessenger;
74
78
  state: NotificationServicesPushControllerState;
75
- env: PushNotificationEnv;
79
+ /** Push Environment is only required for extension */
80
+ env?: PushNotificationEnv;
76
81
  config: ControllerConfig;
77
82
  });
78
83
  subscribeToPushNotifications(): Promise<void>;
@@ -85,19 +90,13 @@ export default class NotificationServicesPushController extends BaseController<t
85
90
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
86
91
  *
87
92
  * @param UUIDs - An array of UUIDs to enable push notifications for.
88
- * @param fcmToken - The optional FCM token to use for push notifications.
89
93
  */
90
- enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
94
+ enablePushNotifications(UUIDs: string[]): Promise<void>;
91
95
  /**
92
96
  * Disables push notifications for the application.
93
- * This method handles the process of disabling push notifications by:
94
- * 1. Unregistering the service worker to stop listening for messages.
95
- * 2. Sending a request to the server to unregister the device using the FCM token.
96
- * 3. Removing the FCM token from the state to complete the process.
97
- *
98
- * @param UUIDs - An array of UUIDs for which push notifications should be disabled.
97
+ * This removes the registration token on this device, and ensures we unsubscribe from any listeners
99
98
  */
100
- disablePushNotifications(UUIDs: string[]): Promise<void>;
99
+ disablePushNotifications(): Promise<void>;
101
100
  /**
102
101
  * Updates the triggers for push notifications.
103
102
  * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAUlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;A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E,MAAM,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM;IAqChE;;;;;;;;OAQG;IACG,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE;IAyC9C;;;;;;OAMG;IACG,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CAgCrD"}
1
+ {"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAQlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,2CAAuC;AAClE,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,aAAa,EAAE,OAAO,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,GAAG,OAAO,cAAc,0BAA0B,CAAC;IACzD,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AACF,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,2BAA2B,CAAC;IAC1D,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AACF,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,GAAG,OAAO,cAAc,iCAAiC,CAAC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