@metamask-previews/notification-services-controller 0.16.0-preview-5f45f70f → 0.16.0-preview-b1518d1
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.
- package/dist/NotificationServicesController/NotificationServicesController.cjs +28 -41
- package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.cts +3 -3
- package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.mts +3 -3
- package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.mjs +28 -41
- package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
- package/dist/NotificationServicesController/index.cjs +0 -1
- package/dist/NotificationServicesController/index.cjs.map +1 -1
- package/dist/NotificationServicesController/index.d.cts +0 -1
- package/dist/NotificationServicesController/index.d.cts.map +1 -1
- package/dist/NotificationServicesController/index.d.mts +0 -1
- package/dist/NotificationServicesController/index.d.mts.map +1 -1
- package/dist/NotificationServicesController/index.mjs +0 -1
- package/dist/NotificationServicesController/index.mjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +35 -37
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +18 -19
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +18 -19
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +36 -38
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
- package/dist/NotificationServicesPushController/{types/push-service-interface.cjs → services/push/index.cjs} +1 -1
- package/dist/NotificationServicesPushController/services/push/index.cjs.map +1 -0
- package/dist/NotificationServicesPushController/services/push/index.d.cts +6 -0
- package/dist/NotificationServicesPushController/services/push/index.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/services/push/index.d.mts +6 -0
- package/dist/NotificationServicesPushController/services/push/index.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/services/push/index.mjs +2 -0
- package/dist/NotificationServicesPushController/services/push/index.mjs.map +1 -0
- package/dist/NotificationServicesPushController/{web/push-helpers.cjs → services/push/push-web.cjs} +7 -34
- package/dist/NotificationServicesPushController/services/push/push-web.cjs.map +1 -0
- package/dist/NotificationServicesPushController/services/push/push-web.d.cts +33 -0
- package/dist/NotificationServicesPushController/services/push/push-web.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/services/push/push-web.d.mts +33 -0
- package/dist/NotificationServicesPushController/services/push/push-web.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/{web/push-helpers.mjs → services/push/push-web.mjs} +6 -34
- package/dist/NotificationServicesPushController/services/push/push-web.mjs.map +1 -0
- package/dist/NotificationServicesPushController/services/services.cjs +24 -1
- package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.cts +14 -1
- package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.mts +14 -1
- package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.mjs +22 -0
- package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
- package/dist/NotificationServicesPushController/types/index.cjs +0 -1
- package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
- package/dist/NotificationServicesPushController/types/index.d.cts +0 -1
- package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/types/index.d.mts +0 -1
- package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/types/index.mjs +0 -1
- package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
- package/package.json +1 -11
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs +0 -14
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs.map +0 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts +0 -3
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts +0 -3
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs +0 -10
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs.map +0 -1
- package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +0 -1
- package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +0 -30
- package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +0 -30
- package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/types/push-service-interface.mjs +0 -2
- package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +0 -1
- package/dist/NotificationServicesPushController/web/index.cjs +0 -8
- package/dist/NotificationServicesPushController/web/index.cjs.map +0 -1
- package/dist/NotificationServicesPushController/web/index.d.cts +0 -2
- package/dist/NotificationServicesPushController/web/index.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/web/index.d.mts +0 -2
- package/dist/NotificationServicesPushController/web/index.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/web/index.mjs +0 -2
- package/dist/NotificationServicesPushController/web/index.mjs.map +0 -1
- package/dist/NotificationServicesPushController/web/push-helpers.cjs.map +0 -1
- package/dist/NotificationServicesPushController/web/push-helpers.d.cts +0 -36
- package/dist/NotificationServicesPushController/web/push-helpers.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/web/push-helpers.d.mts +0 -36
- package/dist/NotificationServicesPushController/web/push-helpers.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/web/push-helpers.mjs.map +0 -1
- package/push-services/web/package.json +0 -9
|
@@ -18,32 +18,18 @@ 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");
|
|
21
22
|
const services_1 = require("./services/services.cjs");
|
|
22
23
|
const controllerName = 'NotificationServicesPushController';
|
|
23
24
|
exports.defaultState = {
|
|
24
|
-
isPushEnabled: true,
|
|
25
25
|
fcmToken: '',
|
|
26
26
|
};
|
|
27
27
|
const metadata = {
|
|
28
|
-
isPushEnabled: {
|
|
29
|
-
persist: true,
|
|
30
|
-
anonymous: true,
|
|
31
|
-
},
|
|
32
28
|
fcmToken: {
|
|
33
29
|
persist: true,
|
|
34
30
|
anonymous: true,
|
|
35
31
|
},
|
|
36
32
|
};
|
|
37
|
-
const defaultPushEnv = {
|
|
38
|
-
apiKey: '',
|
|
39
|
-
authDomain: '',
|
|
40
|
-
storageBucket: '',
|
|
41
|
-
projectId: '',
|
|
42
|
-
messagingSenderId: '',
|
|
43
|
-
appId: '',
|
|
44
|
-
measurementId: '',
|
|
45
|
-
vapidKey: '',
|
|
46
|
-
};
|
|
47
33
|
/**
|
|
48
34
|
* Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
|
|
49
35
|
* This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.
|
|
@@ -65,22 +51,29 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
65
51
|
_NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
|
|
66
52
|
_NotificationServicesPushController_env.set(this, void 0);
|
|
67
53
|
_NotificationServicesPushController_config.set(this, void 0);
|
|
68
|
-
__classPrivateFieldSet(this, _NotificationServicesPushController_env, env
|
|
54
|
+
__classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
|
|
69
55
|
__classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
|
|
70
56
|
__classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
|
|
71
57
|
}
|
|
72
|
-
setIsPushNotificationsEnabled(isEnabled) {
|
|
73
|
-
this.update((state) => {
|
|
74
|
-
state.isPushEnabled = isEnabled;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
58
|
async subscribeToPushNotifications() {
|
|
78
59
|
if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
|
|
79
60
|
__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
|
|
80
61
|
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
|
|
81
62
|
}
|
|
82
63
|
try {
|
|
83
|
-
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe,
|
|
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");
|
|
84
77
|
}
|
|
85
78
|
catch (e) {
|
|
86
79
|
// Do nothing, we are silently failing if push notification registration fails
|
|
@@ -95,10 +88,10 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
95
88
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
96
89
|
*
|
|
97
90
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
98
|
-
* @param
|
|
91
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
99
92
|
*/
|
|
100
|
-
async enablePushNotifications(UUIDs,
|
|
101
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
93
|
+
async enablePushNotifications(UUIDs, fcmToken) {
|
|
94
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
102
95
|
return;
|
|
103
96
|
}
|
|
104
97
|
// Handle creating new reg token (if available)
|
|
@@ -111,14 +104,13 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
111
104
|
bearerToken,
|
|
112
105
|
triggers: UUIDs,
|
|
113
106
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
114
|
-
fcmToken
|
|
115
|
-
createRegToken:
|
|
107
|
+
fcmToken,
|
|
108
|
+
createRegToken: push_web_1.createRegToken,
|
|
116
109
|
platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
|
|
117
110
|
}).catch(() => null);
|
|
118
111
|
if (regToken) {
|
|
119
112
|
this.update((state) => {
|
|
120
113
|
state.fcmToken = regToken;
|
|
121
|
-
state.isPushEnabled = true;
|
|
122
114
|
});
|
|
123
115
|
}
|
|
124
116
|
}
|
|
@@ -139,7 +131,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
139
131
|
* @param UUIDs - An array of UUIDs for which push notifications should be disabled.
|
|
140
132
|
*/
|
|
141
133
|
async disablePushNotifications(UUIDs) {
|
|
142
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
134
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
143
135
|
return;
|
|
144
136
|
}
|
|
145
137
|
const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
|
|
@@ -150,21 +142,27 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
150
142
|
bearerToken,
|
|
151
143
|
triggers: UUIDs,
|
|
152
144
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
153
|
-
deleteRegToken:
|
|
145
|
+
deleteRegToken: push_web_1.deleteRegToken,
|
|
154
146
|
regToken: this.state.fcmToken,
|
|
155
147
|
});
|
|
156
148
|
}
|
|
157
149
|
catch (error) {
|
|
158
150
|
const errorMessage = `Failed to disable push notifications: ${error}`;
|
|
159
151
|
loglevel_1.default.error(errorMessage);
|
|
152
|
+
throw new Error(errorMessage);
|
|
153
|
+
}
|
|
154
|
+
// Remove the FCM token from the state
|
|
155
|
+
if (!isPushNotificationsDisabled) {
|
|
156
|
+
return;
|
|
160
157
|
}
|
|
161
158
|
// Unsubscribe from push notifications
|
|
162
159
|
__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
|
|
163
160
|
// Update State
|
|
164
|
-
|
|
165
|
-
state
|
|
166
|
-
|
|
167
|
-
|
|
161
|
+
if (isPushNotificationsDisabled) {
|
|
162
|
+
this.update((state) => {
|
|
163
|
+
state.fcmToken = '';
|
|
164
|
+
});
|
|
165
|
+
}
|
|
168
166
|
}
|
|
169
167
|
/**
|
|
170
168
|
* Updates the triggers for push notifications.
|
|
@@ -174,7 +172,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
174
172
|
* @param UUIDs - An array of UUIDs that should trigger push notifications.
|
|
175
173
|
*/
|
|
176
174
|
async updateTriggerPushNotifications(UUIDs) {
|
|
177
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
175
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
178
176
|
return;
|
|
179
177
|
}
|
|
180
178
|
const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
|
|
@@ -183,8 +181,8 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
183
181
|
bearerToken,
|
|
184
182
|
triggers: UUIDs,
|
|
185
183
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
186
|
-
createRegToken:
|
|
187
|
-
deleteRegToken:
|
|
184
|
+
createRegToken: push_web_1.createRegToken,
|
|
185
|
+
deleteRegToken: push_web_1.deleteRegToken,
|
|
188
186
|
platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
|
|
189
187
|
regToken: this.state.fcmToken,
|
|
190
188
|
});
|
|
@@ -1 +1 @@
|
|
|
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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oBACpB,KAAK,CAAC,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;;IAzLG,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;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"]}
|
|
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,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nB,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 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"]}
|
|
@@ -3,10 +3,8 @@ 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";
|
|
7
6
|
declare const controllerName = "NotificationServicesPushController";
|
|
8
7
|
export type NotificationServicesPushControllerState = {
|
|
9
|
-
isPushEnabled: boolean;
|
|
10
8
|
fcmToken: string;
|
|
11
9
|
};
|
|
12
10
|
export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
|
|
@@ -41,23 +39,26 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
|
|
|
41
39
|
export type AllowedEvents = never;
|
|
42
40
|
export type NotificationServicesPushControllerMessenger = RestrictedControllerMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
43
41
|
export declare const defaultState: NotificationServicesPushControllerState;
|
|
44
|
-
|
|
42
|
+
type ControllerConfig = {
|
|
45
43
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
44
|
+
* Config to turn on/off push notifications.
|
|
45
|
+
* This is currently linked to MV3 builds on extension.
|
|
48
46
|
*/
|
|
49
|
-
|
|
47
|
+
isPushEnabled: boolean;
|
|
50
48
|
/**
|
|
51
|
-
*
|
|
49
|
+
* Must handle when a push notification is received.
|
|
50
|
+
* You must call `registration.showNotification` or equivalent to show the notification on web/mobile
|
|
52
51
|
*/
|
|
53
|
-
|
|
52
|
+
onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
|
|
54
53
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* - delete reg token
|
|
58
|
-
* - subscribe to push notifications
|
|
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.
|
|
59
56
|
*/
|
|
60
|
-
|
|
57
|
+
onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
|
|
58
|
+
/**
|
|
59
|
+
* determine the config used for push notification services
|
|
60
|
+
*/
|
|
61
|
+
platform: 'extension' | 'mobile';
|
|
61
62
|
};
|
|
62
63
|
/**
|
|
63
64
|
* Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
|
|
@@ -72,12 +73,10 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
72
73
|
#private;
|
|
73
74
|
constructor({ messenger, state, env, config, }: {
|
|
74
75
|
messenger: NotificationServicesPushControllerMessenger;
|
|
75
|
-
state
|
|
76
|
-
|
|
77
|
-
env?: PushNotificationEnv;
|
|
76
|
+
state: NotificationServicesPushControllerState;
|
|
77
|
+
env: PushNotificationEnv;
|
|
78
78
|
config: ControllerConfig;
|
|
79
79
|
});
|
|
80
|
-
setIsPushNotificationsEnabled(isEnabled: boolean): void;
|
|
81
80
|
subscribeToPushNotifications(): Promise<void>;
|
|
82
81
|
/**
|
|
83
82
|
* Enables push notifications for the application.
|
|
@@ -88,9 +87,9 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
88
87
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
89
88
|
*
|
|
90
89
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
91
|
-
* @param
|
|
90
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
92
91
|
*/
|
|
93
|
-
enablePushNotifications(UUIDs: string[],
|
|
92
|
+
enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
|
|
94
93
|
/**
|
|
95
94
|
* Disables push notifications for the application.
|
|
96
95
|
* This method handles the process of disabling push notifications by:
|
package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map
CHANGED
|
@@ -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;
|
|
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;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cA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EAAE;CAgCrD"}
|
|
@@ -3,10 +3,8 @@ 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";
|
|
7
6
|
declare const controllerName = "NotificationServicesPushController";
|
|
8
7
|
export type NotificationServicesPushControllerState = {
|
|
9
|
-
isPushEnabled: boolean;
|
|
10
8
|
fcmToken: string;
|
|
11
9
|
};
|
|
12
10
|
export type NotificationServicesPushControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NotificationServicesPushControllerState>;
|
|
@@ -41,23 +39,26 @@ export type Events = NotificationServicesPushControllerStateChangeEvent | Notifi
|
|
|
41
39
|
export type AllowedEvents = never;
|
|
42
40
|
export type NotificationServicesPushControllerMessenger = RestrictedControllerMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
43
41
|
export declare const defaultState: NotificationServicesPushControllerState;
|
|
44
|
-
|
|
42
|
+
type ControllerConfig = {
|
|
45
43
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
44
|
+
* Config to turn on/off push notifications.
|
|
45
|
+
* This is currently linked to MV3 builds on extension.
|
|
48
46
|
*/
|
|
49
|
-
|
|
47
|
+
isPushEnabled: boolean;
|
|
50
48
|
/**
|
|
51
|
-
*
|
|
49
|
+
* Must handle when a push notification is received.
|
|
50
|
+
* You must call `registration.showNotification` or equivalent to show the notification on web/mobile
|
|
52
51
|
*/
|
|
53
|
-
|
|
52
|
+
onPushNotificationReceived: (notification: Types.INotification) => void | Promise<void>;
|
|
54
53
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* - delete reg token
|
|
58
|
-
* - subscribe to push notifications
|
|
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.
|
|
59
56
|
*/
|
|
60
|
-
|
|
57
|
+
onPushNotificationClicked: (event: NotificationEvent, notification?: Types.INotification) => void;
|
|
58
|
+
/**
|
|
59
|
+
* determine the config used for push notification services
|
|
60
|
+
*/
|
|
61
|
+
platform: 'extension' | 'mobile';
|
|
61
62
|
};
|
|
62
63
|
/**
|
|
63
64
|
* Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
|
|
@@ -72,12 +73,10 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
72
73
|
#private;
|
|
73
74
|
constructor({ messenger, state, env, config, }: {
|
|
74
75
|
messenger: NotificationServicesPushControllerMessenger;
|
|
75
|
-
state
|
|
76
|
-
|
|
77
|
-
env?: PushNotificationEnv;
|
|
76
|
+
state: NotificationServicesPushControllerState;
|
|
77
|
+
env: PushNotificationEnv;
|
|
78
78
|
config: ControllerConfig;
|
|
79
79
|
});
|
|
80
|
-
setIsPushNotificationsEnabled(isEnabled: boolean): void;
|
|
81
80
|
subscribeToPushNotifications(): Promise<void>;
|
|
82
81
|
/**
|
|
83
82
|
* Enables push notifications for the application.
|
|
@@ -88,9 +87,9 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
88
87
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
89
88
|
*
|
|
90
89
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
91
|
-
* @param
|
|
90
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
92
91
|
*/
|
|
93
|
-
enablePushNotifications(UUIDs: string[],
|
|
92
|
+
enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
|
|
94
93
|
/**
|
|
95
94
|
* Disables push notifications for the application.
|
|
96
95
|
* This method handles the process of disabling push notifications by:
|
package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map
CHANGED
|
@@ -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;
|
|
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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,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1B;;;OAGG;IACH,yBAAyB,EAAE,CACzB,KAAK,EAAE,iBAAiB,EACxB,YAAY,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"}
|
|
@@ -19,32 +19,18 @@ 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 {
|
|
22
|
+
import { createRegToken, deleteRegToken } from "./services/push/push-web.mjs";
|
|
23
|
+
import { activatePushNotifications, deactivatePushNotifications, listenToPushNotifications, updateTriggerPushNotifications } from "./services/services.mjs";
|
|
23
24
|
const controllerName = 'NotificationServicesPushController';
|
|
24
25
|
export const defaultState = {
|
|
25
|
-
isPushEnabled: true,
|
|
26
26
|
fcmToken: '',
|
|
27
27
|
};
|
|
28
28
|
const metadata = {
|
|
29
|
-
isPushEnabled: {
|
|
30
|
-
persist: true,
|
|
31
|
-
anonymous: true,
|
|
32
|
-
},
|
|
33
29
|
fcmToken: {
|
|
34
30
|
persist: true,
|
|
35
31
|
anonymous: true,
|
|
36
32
|
},
|
|
37
33
|
};
|
|
38
|
-
const defaultPushEnv = {
|
|
39
|
-
apiKey: '',
|
|
40
|
-
authDomain: '',
|
|
41
|
-
storageBucket: '',
|
|
42
|
-
projectId: '',
|
|
43
|
-
messagingSenderId: '',
|
|
44
|
-
appId: '',
|
|
45
|
-
measurementId: '',
|
|
46
|
-
vapidKey: '',
|
|
47
|
-
};
|
|
48
34
|
/**
|
|
49
35
|
* Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.
|
|
50
36
|
* This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.
|
|
@@ -66,22 +52,29 @@ class NotificationServicesPushController extends BaseController {
|
|
|
66
52
|
_NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
|
|
67
53
|
_NotificationServicesPushController_env.set(this, void 0);
|
|
68
54
|
_NotificationServicesPushController_config.set(this, void 0);
|
|
69
|
-
__classPrivateFieldSet(this, _NotificationServicesPushController_env, env
|
|
55
|
+
__classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
|
|
70
56
|
__classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
|
|
71
57
|
__classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
|
|
72
58
|
}
|
|
73
|
-
setIsPushNotificationsEnabled(isEnabled) {
|
|
74
|
-
this.update((state) => {
|
|
75
|
-
state.isPushEnabled = isEnabled;
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
59
|
async subscribeToPushNotifications() {
|
|
79
60
|
if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
|
|
80
61
|
__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
|
|
81
62
|
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
|
|
82
63
|
}
|
|
83
64
|
try {
|
|
84
|
-
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe,
|
|
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");
|
|
85
78
|
}
|
|
86
79
|
catch (e) {
|
|
87
80
|
// Do nothing, we are silently failing if push notification registration fails
|
|
@@ -96,10 +89,10 @@ class NotificationServicesPushController extends BaseController {
|
|
|
96
89
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
97
90
|
*
|
|
98
91
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
99
|
-
* @param
|
|
92
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
100
93
|
*/
|
|
101
|
-
async enablePushNotifications(UUIDs,
|
|
102
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
94
|
+
async enablePushNotifications(UUIDs, fcmToken) {
|
|
95
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
103
96
|
return;
|
|
104
97
|
}
|
|
105
98
|
// Handle creating new reg token (if available)
|
|
@@ -112,14 +105,13 @@ class NotificationServicesPushController extends BaseController {
|
|
|
112
105
|
bearerToken,
|
|
113
106
|
triggers: UUIDs,
|
|
114
107
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
115
|
-
fcmToken
|
|
116
|
-
createRegToken
|
|
108
|
+
fcmToken,
|
|
109
|
+
createRegToken,
|
|
117
110
|
platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
|
|
118
111
|
}).catch(() => null);
|
|
119
112
|
if (regToken) {
|
|
120
113
|
this.update((state) => {
|
|
121
114
|
state.fcmToken = regToken;
|
|
122
|
-
state.isPushEnabled = true;
|
|
123
115
|
});
|
|
124
116
|
}
|
|
125
117
|
}
|
|
@@ -140,7 +132,7 @@ class NotificationServicesPushController extends BaseController {
|
|
|
140
132
|
* @param UUIDs - An array of UUIDs for which push notifications should be disabled.
|
|
141
133
|
*/
|
|
142
134
|
async disablePushNotifications(UUIDs) {
|
|
143
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
135
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
144
136
|
return;
|
|
145
137
|
}
|
|
146
138
|
const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
|
|
@@ -151,21 +143,27 @@ class NotificationServicesPushController extends BaseController {
|
|
|
151
143
|
bearerToken,
|
|
152
144
|
triggers: UUIDs,
|
|
153
145
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
154
|
-
deleteRegToken
|
|
146
|
+
deleteRegToken,
|
|
155
147
|
regToken: this.state.fcmToken,
|
|
156
148
|
});
|
|
157
149
|
}
|
|
158
150
|
catch (error) {
|
|
159
151
|
const errorMessage = `Failed to disable push notifications: ${error}`;
|
|
160
152
|
log.error(errorMessage);
|
|
153
|
+
throw new Error(errorMessage);
|
|
154
|
+
}
|
|
155
|
+
// Remove the FCM token from the state
|
|
156
|
+
if (!isPushNotificationsDisabled) {
|
|
157
|
+
return;
|
|
161
158
|
}
|
|
162
159
|
// Unsubscribe from push notifications
|
|
163
160
|
__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
|
|
164
161
|
// Update State
|
|
165
|
-
|
|
166
|
-
state
|
|
167
|
-
|
|
168
|
-
|
|
162
|
+
if (isPushNotificationsDisabled) {
|
|
163
|
+
this.update((state) => {
|
|
164
|
+
state.fcmToken = '';
|
|
165
|
+
});
|
|
166
|
+
}
|
|
169
167
|
}
|
|
170
168
|
/**
|
|
171
169
|
* Updates the triggers for push notifications.
|
|
@@ -175,7 +173,7 @@ class NotificationServicesPushController extends BaseController {
|
|
|
175
173
|
* @param UUIDs - An array of UUIDs that should trigger push notifications.
|
|
176
174
|
*/
|
|
177
175
|
async updateTriggerPushNotifications(UUIDs) {
|
|
178
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
176
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
179
177
|
return;
|
|
180
178
|
}
|
|
181
179
|
const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
|
|
@@ -184,8 +182,8 @@ class NotificationServicesPushController extends BaseController {
|
|
|
184
182
|
bearerToken,
|
|
185
183
|
triggers: UUIDs,
|
|
186
184
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
187
|
-
createRegToken
|
|
188
|
-
deleteRegToken
|
|
185
|
+
createRegToken,
|
|
186
|
+
deleteRegToken,
|
|
189
187
|
platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
|
|
190
188
|
regToken: this.state.fcmToken,
|
|
191
189
|
});
|