@metamask-previews/notification-services-controller 0.21.0-preview-5575cd05 → 0.21.0-preview-a1eb992b
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 +74 -62
- package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.cts +34 -10
- package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.mts +34 -10
- package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.mjs +74 -62
- 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 +56 -56
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +24 -18
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +24 -18
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +57 -57
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs +13 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.cjs.map +1 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts +1 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts +1 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs +12 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockServices.mjs.map +1 -1
- package/dist/NotificationServicesPushController/constants.cjs +14 -0
- package/dist/NotificationServicesPushController/constants.cjs.map +1 -0
- package/dist/NotificationServicesPushController/constants.d.cts +11 -0
- package/dist/NotificationServicesPushController/constants.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/constants.d.mts +11 -0
- package/dist/NotificationServicesPushController/constants.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/constants.mjs +11 -0
- package/dist/NotificationServicesPushController/constants.mjs.map +1 -0
- 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/services/services.cjs +93 -15
- package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.cts +27 -4
- package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.mts +27 -4
- package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.mjs +94 -14
- package/dist/NotificationServicesPushController/services/services.mjs.map +1 -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.map +1 -1
- package/dist/NotificationServicesPushController/utils/get-notification-message.cjs +19 -1
- package/dist/NotificationServicesPushController/utils/get-notification-message.cjs.map +1 -1
- package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts +7 -0
- package/dist/NotificationServicesPushController/utils/get-notification-message.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts +7 -0
- package/dist/NotificationServicesPushController/utils/get-notification-message.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +17 -0
- package/dist/NotificationServicesPushController/utils/get-notification-message.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-utils.cjs +0 -170
- package/dist/NotificationServicesPushController/web/push-utils.cjs.map +0 -1
- package/dist/NotificationServicesPushController/web/push-utils.d.cts +0 -37
- package/dist/NotificationServicesPushController/web/push-utils.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/web/push-utils.d.mts +0 -37
- package/dist/NotificationServicesPushController/web/push-utils.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/web/push-utils.mjs +0 -168
- package/dist/NotificationServicesPushController/web/push-utils.mjs.map +0 -1
- package/dist/shared/index.cjs +0 -19
- package/dist/shared/index.cjs.map +0 -1
- package/dist/shared/index.d.cts +0 -3
- package/dist/shared/index.d.cts.map +0 -1
- package/dist/shared/index.d.mts +0 -3
- package/dist/shared/index.d.mts.map +0 -1
- package/dist/shared/index.mjs +0 -3
- package/dist/shared/index.mjs.map +0 -1
- package/dist/shared/is-onchain-notification.cjs +0 -22
- package/dist/shared/is-onchain-notification.cjs.map +0 -1
- package/dist/shared/is-onchain-notification.d.cts +0 -9
- package/dist/shared/is-onchain-notification.d.cts.map +0 -1
- package/dist/shared/is-onchain-notification.d.mts +0 -9
- package/dist/shared/is-onchain-notification.d.mts.map +0 -1
- package/dist/shared/is-onchain-notification.mjs +0 -18
- package/dist/shared/is-onchain-notification.mjs.map +0 -1
- package/push-services/web/package.json +0 -150
|
@@ -13,37 +13,23 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_getAndAssertBearerToken
|
|
16
|
+
var _NotificationServicesPushController_instances, _NotificationServicesPushController_pushListenerUnsubscribe, _NotificationServicesPushController_env, _NotificationServicesPushController_config, _NotificationServicesPushController_registerMessageHandlers, _NotificationServicesPushController_getAndAssertBearerToken;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.defaultState = void 0;
|
|
19
19
|
const base_controller_1 = require("@metamask/base-controller");
|
|
20
20
|
const loglevel_1 = __importDefault(require("loglevel"));
|
|
21
|
+
const push_web_1 = require("./services/push/push-web.cjs");
|
|
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.
|
|
@@ -63,20 +49,29 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
63
49
|
_NotificationServicesPushController_pushListenerUnsubscribe.set(this, undefined);
|
|
64
50
|
_NotificationServicesPushController_env.set(this, void 0);
|
|
65
51
|
_NotificationServicesPushController_config.set(this, void 0);
|
|
66
|
-
__classPrivateFieldSet(this, _NotificationServicesPushController_env, env
|
|
52
|
+
__classPrivateFieldSet(this, _NotificationServicesPushController_env, env, "f");
|
|
67
53
|
__classPrivateFieldSet(this, _NotificationServicesPushController_config, config, "f");
|
|
68
54
|
__classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_registerMessageHandlers).call(this);
|
|
69
55
|
}
|
|
70
56
|
async subscribeToPushNotifications() {
|
|
71
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
57
|
if (__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")) {
|
|
75
58
|
__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f").call(this);
|
|
76
59
|
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, undefined, "f");
|
|
77
60
|
}
|
|
78
61
|
try {
|
|
79
|
-
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe,
|
|
62
|
+
__classPrivateFieldSet(this, _NotificationServicesPushController_pushListenerUnsubscribe, await (0, services_1.listenToPushNotifications)({
|
|
63
|
+
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
64
|
+
listenToPushReceived: async (n) => {
|
|
65
|
+
this.messagingSystem.publish('NotificationServicesPushController:onNewNotifications', n);
|
|
66
|
+
await __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationReceived(n);
|
|
67
|
+
},
|
|
68
|
+
listenToPushClicked: (e, n) => {
|
|
69
|
+
if (n) {
|
|
70
|
+
this.messagingSystem.publish('NotificationServicesPushController:pushNotificationClicked', n);
|
|
71
|
+
}
|
|
72
|
+
__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").onPushNotificationClicked(e, n);
|
|
73
|
+
},
|
|
74
|
+
}), "f");
|
|
80
75
|
}
|
|
81
76
|
catch {
|
|
82
77
|
// Do nothing, we are silently failing if push notification registration fails
|
|
@@ -91,9 +86,10 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
91
86
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
92
87
|
*
|
|
93
88
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
89
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
94
90
|
*/
|
|
95
|
-
async enablePushNotifications(UUIDs) {
|
|
96
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
91
|
+
async enablePushNotifications(UUIDs, fcmToken) {
|
|
92
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
97
93
|
return;
|
|
98
94
|
}
|
|
99
95
|
// Handle creating new reg token (if available)
|
|
@@ -102,15 +98,18 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
102
98
|
// If there is a bearer token, lets try to refresh/create new reg token
|
|
103
99
|
if (bearerToken) {
|
|
104
100
|
// Activate Push Notifications
|
|
105
|
-
const
|
|
101
|
+
const regToken = await (0, services_1.activatePushNotifications)({
|
|
106
102
|
bearerToken,
|
|
107
103
|
triggers: UUIDs,
|
|
108
104
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
109
|
-
|
|
105
|
+
fcmToken,
|
|
106
|
+
createRegToken: push_web_1.createRegToken,
|
|
110
107
|
platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
|
|
111
108
|
}).catch(() => null);
|
|
112
|
-
if (
|
|
113
|
-
|
|
109
|
+
if (regToken) {
|
|
110
|
+
this.update((state) => {
|
|
111
|
+
state.fcmToken = regToken;
|
|
112
|
+
});
|
|
114
113
|
}
|
|
115
114
|
}
|
|
116
115
|
}
|
|
@@ -122,17 +121,26 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
122
121
|
}
|
|
123
122
|
/**
|
|
124
123
|
* Disables push notifications for the application.
|
|
125
|
-
* This
|
|
124
|
+
* This method handles the process of disabling push notifications by:
|
|
125
|
+
* 1. Unregistering the service worker to stop listening for messages.
|
|
126
|
+
* 2. Sending a request to the server to unregister the device using the FCM token.
|
|
127
|
+
* 3. Removing the FCM token from the state to complete the process.
|
|
128
|
+
*
|
|
129
|
+
* @param UUIDs - An array of UUIDs for which push notifications should be disabled.
|
|
126
130
|
*/
|
|
127
|
-
async disablePushNotifications() {
|
|
128
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
131
|
+
async disablePushNotifications(UUIDs) {
|
|
132
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
129
133
|
return;
|
|
130
134
|
}
|
|
135
|
+
const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
|
|
136
|
+
let isPushNotificationsDisabled;
|
|
131
137
|
try {
|
|
132
138
|
// Send a request to the server to unregister the token/device
|
|
133
|
-
await (0, services_1.deactivatePushNotifications)({
|
|
139
|
+
isPushNotificationsDisabled = await (0, services_1.deactivatePushNotifications)({
|
|
140
|
+
bearerToken,
|
|
141
|
+
triggers: UUIDs,
|
|
134
142
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
135
|
-
deleteRegToken:
|
|
143
|
+
deleteRegToken: push_web_1.deleteRegToken,
|
|
136
144
|
regToken: this.state.fcmToken,
|
|
137
145
|
});
|
|
138
146
|
}
|
|
@@ -141,10 +149,18 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
141
149
|
loglevel_1.default.error(errorMessage);
|
|
142
150
|
throw new Error(errorMessage);
|
|
143
151
|
}
|
|
152
|
+
// Remove the FCM token from the state
|
|
153
|
+
if (!isPushNotificationsDisabled) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
144
156
|
// Unsubscribe from push notifications
|
|
145
157
|
__classPrivateFieldGet(this, _NotificationServicesPushController_pushListenerUnsubscribe, "f")?.call(this);
|
|
146
158
|
// Update State
|
|
147
|
-
|
|
159
|
+
if (isPushNotificationsDisabled) {
|
|
160
|
+
this.update((state) => {
|
|
161
|
+
state.fcmToken = '';
|
|
162
|
+
});
|
|
163
|
+
}
|
|
148
164
|
}
|
|
149
165
|
/**
|
|
150
166
|
* Updates the triggers for push notifications.
|
|
@@ -154,7 +170,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
154
170
|
* @param UUIDs - An array of UUIDs that should trigger push notifications.
|
|
155
171
|
*/
|
|
156
172
|
async updateTriggerPushNotifications(UUIDs) {
|
|
157
|
-
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").
|
|
173
|
+
if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushEnabled) {
|
|
158
174
|
return;
|
|
159
175
|
}
|
|
160
176
|
const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
|
|
@@ -163,13 +179,16 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
163
179
|
bearerToken,
|
|
164
180
|
triggers: UUIDs,
|
|
165
181
|
env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
|
|
166
|
-
createRegToken:
|
|
167
|
-
deleteRegToken:
|
|
182
|
+
createRegToken: push_web_1.createRegToken,
|
|
183
|
+
deleteRegToken: push_web_1.deleteRegToken,
|
|
168
184
|
platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
|
|
185
|
+
regToken: this.state.fcmToken,
|
|
169
186
|
});
|
|
170
187
|
// update the state with the new FCM token
|
|
171
188
|
if (fcmToken) {
|
|
172
|
-
|
|
189
|
+
this.update((state) => {
|
|
190
|
+
state.fcmToken = fcmToken;
|
|
191
|
+
});
|
|
173
192
|
}
|
|
174
193
|
}
|
|
175
194
|
catch (error) {
|
|
@@ -191,25 +210,6 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
|
|
|
191
210
|
throw new Error('BearerToken token is missing');
|
|
192
211
|
}
|
|
193
212
|
return bearerToken;
|
|
194
|
-
}, _NotificationServicesPushController_updatePushState = function _NotificationServicesPushController_updatePushState(command) {
|
|
195
|
-
if (command.type === 'enable') {
|
|
196
|
-
this.update((state) => {
|
|
197
|
-
state.isPushEnabled = true;
|
|
198
|
-
state.fcmToken = command.fcmToken;
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
if (command.type === 'disable') {
|
|
202
|
-
this.update((state) => {
|
|
203
|
-
state.isPushEnabled = false;
|
|
204
|
-
state.fcmToken = '';
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
if (command.type === 'update') {
|
|
208
|
-
this.update((state) => {
|
|
209
|
-
state.isPushEnabled = true;
|
|
210
|
-
state.fcmToken = command.fcmToken;
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
213
|
};
|
|
214
214
|
exports.default = NotificationServicesPushController;
|
|
215
215
|
//# sourceMappingURL=NotificationServicesPushController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;AAE3D,wDAA2B;AAE3B,sDAI6B;AAK7B,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAwE/C,QAAA,YAAY,GAA4C;IACnE,aAAa,EAAE,IA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,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE;QAC9B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC;YAC5B,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC;QACtB,CAAC,CAAC,CAAC;KACJ;IAED,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACpC,CAAC,CAAC,CAAC;KACJ;AACH,CAAC;kBA1FkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { PushService } from './types/push-service-interface';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n isPushEnabled: boolean;\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n isPushEnabled: true,\n fcmToken: '',\n};\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\ntype StateCommand =\n | { type: 'enable'; fcmToken: string }\n | { type: 'disable' }\n | { type: 'update'; fcmToken: string };\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n /** Push Environment is only required for extension */\n env?: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env ?? defaultPushEnv;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\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 #updatePushState(command: StateCommand) {\n if (command.type === 'enable') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n });\n }\n\n if (command.type === 'disable') {\n this.update((state) => {\n state.isPushEnabled = false;\n state.fcmToken = '';\n });\n }\n\n if (command.type === 'update') {\n this.update((state) => {\n state.isPushEnabled = true;\n state.fcmToken = command.fcmToken;\n });\n }\n }\n\n async subscribeToPushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe =\n (await this.#config.pushService.subscribeToPushNotifications(\n this.#env,\n )) ?? undefined;\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n */\n async enablePushNotifications(UUIDs: 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 fcmToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken: this.#config.pushService.createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (fcmToken) {\n this.#updatePushState({ type: 'enable', fcmToken });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n await this.subscribeToPushNotifications();\n }\n\n /**\n * Disables push notifications for the application.\n * This removes the registration token on this device, and ensures we unsubscribe from any listeners\n */\n async disablePushNotifications() {\n if (!this.#config.isPushFeatureEnabled) {\n return;\n }\n\n try {\n // Send a request to the server to unregister the token/device\n await deactivatePushNotifications({\n env: this.#env,\n deleteRegToken: this.#config.pushService.deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n this.#updatePushState({ type: 'disable' });\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n 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 });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.#updatePushState({ type: 'update', fcmToken });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.cjs","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,+DAA2D;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C,IAAI,CACjD,yCAAyC,CAC1C,CAAC;IACF,IAAI,CAAC,WAAW,EAAE;QAChB,kBAAG,CAAC,KAAK,CACP,oEAAoE,CACrE,CAAC;QACF,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;kBAlEkB,kCAAkC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport { createRegToken, deleteRegToken } from './services/push/push-web';\nimport {\n activatePushNotifications,\n deactivatePushNotifications,\n listenToPushNotifications,\n updateTriggerPushNotifications,\n} from './services/services';\nimport type { PushNotificationEnv } from './types';\nimport type { Types } from '../NotificationServicesController';\n\nconst controllerName = 'NotificationServicesPushController';\n\nexport type NotificationServicesPushControllerState = {\n fcmToken: string;\n};\n\nexport type NotificationServicesPushControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerEnablePushNotificationsAction = {\n type: `${typeof controllerName}:enablePushNotifications`;\n handler: NotificationServicesPushController['enablePushNotifications'];\n};\nexport type NotificationServicesPushControllerDisablePushNotificationsAction = {\n type: `${typeof controllerName}:disablePushNotifications`;\n handler: NotificationServicesPushController['disablePushNotifications'];\n};\nexport type NotificationServicesPushControllerUpdateTriggerPushNotificationsAction =\n {\n type: `${typeof controllerName}:updateTriggerPushNotifications`;\n handler: NotificationServicesPushController['updateTriggerPushNotifications'];\n };\nexport type NotificationServicesPushControllerSubscribeToNotificationsAction = {\n type: `${typeof controllerName}:subscribeToPushNotifications`;\n handler: NotificationServicesPushController['subscribeToPushNotifications'];\n};\n\nexport type Actions =\n | NotificationServicesPushControllerGetStateAction\n | NotificationServicesPushControllerEnablePushNotificationsAction\n | NotificationServicesPushControllerDisablePushNotificationsAction\n | NotificationServicesPushControllerUpdateTriggerPushNotificationsAction\n | NotificationServicesPushControllerSubscribeToNotificationsAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type NotificationServicesPushControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NotificationServicesPushControllerState\n >;\n\nexport type NotificationServicesPushControllerOnNewNotificationEvent = {\n type: `${typeof controllerName}:onNewNotifications`;\n payload: [Types.INotification];\n};\n\nexport type NotificationServicesPushControllerPushNotificationClickedEvent = {\n type: `${typeof controllerName}:pushNotificationClicked`;\n payload: [Types.INotification];\n};\n\nexport type Events =\n | NotificationServicesPushControllerStateChangeEvent\n | NotificationServicesPushControllerOnNewNotificationEvent\n | NotificationServicesPushControllerPushNotificationClickedEvent;\n\nexport type AllowedEvents = never;\n\nexport type NotificationServicesPushControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nexport const defaultState: NotificationServicesPushControllerState = {\n fcmToken: '',\n};\nconst metadata: StateMetadata<NotificationServicesPushControllerState> = {\n fcmToken: {\n persist: true,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n /**\n * Config to turn on/off push notifications.\n * This is currently linked to MV3 builds on extension.\n */\n isPushEnabled: boolean;\n\n /**\n * Must handle when a push notification is received.\n * You must call `registration.showNotification` or equivalent to show the notification on web/mobile\n */\n onPushNotificationReceived: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n\n /**\n * Must handle when a push notification is clicked.\n * You must call `event.notification.close();` or equivalent for closing and opening notification in a new window.\n */\n onPushNotificationClicked: (\n event: NotificationEvent,\n notification?: Types.INotification,\n ) => void;\n\n /**\n * determine the config used for push notification services\n */\n platform: 'extension' | 'mobile';\n};\n\n/**\n * Manages push notifications for the application, including enabling, disabling, and updating triggers for push notifications.\n * This controller integrates with Firebase Cloud Messaging (FCM) to handle the registration and management of push notifications.\n * It is responsible for registering and unregistering the service worker that listens for push notifications,\n * managing the FCM token, and communicating with the server to register or unregister the device for push notifications.\n * Additionally, it provides functionality to update the server with new UUIDs that should trigger push notifications.\n */\nexport default class NotificationServicesPushController extends BaseController<\n typeof controllerName,\n NotificationServicesPushControllerState,\n NotificationServicesPushControllerMessenger\n> {\n #pushListenerUnsubscribe: (() => void) | undefined = undefined;\n\n readonly #env: PushNotificationEnv;\n\n readonly #config: ControllerConfig;\n\n constructor({\n messenger,\n state,\n env,\n config,\n }: {\n messenger: NotificationServicesPushControllerMessenger;\n state: NotificationServicesPushControllerState;\n env: PushNotificationEnv;\n config: ControllerConfig;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n this.#env = env;\n this.#config = config;\n\n this.#registerMessageHandlers();\n }\n\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:enablePushNotifications',\n this.enablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:disablePushNotifications',\n this.disablePushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:updateTriggerPushNotifications',\n this.updateTriggerPushNotifications.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n 'NotificationServicesPushController:subscribeToPushNotifications',\n this.subscribeToPushNotifications.bind(this),\n );\n }\n\n async #getAndAssertBearerToken() {\n const bearerToken = await this.messagingSystem.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!bearerToken) {\n log.error(\n 'Failed to enable push notifications: BearerToken token is missing.',\n );\n throw new Error('BearerToken token is missing');\n }\n\n return bearerToken;\n }\n\n async subscribeToPushNotifications() {\n if (this.#pushListenerUnsubscribe) {\n this.#pushListenerUnsubscribe();\n this.#pushListenerUnsubscribe = undefined;\n }\n\n try {\n this.#pushListenerUnsubscribe = await listenToPushNotifications({\n env: this.#env,\n listenToPushReceived: async (n) => {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:onNewNotifications',\n n,\n );\n await this.#config.onPushNotificationReceived(n);\n },\n listenToPushClicked: (e, n) => {\n if (n) {\n this.messagingSystem.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n n,\n );\n }\n\n this.#config.onPushNotificationClicked(e, n);\n },\n });\n } catch {\n // Do nothing, we are silently failing if push notification registration fails\n }\n }\n\n /**\n * Enables push notifications for the application.\n *\n * This method sets up the necessary infrastructure for handling push notifications by:\n * 1. Registering the service worker to listen for messages.\n * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.\n * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.\n *\n * @param UUIDs - An array of UUIDs to enable push notifications for.\n * @param fcmToken - The optional FCM token to use for push notifications.\n */\n async enablePushNotifications(UUIDs: string[], fcmToken?: string) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n // Handle creating new reg token (if available)\n try {\n const bearerToken = await this.#getAndAssertBearerToken().catch(\n () => null,\n );\n\n // If there is a bearer token, lets try to refresh/create new reg token\n if (bearerToken) {\n // Activate Push Notifications\n const regToken = await activatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n fcmToken,\n createRegToken,\n platform: this.#config.platform,\n }).catch(() => null);\n\n if (regToken) {\n this.update((state) => {\n state.fcmToken = regToken;\n });\n }\n }\n } catch {\n // Do nothing, we are silently failing\n }\n\n // New token created, (re)subscribe to push notifications\n await this.subscribeToPushNotifications();\n }\n\n /**\n * Disables push notifications for the application.\n * This method handles the process of disabling push notifications by:\n * 1. Unregistering the service worker to stop listening for messages.\n * 2. Sending a request to the server to unregister the device using the FCM token.\n * 3. Removing the FCM token from the state to complete the process.\n *\n * @param UUIDs - An array of UUIDs for which push notifications should be disabled.\n */\n async disablePushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n let isPushNotificationsDisabled: boolean;\n\n try {\n // Send a request to the server to unregister the token/device\n isPushNotificationsDisabled = await deactivatePushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n deleteRegToken,\n regToken: this.state.fcmToken,\n });\n } catch (error) {\n const errorMessage = `Failed to disable push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n\n // Remove the FCM token from the state\n if (!isPushNotificationsDisabled) {\n return;\n }\n\n // Unsubscribe from push notifications\n this.#pushListenerUnsubscribe?.();\n\n // Update State\n if (isPushNotificationsDisabled) {\n this.update((state) => {\n state.fcmToken = '';\n });\n }\n }\n\n /**\n * Updates the triggers for push notifications.\n * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.\n * It uses the current FCM token and a BearerToken for authentication.\n *\n * @param UUIDs - An array of UUIDs that should trigger push notifications.\n */\n async updateTriggerPushNotifications(UUIDs: string[]) {\n if (!this.#config.isPushEnabled) {\n return;\n }\n\n const bearerToken = await this.#getAndAssertBearerToken();\n\n try {\n const { fcmToken } = await updateTriggerPushNotifications({\n bearerToken,\n triggers: UUIDs,\n env: this.#env,\n createRegToken,\n deleteRegToken,\n platform: this.#config.platform,\n regToken: this.state.fcmToken,\n });\n\n // update the state with the new FCM token\n if (fcmToken) {\n this.update((state) => {\n state.fcmToken = fcmToken;\n });\n }\n } catch (error) {\n const errorMessage = `Failed to update triggers for push notifications: ${\n error as string\n }`;\n log.error(errorMessage);\n throw new Error(errorMessage);\n }\n }\n}\n"]}
|
|
@@ -2,11 +2,9 @@ import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChan
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
4
4
|
import type { PushNotificationEnv } from "./types/index.cjs";
|
|
5
|
-
import type { PushService } from "./types/push-service-interface.cjs";
|
|
6
5
|
import type { Types } from "../NotificationServicesController/index.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 = RestrictedMessenger<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.
|
|
@@ -71,8 +72,7 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
71
72
|
constructor({ messenger, state, env, config, }: {
|
|
72
73
|
messenger: NotificationServicesPushControllerMessenger;
|
|
73
74
|
state: NotificationServicesPushControllerState;
|
|
74
|
-
|
|
75
|
-
env?: PushNotificationEnv;
|
|
75
|
+
env: PushNotificationEnv;
|
|
76
76
|
config: ControllerConfig;
|
|
77
77
|
});
|
|
78
78
|
subscribeToPushNotifications(): Promise<void>;
|
|
@@ -85,13 +85,19 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
85
85
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
86
86
|
*
|
|
87
87
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
88
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
88
89
|
*/
|
|
89
|
-
enablePushNotifications(UUIDs: string[]): Promise<void>;
|
|
90
|
+
enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
|
|
90
91
|
/**
|
|
91
92
|
* Disables push notifications for the application.
|
|
92
|
-
* This
|
|
93
|
+
* This method handles the process of disabling push notifications by:
|
|
94
|
+
* 1. Unregistering the service worker to stop listening for messages.
|
|
95
|
+
* 2. Sending a request to the server to unregister the device using the FCM token.
|
|
96
|
+
* 3. Removing the FCM token from the state to complete the process.
|
|
97
|
+
*
|
|
98
|
+
* @param UUIDs - An array of UUIDs for which push notifications should be disabled.
|
|
93
99
|
*/
|
|
94
|
-
disablePushNotifications(): Promise<void>;
|
|
100
|
+
disablePushNotifications(UUIDs: string[]): Promise<void>;
|
|
95
101
|
/**
|
|
96
102
|
* Updates the triggers for push notifications.
|
|
97
103
|
* This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.
|
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,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.d.cts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAUlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,GAAG,OAAO,cAAc,0BAA0B,CAAC;IACzD,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AACF,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,2BAA2B,CAAC;IAC1D,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AACF,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,GAAG,OAAO,cAAc,iCAAiC,CAAC;IAChE,OAAO,EAAE,kCAAkC,CAAC,gCAAgC,CAAC,CAAC;CAC/E,CAAC;AACJ,MAAM,MAAM,gEAAgE,GAAG;IAC7E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B,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;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,kCAAmC,SAAQ,cAAc,CAC5E,OAAO,cAAc,EACrB,uCAAuC,EACvC,2CAA2C,CAC5C;;gBAOa,EACV,SAAS,EACT,KAAK,EACL,GAAG,EACH,MAAM,GACP,EAAE;QACD,SAAS,EAAE,2CAA2C,CAAC;QACvD,KAAK,EAAE,uCAAuC,CAAC;QAC/C,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"}
|
|
@@ -2,11 +2,9 @@ import type { RestrictedMessenger, ControllerGetStateAction, ControllerStateChan
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
4
4
|
import type { PushNotificationEnv } from "./types/index.mjs";
|
|
5
|
-
import type { PushService } from "./types/push-service-interface.mjs";
|
|
6
5
|
import type { Types } from "../NotificationServicesController/index.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 = RestrictedMessenger<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.
|
|
@@ -71,8 +72,7 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
71
72
|
constructor({ messenger, state, env, config, }: {
|
|
72
73
|
messenger: NotificationServicesPushControllerMessenger;
|
|
73
74
|
state: NotificationServicesPushControllerState;
|
|
74
|
-
|
|
75
|
-
env?: PushNotificationEnv;
|
|
75
|
+
env: PushNotificationEnv;
|
|
76
76
|
config: ControllerConfig;
|
|
77
77
|
});
|
|
78
78
|
subscribeToPushNotifications(): Promise<void>;
|
|
@@ -85,13 +85,19 @@ export default class NotificationServicesPushController extends BaseController<t
|
|
|
85
85
|
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
|
|
86
86
|
*
|
|
87
87
|
* @param UUIDs - An array of UUIDs to enable push notifications for.
|
|
88
|
+
* @param fcmToken - The optional FCM token to use for push notifications.
|
|
88
89
|
*/
|
|
89
|
-
enablePushNotifications(UUIDs: string[]): Promise<void>;
|
|
90
|
+
enablePushNotifications(UUIDs: string[], fcmToken?: string): Promise<void>;
|
|
90
91
|
/**
|
|
91
92
|
* Disables push notifications for the application.
|
|
92
|
-
* This
|
|
93
|
+
* This method handles the process of disabling push notifications by:
|
|
94
|
+
* 1. Unregistering the service worker to stop listening for messages.
|
|
95
|
+
* 2. Sending a request to the server to unregister the device using the FCM token.
|
|
96
|
+
* 3. Removing the FCM token from the state to complete the process.
|
|
97
|
+
*
|
|
98
|
+
* @param UUIDs - An array of UUIDs for which push notifications should be disabled.
|
|
93
99
|
*/
|
|
94
|
-
disablePushNotifications(): Promise<void>;
|
|
100
|
+
disablePushNotifications(UUIDs: string[]): Promise<void>;
|
|
95
101
|
/**
|
|
96
102
|
* Updates the triggers for push notifications.
|
|
97
103
|
* This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.
|
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,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;
|
|
1
|
+
{"version":3,"file":"NotificationServicesPushController.d.mts","sourceRoot":"","sources":["../../src/NotificationServicesPushController/NotificationServicesPushController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAUlF,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAAgB;AACnD,OAAO,KAAK,EAAE,KAAK,EAAE,oDAA0C;AAE/D,QAAA,MAAM,cAAc,uCAAuC,CAAC;AAE5D,MAAM,MAAM,uCAAuC,GAAG;IACpD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gDAAgD,GAC1D,wBAAwB,CACtB,OAAO,cAAc,EACrB,uCAAuC,CACxC,CAAC;AAEJ,MAAM,MAAM,+DAA+D,GAAG;IAC5E,IAAI,EAAE,GAAG,OAAO,cAAc,0BAA0B,CAAC;IACzD,OAAO,EAAE,kCAAkC,CAAC,yBAAyB,CAAC,CAAC;CACxE,CAAC;AACF,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,2BAA2B,CAAC;IAC1D,OAAO,EAAE,kCAAkC,CAAC,0BAA0B,CAAC,CAAC;CACzE,CAAC;AACF,MAAM,MAAM,sEAAsE,GAChF;IACE,IAAI,EAAE,GAAG,OAAO,cAAc,iCAAiC,CAAC;IAChE,OAAO,EAAE,kCAAkC,CAAC,gCAAgC,CAAC,CAAC;CAC/E,CAAC;AACJ,MAAM,MAAM,gEAAgE,GAAG;IAC7E,IAAI,EAAE,GAAG,OAAO,cAAc,+BAA+B,CAAC;IAC9D,OAAO,EAAE,kCAAkC,CAAC,8BAA8B,CAAC,CAAC;CAC7E,CAAC;AAEF,MAAM,MAAM,OAAO,GACf,gDAAgD,GAChD,+DAA+D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