@metamask-previews/notification-services-controller 0.16.0-preview-183f0b5a → 0.16.0-preview-5f45f70f

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 (87) hide show
  1. package/dist/NotificationServicesController/NotificationServicesController.cjs +41 -28
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  3. package/dist/NotificationServicesController/NotificationServicesController.d.cts +3 -3
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  5. package/dist/NotificationServicesController/NotificationServicesController.d.mts +3 -3
  6. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  7. package/dist/NotificationServicesController/NotificationServicesController.mjs +41 -28
  8. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  9. package/dist/NotificationServicesController/index.cjs +1 -0
  10. package/dist/NotificationServicesController/index.cjs.map +1 -1
  11. package/dist/NotificationServicesController/index.d.cts +1 -0
  12. package/dist/NotificationServicesController/index.d.cts.map +1 -1
  13. package/dist/NotificationServicesController/index.d.mts +1 -0
  14. package/dist/NotificationServicesController/index.d.mts.map +1 -1
  15. package/dist/NotificationServicesController/index.mjs +1 -0
  16. package/dist/NotificationServicesController/index.mjs.map +1 -1
  17. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +37 -35
  18. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  19. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +19 -18
  20. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  21. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +19 -18
  22. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  23. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +38 -36
  24. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  25. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs +14 -0
  26. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs.map +1 -0
  27. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts +3 -0
  28. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts.map +1 -0
  29. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts +3 -0
  30. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts.map +1 -0
  31. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs +10 -0
  32. package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs.map +1 -0
  33. package/dist/NotificationServicesPushController/services/services.cjs +1 -24
  34. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  35. package/dist/NotificationServicesPushController/services/services.d.cts +1 -14
  36. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  37. package/dist/NotificationServicesPushController/services/services.d.mts +1 -14
  38. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  39. package/dist/NotificationServicesPushController/services/services.mjs +0 -22
  40. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  41. package/dist/NotificationServicesPushController/types/index.cjs +1 -0
  42. package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
  43. package/dist/NotificationServicesPushController/types/index.d.cts +1 -0
  44. package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
  45. package/dist/NotificationServicesPushController/types/index.d.mts +1 -0
  46. package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
  47. package/dist/NotificationServicesPushController/types/index.mjs +1 -0
  48. package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
  49. package/dist/NotificationServicesPushController/{services/push/index.cjs → types/push-service-interface.cjs} +1 -1
  50. package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +1 -0
  51. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +30 -0
  52. package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +1 -0
  53. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +30 -0
  54. package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +1 -0
  55. package/dist/NotificationServicesPushController/types/push-service-interface.mjs +2 -0
  56. package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +1 -0
  57. package/dist/NotificationServicesPushController/web/index.cjs +8 -0
  58. package/dist/NotificationServicesPushController/web/index.cjs.map +1 -0
  59. package/dist/NotificationServicesPushController/web/index.d.cts +2 -0
  60. package/dist/NotificationServicesPushController/web/index.d.cts.map +1 -0
  61. package/dist/NotificationServicesPushController/web/index.d.mts +2 -0
  62. package/dist/NotificationServicesPushController/web/index.d.mts.map +1 -0
  63. package/dist/NotificationServicesPushController/web/index.mjs +2 -0
  64. package/dist/NotificationServicesPushController/web/index.mjs.map +1 -0
  65. package/dist/NotificationServicesPushController/{services/push/push-web.cjs → web/push-helpers.cjs} +34 -7
  66. package/dist/NotificationServicesPushController/web/push-helpers.cjs.map +1 -0
  67. package/dist/NotificationServicesPushController/web/push-helpers.d.cts +36 -0
  68. package/dist/NotificationServicesPushController/web/push-helpers.d.cts.map +1 -0
  69. package/dist/NotificationServicesPushController/web/push-helpers.d.mts +36 -0
  70. package/dist/NotificationServicesPushController/web/push-helpers.d.mts.map +1 -0
  71. package/dist/NotificationServicesPushController/{services/push/push-web.mjs → web/push-helpers.mjs} +34 -6
  72. package/dist/NotificationServicesPushController/web/push-helpers.mjs.map +1 -0
  73. package/package.json +13 -3
  74. package/push-services/web/package.json +9 -0
  75. package/dist/NotificationServicesPushController/services/push/index.cjs.map +0 -1
  76. package/dist/NotificationServicesPushController/services/push/index.d.cts +0 -6
  77. package/dist/NotificationServicesPushController/services/push/index.d.cts.map +0 -1
  78. package/dist/NotificationServicesPushController/services/push/index.d.mts +0 -6
  79. package/dist/NotificationServicesPushController/services/push/index.d.mts.map +0 -1
  80. package/dist/NotificationServicesPushController/services/push/index.mjs +0 -2
  81. package/dist/NotificationServicesPushController/services/push/index.mjs.map +0 -1
  82. package/dist/NotificationServicesPushController/services/push/push-web.cjs.map +0 -1
  83. package/dist/NotificationServicesPushController/services/push/push-web.d.cts +0 -33
  84. package/dist/NotificationServicesPushController/services/push/push-web.d.cts.map +0 -1
  85. package/dist/NotificationServicesPushController/services/push/push-web.d.mts +0 -33
  86. package/dist/NotificationServicesPushController/services/push/push-web.d.mts.map +0 -1
  87. package/dist/NotificationServicesPushController/services/push/push-web.mjs.map +0 -1
@@ -18,18 +18,32 @@ 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
26
  };
27
27
  const metadata = {
28
+ isPushEnabled: {
29
+ persist: true,
30
+ anonymous: true,
31
+ },
28
32
  fcmToken: {
29
33
  persist: true,
30
34
  anonymous: true,
31
35
  },
32
36
  };
37
+ const defaultPushEnv = {
38
+ apiKey: '',
39
+ authDomain: '',
40
+ storageBucket: '',
41
+ projectId: '',
42
+ messagingSenderId: '',
43
+ appId: '',
44
+ measurementId: '',
45
+ vapidKey: '',
46
+ };
33
47
  /**
34
48
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
35
49
  * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.
@@ -51,29 +65,22 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
51
65
  _NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
52
66
  _NotificationServicesPushController_env.set(this, void 0);
53
67
  _NotificationServicesPushController_config.set(this, void 0);
54
- __classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
68
+ __classPrivateFieldSet(this, _NotificationServicesPushController_env, env ?? defaultPushEnv, "f");
55
69
  __classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
56
70
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
57
71
  }
72
+ setIsPushNotificationsEnabled(isEnabled) {
73
+ this.update((state) => {
74
+ state.isPushEnabled = isEnabled;
75
+ });
76
+ }
58
77
  async subscribeToPushNotifications() {
59
78
  if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
60
79
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
61
80
  __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
62
81
  }
63
82
  try {
64
- __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, await (0, services_1.listenToPushNotifications)({
65
- env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
66
- listenToPushReceived: async (n) => {
67
- this.messagingSystem.publish('NotificationServicesPushController:onNewNotifications', n);
68
- await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationReceived(n);
69
- },
70
- listenToPushClicked: (e, n) => {
71
- if (n) {
72
- this.messagingSystem.publish('NotificationServicesPushController:pushNotificationClicked', n);
73
- }
74
- __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationClicked(e, n);
75
- },
76
- }), "f");
83
+ __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, (await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.subscribeToPushNotifications(__classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"))) ?? undefined, "f");
77
84
  }
78
85
  catch (e) {
79
86
  // Do nothing, we are silently failing if push notification registration fails
@@ -88,10 +95,10 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
88
95
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
89
96
  *
90
97
  * @param UUIDs - An array of UUIDs to enable push notifications for.
91
- * @param fcmToken - The optional FCM token to use for push notifications.
98
+ * @param customFcmToken - The optional custom FCM token to use for push notifications.
92
99
  */
93
- async enablePushNotifications(UUIDs, fcmToken) {
94
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
100
+ async enablePushNotifications(UUIDs, customFcmToken) {
101
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
95
102
  return;
96
103
  }
97
104
  // Handle creating new reg token (if available)
@@ -104,13 +111,14 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
104
111
  bearerToken,
105
112
  triggers: UUIDs,
106
113
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
107
- fcmToken,
108
- createRegToken: push_web_1.createRegToken,
114
+ fcmToken: customFcmToken,
115
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
109
116
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
110
117
  }).catch(() => null);
111
118
  if (regToken) {
112
119
  this.update((state) => {
113
120
  state.fcmToken = regToken;
121
+ state.isPushEnabled = true;
114
122
  });
115
123
  }
116
124
  }
@@ -131,7 +139,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
131
139
  * @param UUIDs - An array of UUIDs for which push notifications should be disabled.
132
140
  */
133
141
  async disablePushNotifications(UUIDs) {
134
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
142
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
135
143
  return;
136
144
  }
137
145
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
@@ -142,27 +150,21 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
142
150
  bearerToken,
143
151
  triggers: UUIDs,
144
152
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
145
- deleteRegToken: push_web_1.deleteRegToken,
153
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
146
154
  regToken: this.state.fcmToken,
147
155
  });
148
156
  }
149
157
  catch (error) {
150
158
  const errorMessage = `Failed to disable push notifications: ${error}`;
151
159
  loglevel_1.default.error(errorMessage);
152
- throw new Error(errorMessage);
153
- }
154
- // Remove the FCM token from the state
155
- if (!isPushNotificationsDisabled) {
156
- return;
157
160
  }
158
161
  // Unsubscribe from push notifications
159
162
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
160
163
  // Update State
161
- if (isPushNotificationsDisabled) {
162
- this.update((state) => {
163
- state.fcmToken = '';
164
- });
165
- }
164
+ this.update((state) => {
165
+ state.fcmToken = '';
166
+ state.isPushEnabled = false;
167
+ });
166
168
  }
167
169
  /**
168
170
  * Updates the triggers for push notifications.
@@ -172,7 +174,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
172
174
  * @param UUIDs - An array of UUIDs that should trigger push notifications.
173
175
  */
174
176
  async updateTriggerPushNotifications(UUIDs) {
175
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
177
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
176
178
  return;
177
179
  }
178
180
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
@@ -181,8 +183,8 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
181
183
  bearerToken,
182
184
  triggers: UUIDs,
183
185
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
184
- createRegToken: push_web_1.createRegToken,
185
- deleteRegToken: push_web_1.deleteRegToken,
186
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
187
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
186
188
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
187
189
  regToken: this.state.fcmToken,
188
190
  });
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAG3B,2DAA0E;AAC1E,sDAK6B;AAG7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAwE/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;;;;;;;;GAQG;AACH,MAAqB,kCAAmC,SAAQ,gCAI/D;IAOC,YAAY,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GAMP;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ;YACR,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,EAAE,GAAG,oBAAY,EAAE,GAAG,KAAK,EAAE;SACrC,CAAC,CAAC;;QAtBL,sEAAqD,SAAS,EAAC;QAE/D,0DAA0B;QAE1B,6DAA0B;QAoBxB,uBAAA,IAAI,2CAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,8CAAW,MAAM,MAAA,CAAC;QAEtB,uBAAA,IAAI,kHAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAmCD,KAAK,CAAC,4BAA4B;QAChC,IAAI,uBAAA,IAAI,mEAAyB,EAAE;YACjC,uBAAA,IAAI,mEAAyB,MAA7B,IAAI,CAA2B,CAAC;YAChC,uBAAA,IAAI,+DAA4B,SAAS,MAAA,CAAC;SAC3C;QAED,IAAI;YACF,uBAAA,IAAI,+DAA4B,MAAM,IAAA,oCAAyB,EAAC;gBAC9D,GAAG,EAAE,uBAAA,IAAI,+C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AAI,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 RestrictedControllerMessenger,\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 type { Types } from '../NotificationServicesController';\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';\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 =\n RestrictedControllerMessenger<\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 *\n * @augments {BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger>}\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n #env: PushNotificationEnv;\n\n #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 (e) {\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;AAG3B,sDAI6B;AAI7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAyE/C,QAAA,YAAY,GAA4C;IACnE,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,EAAE;CACb,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;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,EAAE,EAAE;IACjB,QAAQ,EAAE,EAAE;CACb,CAAC;AAuBF;;;;;;;;GAQG;AACH,MAAqB,kCAAmC,SAAQ,gCAI/D;IAOC,YAAY,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GAOP;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ;YACR,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,EAAE,GAAG,oBAAY,EAAE,GAAG,KAAK,EAAE;SACrC,CAAC,CAAC;;QAvBL,sEAAqD,SAAS,EAAC;QAE/D,0DAA0B;QAE1B,6DAA0B;QAqBxB,uBAAA,IAAI,2CAAQ,GAAG,IAAI,cAAc,MAAA,CAAC;QAClC,uBAAA,IAAI,8CAAW,MAAM,MAAA,CAAC;QAEtB,uBAAA,IAAI,kHAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAmCD,6BAA6B,CAAC,SAAkB;QAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,E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E,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;kBAnEkB,kCAAkC","sourcesContent":["import type {\n RestrictedControllerMessenger,\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 type { Types } from '../NotificationServicesController';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\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 =\n RestrictedControllerMessenger<\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};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n isPushEnabled: {\n persist: true,\n anonymous: true,\n },\n fcmToken: {\n persist: true,\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 * 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\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 *\n * @augments {BaseController<typeof controllerName, NotificationServicesPushControllerState, NotificationServicesPushControllerMessenger>}\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n #env: PushNotificationEnv;\n\n #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state?: Partial<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 }\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 setIsPushNotificationsEnabled(isEnabled: boolean) {\n this.update((state) => {\n state.isPushEnabled = isEnabled;\n });\n }\n\n async subscribeToPushNotifications() {\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 (e) {\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 customFcmToken - The optional custom FCM token to use for push notifications.\n */\n async enablePushNotifications(UUIDs: string[], customFcmToken?: string) {\n if (!this.#config.isPushFeatureEnabled) {\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: customFcmToken,\n createRegToken: this.#config.pushService.createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (regToken) {\n this.update((state) => {\n state.fcmToken = regToken;\n state.isPushEnabled = true;\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.isPushFeatureEnabled) {\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: 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 }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.update((state) => {\n state.fcmToken = '';\n state.isPushEnabled = false;\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.isPushFeatureEnabled) {\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: this.#config.pushService.createRegToken,\n deleteRegToken: this.#config.pushService.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"]}
@@ -3,8 +3,10 @@ import { BaseController } from "@metamask/base-controller";
3
3
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
4
4
  import type { Types } from "../NotificationServicesController/index.cjs";
5
5
  import type { PushNotificationEnv } from "./types/index.cjs";
6
+ import type { PushService } from "./types/push-service-interface.cjs";
6
7
  declare const controllerName = "NotificationServicesPushController";
7
8
  export type NotificationServicesPushControllerState = {
9
+ isPushEnabled: boolean;
8
10
  fcmToken: string;
9
11
  };
10
12
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
@@ -39,26 +41,23 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
39
41
  export type AllowedEvents = never;
40
42
  export type NotificationServicesPushControllerMessenger = RestrictedControllerMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
41
43
  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;
44
+ export type ControllerConfig = {
48
45
  /**
49
- * Must handle when a push notification is received.
50
- * You must call `registration.showNotification` or equivalent to show the notification on web/mobile
46
+ * Global switch to determine to use push notifications
47
+ * Allows us to control Builds on extension (MV2 vs MV3)
51
48
  */
52
- onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
53
- /**
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.
56
- */
57
- onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
49
+ isPushFeatureEnabled?: boolean;
58
50
  /**
59
51
  * determine the config used for push notification services
60
52
  */
61
53
  platform: 'extension' | 'mobile';
54
+ /**
55
+ * Push Service Interface
56
+ * - create reg token
57
+ * - delete reg token
58
+ * - subscribe to push notifications
59
+ */
60
+ pushService: PushService;
62
61
  };
63
62
  /**
64
63
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -73,10 +72,12 @@ export default class NotificationServicesPushController extends BaseController<t
73
72
  #private;
74
73
  constructor({ messenger, state, env, config, }: {
75
74
  messenger: NotificationServicesPushControllerMessenger;
76
- state: NotificationServicesPushControllerState;
77
- env: PushNotificationEnv;
75
+ state?: Partial<NotificationServicesPushControllerState>;
76
+ /** Push Environment is only required for extension */
77
+ env?: PushNotificationEnv;
78
78
  config: ControllerConfig;
79
79
  });
80
+ setIsPushNotificationsEnabled(isEnabled: boolean): void;
80
81
  subscribeToPushNotifications(): Promise<void>;
81
82
  /**
82
83
  * Enables push notifications for the application.
@@ -87,9 +88,9 @@ export default class NotificationServicesPushController extends BaseController<t
87
88
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
88
89
  *
89
90
  * @param UUIDs - An array of UUIDs to enable push notifications for.
90
- * @param fcmToken - The optional FCM token to use for push notifications.
91
+ * @param customFcmToken - The optional custom FCM token to use for push notifications.
91
92
  */
92
- enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
93
+ enablePushNotifications(UUIDs: string[], customFcmToken?: string): Promise<void>;
93
94
  /**
94
95
  * Disables push notifications for the application.
95
96
  * This method handles the process of disabling push notifications by:
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAQ/D,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AAEnD,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;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AyC9C;;;;;;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,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAM/D,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,2CAAuC;AAElE,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,aAAa,EAAE,OAAO,CAAC;IACvB,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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,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,CAAC,EAAE,OAAO,CAAC,uCAAuC,CAAC,CAAC;QACzD,sDAAsD;QACtD,GAAG,CAAC,EAAE,mBAAmB,CAAC;QAC1B,MAAM,EAAE,gBAAgB,CAAC;KAC1B;IA+CD,6BAA6B,CAAC,SAAS,EAAE,OAAO;IAM1C,4BAA4B;IAgBlC;;;;;;;;;;OAUG;IACG,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,cAAc,CAAC,EAAE,MAAM;IAsCtE;;;;;;;;OAQG;IACG,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE;IAkC9C;;;;;;OAMG;IACG,8BAA8B,CAAC,KAAK,EAAE,MAAM,EAAE;CAgCrD"}
@@ -3,8 +3,10 @@ import { BaseController } from "@metamask/base-controller";
3
3
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
4
4
  import type { Types } from "../NotificationServicesController/index.mjs";
5
5
  import type { PushNotificationEnv } from "./types/index.mjs";
6
+ import type { PushService } from "./types/push-service-interface.mjs";
6
7
  declare const controllerName = "NotificationServicesPushController";
7
8
  export type NotificationServicesPushControllerState = {
9
+ isPushEnabled: boolean;
8
10
  fcmToken: string;
9
11
  };
10
12
  export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
@@ -39,26 +41,23 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
39
41
  export type AllowedEvents = never;
40
42
  export type NotificationServicesPushControllerMessenger = RestrictedControllerMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
41
43
  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;
44
+ export type ControllerConfig = {
48
45
  /**
49
- * Must handle when a push notification is received.
50
- * You must call `registration.showNotification` or equivalent to show the notification on web/mobile
46
+ * Global switch to determine to use push notifications
47
+ * Allows us to control Builds on extension (MV2 vs MV3)
51
48
  */
52
- onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
53
- /**
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.
56
- */
57
- onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
49
+ isPushFeatureEnabled?: boolean;
58
50
  /**
59
51
  * determine the config used for push notification services
60
52
  */
61
53
  platform: 'extension' | 'mobile';
54
+ /**
55
+ * Push Service Interface
56
+ * - create reg token
57
+ * - delete reg token
58
+ * - subscribe to push notifications
59
+ */
60
+ pushService: PushService;
62
61
  };
63
62
  /**
64
63
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
@@ -73,10 +72,12 @@ export default class NotificationServicesPushController extends BaseController<t
73
72
  #private;
74
73
  constructor({ messenger, state, env, config, }: {
75
74
  messenger: NotificationServicesPushControllerMessenger;
76
- state: NotificationServicesPushControllerState;
77
- env: PushNotificationEnv;
75
+ state?: Partial<NotificationServicesPushControllerState>;
76
+ /** Push Environment is only required for extension */
77
+ env?: PushNotificationEnv;
78
78
  config: ControllerConfig;
79
79
  });
80
+ setIsPushNotificationsEnabled(isEnabled: boolean): void;
80
81
  subscribeToPushNotifications(): Promise<void>;
81
82
  /**
82
83
  * Enables push notifications for the application.
@@ -87,9 +88,9 @@ export default class NotificationServicesPushController extends BaseController<t
87
88
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
88
89
  *
89
90
  * @param UUIDs - An array of UUIDs to enable push notifications for.
90
- * @param fcmToken - The optional FCM token to use for push notifications.
91
+ * @param customFcmToken - The optional custom FCM token to use for push notifications.
91
92
  */
92
- enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
93
+ enablePushNotifications(UUIDs: string[], customFcmToken?: string): Promise<void>;
93
94
  /**
94
95
  * Disables push notifications for the application.
95
96
  * This method handles the process of disabling push notifications by:
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAQ/D,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AAEnD,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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CAAC,EAAE,KAAK,CAAC,aAAa,KAC/B,IAAI,CAAC;IAEV;;OAEG;IACH,QAAQ,EAAE,WAAW,GAAG,QAAQ,CAAC;CAClC,CAAC;AAEF;;;;;;;;GAQG;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,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.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAGlF,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAM/D,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,2CAAuC;AAElE,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,aAAa,EAAE,OAAO,CAAC;IACvB,QAAQ,EAAE,MAAM,CAA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19,18 +19,32 @@ function $importDefault(module) {
19
19
  import { BaseController } from "@metamask/base-controller";
20
20
  import $log from "loglevel";
21
21
  const log = $importDefault($log);
22
- import { createRegToken, deleteRegToken } from "./services/push/push-web.mjs";
23
- import { activatePushNotifications, deactivatePushNotifications, listenToPushNotifications, updateTriggerPushNotifications } from "./services/services.mjs";
22
+ import { activatePushNotifications, deactivatePushNotifications, updateTriggerPushNotifications } from "./services/services.mjs";
24
23
  const controllerName = 'NotificationServicesPushController';
25
24
  export const defaultState = {
25
+ isPushEnabled: true,
26
26
  fcmToken: '',
27
27
  };
28
28
  const metadata = {
29
+ isPushEnabled: {
30
+ persist: true,
31
+ anonymous: true,
32
+ },
29
33
  fcmToken: {
30
34
  persist: true,
31
35
  anonymous: true,
32
36
  },
33
37
  };
38
+ const defaultPushEnv = {
39
+ apiKey: '',
40
+ authDomain: '',
41
+ storageBucket: '',
42
+ projectId: '',
43
+ messagingSenderId: '',
44
+ appId: '',
45
+ measurementId: '',
46
+ vapidKey: '',
47
+ };
34
48
  /**
35
49
  * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
36
50
  * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.
@@ -52,29 +66,22 @@ class NotificationServicesPushController extends BaseController {
52
66
  _NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
53
67
  _NotificationServicesPushController_env.set(this, void 0);
54
68
  _NotificationServicesPushController_config.set(this, void 0);
55
- __classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
69
+ __classPrivateFieldSet(this, _NotificationServicesPushController_env, env ?? defaultPushEnv, "f");
56
70
  __classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
57
71
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
58
72
  }
73
+ setIsPushNotificationsEnabled(isEnabled) {
74
+ this.update((state) => {
75
+ state.isPushEnabled = isEnabled;
76
+ });
77
+ }
59
78
  async subscribeToPushNotifications() {
60
79
  if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
61
80
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
62
81
  __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
63
82
  }
64
83
  try {
65
- __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, await listenToPushNotifications({
66
- env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
67
- listenToPushReceived: async (n) => {
68
- this.messagingSystem.publish('NotificationServicesPushController:onNewNotifications', n);
69
- await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationReceived(n);
70
- },
71
- listenToPushClicked: (e, n) => {
72
- if (n) {
73
- this.messagingSystem.publish('NotificationServicesPushController:pushNotificationClicked', n);
74
- }
75
- __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationClicked(e, n);
76
- },
77
- }), "f");
84
+ __classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, (await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.subscribeToPushNotifications(__classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"))) ?? undefined, "f");
78
85
  }
79
86
  catch (e) {
80
87
  // Do nothing, we are silently failing if push notification registration fails
@@ -89,10 +96,10 @@ class NotificationServicesPushController extends BaseController {
89
96
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
90
97
  *
91
98
  * @param UUIDs - An array of UUIDs to enable push notifications for.
92
- * @param fcmToken - The optional FCM token to use for push notifications.
99
+ * @param customFcmToken - The optional custom FCM token to use for push notifications.
93
100
  */
94
- async enablePushNotifications(UUIDs, fcmToken) {
95
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
101
+ async enablePushNotifications(UUIDs, customFcmToken) {
102
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
96
103
  return;
97
104
  }
98
105
  // Handle creating new reg token (if available)
@@ -105,13 +112,14 @@ class NotificationServicesPushController extends BaseController {
105
112
  bearerToken,
106
113
  triggers: UUIDs,
107
114
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
108
- fcmToken,
109
- createRegToken,
115
+ fcmToken: customFcmToken,
116
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
110
117
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
111
118
  }).catch(() => null);
112
119
  if (regToken) {
113
120
  this.update((state) => {
114
121
  state.fcmToken = regToken;
122
+ state.isPushEnabled = true;
115
123
  });
116
124
  }
117
125
  }
@@ -132,7 +140,7 @@ class NotificationServicesPushController extends BaseController {
132
140
  * @param UUIDs - An array of UUIDs for which push notifications should be disabled.
133
141
  */
134
142
  async disablePushNotifications(UUIDs) {
135
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
143
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
136
144
  return;
137
145
  }
138
146
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
@@ -143,27 +151,21 @@ class NotificationServicesPushController extends BaseController {
143
151
  bearerToken,
144
152
  triggers: UUIDs,
145
153
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
146
- deleteRegToken,
154
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
147
155
  regToken: this.state.fcmToken,
148
156
  });
149
157
  }
150
158
  catch (error) {
151
159
  const errorMessage = `Failed to disable push notifications: ${error}`;
152
160
  log.error(errorMessage);
153
- throw new Error(errorMessage);
154
- }
155
- // Remove the FCM token from the state
156
- if (!isPushNotificationsDisabled) {
157
- return;
158
161
  }
159
162
  // Unsubscribe from push notifications
160
163
  __classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
161
164
  // Update State
162
- if (isPushNotificationsDisabled) {
163
- this.update((state) => {
164
- state.fcmToken = '';
165
- });
166
- }
165
+ this.update((state) => {
166
+ state.fcmToken = '';
167
+ state.isPushEnabled = false;
168
+ });
167
169
  }
168
170
  /**
169
171
  * Updates the triggers for push notifications.
@@ -173,7 +175,7 @@ class NotificationServicesPushController extends BaseController {
173
175
  * @param UUIDs - An array of UUIDs that should trigger push notifications.
174
176
  */
175
177
  async updateTriggerPushNotifications(UUIDs) {
176
- if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
178
+ if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
177
179
  return;
178
180
  }
179
181
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
@@ -182,8 +184,8 @@ class NotificationServicesPushController extends BaseController {
182
184
  bearerToken,
183
185
  triggers: UUIDs,
184
186
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
185
- createRegToken,
186
- deleteRegToken,
187
+ createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
188
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
187
189
  platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
188
190
  regToken: this.state.fcmToken,
189
191
  });