@metamask-previews/notification-services-controller 0.17.0-preview-a7846907 → 0.18.0-preview-442894ce
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/CHANGELOG.md +8 -1
- package/dist/NotificationServicesController/NotificationServicesController.cjs +48 -49
- 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 +48 -49
- package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
- package/dist/NotificationServicesController/index.cjs +1 -0
- package/dist/NotificationServicesController/index.cjs.map +1 -1
- package/dist/NotificationServicesController/index.d.cts +1 -0
- package/dist/NotificationServicesController/index.d.cts.map +1 -1
- package/dist/NotificationServicesController/index.d.mts +1 -0
- package/dist/NotificationServicesController/index.d.mts.map +1 -1
- package/dist/NotificationServicesController/index.mjs +1 -0
- package/dist/NotificationServicesController/index.mjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +43 -40
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +19 -21
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +19 -21
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +44 -41
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs +14 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.cjs.map +1 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts +3 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts +3 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs +10 -0
- package/dist/NotificationServicesPushController/__fixtures__/mockMessenger.mjs.map +1 -0
- package/dist/NotificationServicesPushController/services/services.cjs +3 -28
- package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.cts +1 -15
- package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.mts +1 -15
- package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.mjs +2 -26
- package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
- package/dist/NotificationServicesPushController/types/index.cjs +0 -15
- package/dist/NotificationServicesPushController/types/index.cjs.map +1 -1
- package/dist/NotificationServicesPushController/types/index.d.cts +2 -1
- package/dist/NotificationServicesPushController/types/index.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/types/index.d.mts +2 -1
- package/dist/NotificationServicesPushController/types/index.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/types/index.mjs +1 -1
- package/dist/NotificationServicesPushController/types/index.mjs.map +1 -1
- package/dist/NotificationServicesPushController/{services/push/index.cjs → types/push-service-interface.cjs} +1 -1
- package/dist/NotificationServicesPushController/types/push-service-interface.cjs.map +1 -0
- package/dist/NotificationServicesPushController/types/push-service-interface.d.cts +30 -0
- package/dist/NotificationServicesPushController/types/push-service-interface.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/types/push-service-interface.d.mts +30 -0
- package/dist/NotificationServicesPushController/types/push-service-interface.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/types/push-service-interface.mjs +2 -0
- package/dist/NotificationServicesPushController/types/push-service-interface.mjs.map +1 -0
- package/dist/NotificationServicesPushController/web/index.cjs +8 -0
- package/dist/NotificationServicesPushController/web/index.cjs.map +1 -0
- package/dist/NotificationServicesPushController/web/index.d.cts +2 -0
- package/dist/NotificationServicesPushController/web/index.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/web/index.d.mts +2 -0
- package/dist/NotificationServicesPushController/web/index.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/web/index.mjs +2 -0
- package/dist/NotificationServicesPushController/web/index.mjs.map +1 -0
- package/dist/NotificationServicesPushController/{services/push/push-web.cjs → web/push-helpers.cjs} +37 -8
- package/dist/NotificationServicesPushController/web/push-helpers.cjs.map +1 -0
- package/dist/NotificationServicesPushController/web/push-helpers.d.cts +37 -0
- package/dist/NotificationServicesPushController/web/push-helpers.d.cts.map +1 -0
- package/dist/NotificationServicesPushController/web/push-helpers.d.mts +37 -0
- package/dist/NotificationServicesPushController/web/push-helpers.d.mts.map +1 -0
- package/dist/NotificationServicesPushController/{services/push/push-web.mjs → web/push-helpers.mjs} +37 -7
- package/dist/NotificationServicesPushController/web/push-helpers.mjs.map +1 -0
- package/package.json +14 -4
- package/push-services/web/package.json +9 -0
- package/dist/NotificationServicesPushController/services/push/index.cjs.map +0 -1
- package/dist/NotificationServicesPushController/services/push/index.d.cts +0 -6
- package/dist/NotificationServicesPushController/services/push/index.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/services/push/index.d.mts +0 -6
- package/dist/NotificationServicesPushController/services/push/index.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/services/push/index.mjs +0 -2
- package/dist/NotificationServicesPushController/services/push/index.mjs.map +0 -1
- package/dist/NotificationServicesPushController/services/push/push-web.cjs.map +0 -1
- package/dist/NotificationServicesPushController/services/push/push-web.d.cts +0 -33
- package/dist/NotificationServicesPushController/services/push/push-web.d.cts.map +0 -1
- package/dist/NotificationServicesPushController/services/push/push-web.d.mts +0 -33
- package/dist/NotificationServicesPushController/services/push/push-web.d.mts.map +0 -1
- package/dist/NotificationServicesPushController/services/push/push-web.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-helpers.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-helpers.ts"],"names":[],"mappings":";;;;;;AAIA,sCAAqD;AACrD,kDAA2D;AAC3D,8CAI+B;AAE/B,wDAA2B;AAG3B,mGAAkE;AAClE,8EAA4E;AAM5E,2BAA2B;AAC3B,uDAAuD;AAC5C,QAAA,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;IACrC,sBAAc,KAAd,sBAAc,GAAK,MAAM,IAAA,gBAAW,GAAE,EAAC;IACvC,OAAO,sBAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI;QACF,OAAO,IAAA,YAAM,GAAE,CAAC;KACjB;IAAC,MAAM;QACN,MAAM,cAAc,GAAG;YACrB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,aAAa,EAAE,GAAG,CAAC,aAAa;YAChC,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;YACxC,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,aAAa,EAAE,GAAG,CAAC,aAAa;SACjC,CAAC;QACF,OAAO,IAAA,mBAAa,EAAC,cAAc,CAAC,CAAC;KACtC;AACH,CAAC,CAAC;AAEF,MAAM,oBAAoB,GAAG,KAAK,EAChC,GAAwB,EACG,EAAE;IAC7B,MAAM,SAAS,GAAG,MAAM,mBAAmB,EAAE,CAAC;IAC9C,IAAI,CAAC,SAAS,EAAE;QACd,OAAO,IAAI,CAAC;KACb;IAED,MAAM,GAAG,GAAG,MAAM,iBAAiB,CAAC,GAAG,CAAC,CAAC;IACzC,OAAO,IAA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AAE;YACrB,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,uDAAuD,EACvD,YAAY,CACb,CAAC;YACF,MAAM,KAAK,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;QAC9C,CAAC,CACF,CAAC;QACF,MAAM,UAAU,GAAG,gCAAgC,CACjD,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE;YACtB,KAAK,CAAC,SAAS,CAAC,OAAO,CACrB,4DAA4D,EAC5D,YAAY,CACb,CAAC;YACF,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;QAC5C,CAAC,CACF,CAAC;QAEF,MAAM,WAAW,GAAG,GAAG,EAAE;YACvB,sBAAsB,EAAE,EAAE,CAAC;YAC3B,UAAU,EAAE,CAAC;QACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAtCD,gFAsCC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport { Processors } from '../../NotificationServicesController';\nimport { toRawOnChainNotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async () => {\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n const data: Types.UnprocessedOnChainRawNotification | undefined =\n payload?.data?.data ? JSON.parse(payload?.data?.data) : undefined;\n\n if (!data) {\n return;\n }\n\n const notificationData = toRawOnChainNotification(data);\n const notification = Processors.processNotification(notificationData);\n await handler(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n throw new Error('Unable to send push notification');\n }\n },\n );\n\n const unsubscribe = () => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n) {\n const clickHandler = (event: NotificationEvent) => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = () =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}) {\n return async function (env: PushNotificationEnv) {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = () => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import type { Types } from "../../NotificationServicesController/index.cjs";
|
|
3
|
+
import type { NotificationServicesPushControllerMessenger } from "../NotificationServicesPushController.cjs";
|
|
4
|
+
import type { PushNotificationEnv } from "../types/firebase.cjs";
|
|
5
|
+
export declare let supportedCache: boolean | null;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a registration token for Firebase Cloud Messaging.
|
|
8
|
+
*
|
|
9
|
+
* @param env - env to configure push notifications
|
|
10
|
+
* @returns A promise that resolves with the registration token or null if an error occurs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createRegToken(env: PushNotificationEnv): Promise<string | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Deletes the Firebase Cloud Messaging registration token.
|
|
15
|
+
*
|
|
16
|
+
* @param env - env to configure push notifications
|
|
17
|
+
* @returns A promise that resolves with true if the token was successfully deleted, false otherwise.
|
|
18
|
+
*/
|
|
19
|
+
export declare function deleteRegToken(env: PushNotificationEnv): Promise<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* A creator function that assists creating web-specific push notification subscription:
|
|
22
|
+
* 1. Creates subscriptions for receiving and clicking notifications
|
|
23
|
+
* 2. Creates click events when a notification is clicked
|
|
24
|
+
* 3. Publishes controller messenger events
|
|
25
|
+
*
|
|
26
|
+
* @param props - props for this creator function.
|
|
27
|
+
* @param props.onReceivedHandler - allows the developer to handle showing a notification
|
|
28
|
+
* @param props.onClickHandler - allows the developer to handle clicking the notification
|
|
29
|
+
* @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events
|
|
30
|
+
* @returns a function that can be used by the controller
|
|
31
|
+
*/
|
|
32
|
+
export declare function createSubscribeToPushNotifications(props: {
|
|
33
|
+
onReceivedHandler: (notification: Types.INotification) => void | Promise<void>;
|
|
34
|
+
onClickHandler: (e: NotificationEvent, notification: Types.INotification) => void;
|
|
35
|
+
messenger: NotificationServicesPushControllerMessenger;
|
|
36
|
+
}): (env: PushNotificationEnv) => Promise<() => void>;
|
|
37
|
+
//# sourceMappingURL=push-helpers.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-helpers.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-helpers.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAGlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAsCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAqED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,SAC6B,mBAAmB,yBA4BhD"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import type { Types } from "../../NotificationServicesController/index.mjs";
|
|
3
|
+
import type { NotificationServicesPushControllerMessenger } from "../NotificationServicesPushController.mjs";
|
|
4
|
+
import type { PushNotificationEnv } from "../types/firebase.mjs";
|
|
5
|
+
export declare let supportedCache: boolean | null;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a registration token for Firebase Cloud Messaging.
|
|
8
|
+
*
|
|
9
|
+
* @param env - env to configure push notifications
|
|
10
|
+
* @returns A promise that resolves with the registration token or null if an error occurs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createRegToken(env: PushNotificationEnv): Promise<string | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Deletes the Firebase Cloud Messaging registration token.
|
|
15
|
+
*
|
|
16
|
+
* @param env - env to configure push notifications
|
|
17
|
+
* @returns A promise that resolves with true if the token was successfully deleted, false otherwise.
|
|
18
|
+
*/
|
|
19
|
+
export declare function deleteRegToken(env: PushNotificationEnv): Promise<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* A creator function that assists creating web-specific push notification subscription:
|
|
22
|
+
* 1. Creates subscriptions for receiving and clicking notifications
|
|
23
|
+
* 2. Creates click events when a notification is clicked
|
|
24
|
+
* 3. Publishes controller messenger events
|
|
25
|
+
*
|
|
26
|
+
* @param props - props for this creator function.
|
|
27
|
+
* @param props.onReceivedHandler - allows the developer to handle showing a notification
|
|
28
|
+
* @param props.onClickHandler - allows the developer to handle clicking the notification
|
|
29
|
+
* @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events
|
|
30
|
+
* @returns a function that can be used by the controller
|
|
31
|
+
*/
|
|
32
|
+
export declare function createSubscribeToPushNotifications(props: {
|
|
33
|
+
onReceivedHandler: (notification: Types.INotification) => void | Promise<void>;
|
|
34
|
+
onClickHandler: (e: NotificationEvent, notification: Types.INotification) => void;
|
|
35
|
+
messenger: NotificationServicesPushControllerMessenger;
|
|
36
|
+
}): (env: PushNotificationEnv) => Promise<() => void>;
|
|
37
|
+
//# sourceMappingURL=push-helpers.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-helpers.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-helpers.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,uDAA6C;AAGlE,OAAO,KAAK,EAAE,2CAA2C,EAAE,kDAA8C;AACzG,OAAO,KAAK,EAAE,mBAAmB,EAAE,8BAA0B;AAM7D,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAsCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAqED;;;;;;;;;;;GAWG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE;IACxD,iBAAiB,EAAE,CACjB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,cAAc,EAAE,CACd,CAAC,EAAE,iBAAiB,EACpB,YAAY,EAAE,KAAK,CAAC,aAAa,KAC9B,IAAI,CAAC;IACV,SAAS,EAAE,2CAA2C,CAAC;CACxD,SAC6B,mBAAmB,yBA4BhD"}
|
package/dist/NotificationServicesPushController/{services/push/push-web.mjs → web/push-helpers.mjs}
RENAMED
|
@@ -9,8 +9,8 @@ import { getToken, deleteToken } from "firebase/messaging";
|
|
|
9
9
|
import { getMessaging, onBackgroundMessage, isSupported } from "firebase/messaging/sw";
|
|
10
10
|
import $log from "loglevel";
|
|
11
11
|
const log = $importDefault($log);
|
|
12
|
-
import { Processors } from "
|
|
13
|
-
import { toRawOnChainNotification } from "
|
|
12
|
+
import { Processors } from "../../NotificationServicesController/index.mjs";
|
|
13
|
+
import { toRawOnChainNotification } from "../../shared/to-raw-notification.mjs";
|
|
14
14
|
// Exported to help testing
|
|
15
15
|
// eslint-disable-next-line import-x/no-mutable-exports
|
|
16
16
|
export let supportedCache = null;
|
|
@@ -80,17 +80,18 @@ export async function deleteRegToken(env) {
|
|
|
80
80
|
await deleteToken(messaging);
|
|
81
81
|
return true;
|
|
82
82
|
}
|
|
83
|
-
catch
|
|
83
|
+
catch {
|
|
84
84
|
return false;
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
88
|
* Service Worker Listener for when push notifications are received.
|
|
89
|
+
*
|
|
89
90
|
* @param env - push notification environment
|
|
90
|
-
* @param handler - handler to actually showing notification, MUST BE
|
|
91
|
+
* @param handler - handler to actually showing notification, MUST BE PROVIDED
|
|
91
92
|
* @returns unsubscribe handler
|
|
92
93
|
*/
|
|
93
|
-
|
|
94
|
+
async function listenToPushNotificationsReceived(env, handler) {
|
|
94
95
|
const messaging = await getFirebaseMessaging(env);
|
|
95
96
|
if (!messaging) {
|
|
96
97
|
return null;
|
|
@@ -125,7 +126,7 @@ export async function listenToPushNotificationsReceived(env, handler) {
|
|
|
125
126
|
* @param handler - listen to NotificationEvent from the service worker
|
|
126
127
|
* @returns unsubscribe handler
|
|
127
128
|
*/
|
|
128
|
-
|
|
129
|
+
function listenToPushNotificationsClicked(handler) {
|
|
129
130
|
const clickHandler = (event) => {
|
|
130
131
|
// Get Data
|
|
131
132
|
const data = event?.notification?.data;
|
|
@@ -135,4 +136,33 @@ export function listenToPushNotificationsClicked(handler) {
|
|
|
135
136
|
const unsubscribe = () => self.removeEventListener('notificationclick', clickHandler);
|
|
136
137
|
return unsubscribe;
|
|
137
138
|
}
|
|
138
|
-
|
|
139
|
+
/**
|
|
140
|
+
* A creator function that assists creating web-specific push notification subscription:
|
|
141
|
+
* 1. Creates subscriptions for receiving and clicking notifications
|
|
142
|
+
* 2. Creates click events when a notification is clicked
|
|
143
|
+
* 3. Publishes controller messenger events
|
|
144
|
+
*
|
|
145
|
+
* @param props - props for this creator function.
|
|
146
|
+
* @param props.onReceivedHandler - allows the developer to handle showing a notification
|
|
147
|
+
* @param props.onClickHandler - allows the developer to handle clicking the notification
|
|
148
|
+
* @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events
|
|
149
|
+
* @returns a function that can be used by the controller
|
|
150
|
+
*/
|
|
151
|
+
export function createSubscribeToPushNotifications(props) {
|
|
152
|
+
return async function (env) {
|
|
153
|
+
const onBackgroundMessageSub = await listenToPushNotificationsReceived(env, async (notification) => {
|
|
154
|
+
props.messenger.publish('NotificationServicesPushController:onNewNotifications', notification);
|
|
155
|
+
await props.onReceivedHandler(notification);
|
|
156
|
+
});
|
|
157
|
+
const onClickSub = listenToPushNotificationsClicked((event, notification) => {
|
|
158
|
+
props.messenger.publish('NotificationServicesPushController:pushNotificationClicked', notification);
|
|
159
|
+
props.onClickHandler(event, notification);
|
|
160
|
+
});
|
|
161
|
+
const unsubscribe = () => {
|
|
162
|
+
onBackgroundMessageSub?.();
|
|
163
|
+
onClickSub();
|
|
164
|
+
};
|
|
165
|
+
return unsubscribe;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=push-helpers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-helpers.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesPushController/web/push-helpers.ts"],"names":[],"mappings":";;;;;;AAIA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,qBAAqB;AACrD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,2BAA2B;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACZ,8BAA8B;AAE/B,OAAO,IAAG,iBAAiB;;AAG3B,OAAO,EAAE,UAAU,EAAE,uDAA6C;AAClE,OAAO,EAAE,wBAAwB,EAAE,6CAAyC;AAM5E,2BAA2B;AAC3B,uDAAuD;AACvD,MAAM,CAAC,IAAI,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;IACrC,cAAc,KAAd,cAAc,GAAK,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ACf,CAAC,CAAC;QAEF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../NotificationServicesController';\nimport { Processors } from '../../NotificationServicesController';\nimport { toRawOnChainNotification } from '../../shared/to-raw-notification';\nimport type { NotificationServicesPushControllerMessenger } from '../NotificationServicesPushController';\nimport type { PushNotificationEnv } from '../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async () => {\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n *\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVIDED\n * @returns unsubscribe handler\n */\nasync function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n const data: Types.UnprocessedOnChainRawNotification | undefined =\n payload?.data?.data ? JSON.parse(payload?.data?.data) : undefined;\n\n if (!data) {\n return;\n }\n\n const notificationData = toRawOnChainNotification(data);\n const notification = Processors.processNotification(notificationData);\n await handler(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n throw new Error('Unable to send push notification');\n }\n },\n );\n\n const unsubscribe = () => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nfunction listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification: Types.INotification) => void,\n) {\n const clickHandler = (event: NotificationEvent) => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = () =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n\n/**\n * A creator function that assists creating web-specific push notification subscription:\n * 1. Creates subscriptions for receiving and clicking notifications\n * 2. Creates click events when a notification is clicked\n * 3. Publishes controller messenger events\n *\n * @param props - props for this creator function.\n * @param props.onReceivedHandler - allows the developer to handle showing a notification\n * @param props.onClickHandler - allows the developer to handle clicking the notification\n * @param props.messenger - the controller messenger to publish the `onNewNotifications` and `pushNotificationsClicked` events\n * @returns a function that can be used by the controller\n */\nexport function createSubscribeToPushNotifications(props: {\n onReceivedHandler: (\n notification: Types.INotification,\n ) => void | Promise<void>;\n onClickHandler: (\n e: NotificationEvent,\n notification: Types.INotification,\n ) => void;\n messenger: NotificationServicesPushControllerMessenger;\n}) {\n return async function (env: PushNotificationEnv) {\n const onBackgroundMessageSub = await listenToPushNotificationsReceived(\n env,\n async (notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:onNewNotifications',\n notification,\n );\n await props.onReceivedHandler(notification);\n },\n );\n const onClickSub = listenToPushNotificationsClicked(\n (event, notification) => {\n props.messenger.publish(\n 'NotificationServicesPushController:pushNotificationClicked',\n notification,\n );\n props.onClickHandler(event, notification);\n },\n );\n\n const unsubscribe = () => {\n onBackgroundMessageSub?.();\n onClickSub();\n };\n\n return unsubscribe;\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/notification-services-controller",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0-preview-442894ce",
|
|
4
4
|
"description": "Manages New MetaMask decentralized Notification system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -67,6 +67,16 @@
|
|
|
67
67
|
"default": "./dist/NotificationServicesPushController/index.cjs"
|
|
68
68
|
}
|
|
69
69
|
},
|
|
70
|
+
"./push-services/web": {
|
|
71
|
+
"import": {
|
|
72
|
+
"types": "./dist/NotificationServicesPushController/web/index.d.mts",
|
|
73
|
+
"default": "./dist/NotificationServicesPushController/web/index.mjs"
|
|
74
|
+
},
|
|
75
|
+
"require": {
|
|
76
|
+
"types": "./dist/NotificationServicesPushController/web/index.d.cts",
|
|
77
|
+
"default": "./dist/NotificationServicesPushController/web/index.cjs"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
70
80
|
"./push-services/mocks": {
|
|
71
81
|
"import": {
|
|
72
82
|
"types": "./dist/NotificationServicesPushController/__fixtures__/index.d.mts",
|
|
@@ -112,8 +122,8 @@
|
|
|
112
122
|
"@lavamoat/allow-scripts": "^3.0.4",
|
|
113
123
|
"@lavamoat/preinstall-always-fail": "^2.1.0",
|
|
114
124
|
"@metamask/auto-changelog": "^3.4.4",
|
|
115
|
-
"@metamask/keyring-controller": "^19.0.
|
|
116
|
-
"@metamask/profile-sync-controller": "^
|
|
125
|
+
"@metamask/keyring-controller": "^19.0.5",
|
|
126
|
+
"@metamask/profile-sync-controller": "^5.0.0",
|
|
117
127
|
"@types/jest": "^27.4.1",
|
|
118
128
|
"@types/readable-stream": "^2.3.0",
|
|
119
129
|
"contentful": "^10.15.0",
|
|
@@ -128,7 +138,7 @@
|
|
|
128
138
|
},
|
|
129
139
|
"peerDependencies": {
|
|
130
140
|
"@metamask/keyring-controller": "^19.0.0",
|
|
131
|
-
"@metamask/profile-sync-controller": "^
|
|
141
|
+
"@metamask/profile-sync-controller": "^5.0.0"
|
|
132
142
|
},
|
|
133
143
|
"engines": {
|
|
134
144
|
"node": "^18.18 || >=20"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/index.ts"],"names":[],"mappings":"","sourcesContent":["import type * as Web from './push-web';\n\nexport type CreateRegToken = typeof Web.createRegToken;\nexport type DeleteRegToken = typeof Web.deleteRegToken;\nexport type ListenToPushNotificationsReceived =\n typeof Web.listenToPushNotificationsReceived;\nexport type ListenToPushNotificationsClicked =\n typeof Web.listenToPushNotificationsClicked;\n"]}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type * as Web from "./push-web.cjs";
|
|
2
|
-
export type CreateRegToken = typeof Web.createRegToken;
|
|
3
|
-
export type DeleteRegToken = typeof Web.deleteRegToken;
|
|
4
|
-
export type ListenToPushNotificationsReceived = typeof Web.listenToPushNotificationsReceived;
|
|
5
|
-
export type ListenToPushNotificationsClicked = typeof Web.listenToPushNotificationsClicked;
|
|
6
|
-
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,GAAG,uBAAmB;AAEvC,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,CAAC,cAAc,CAAC;AACvD,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,CAAC,cAAc,CAAC;AACvD,MAAM,MAAM,iCAAiC,GAC3C,OAAO,GAAG,CAAC,iCAAiC,CAAC;AAC/C,MAAM,MAAM,gCAAgC,GAC1C,OAAO,GAAG,CAAC,gCAAgC,CAAC"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type * as Web from "./push-web.mjs";
|
|
2
|
-
export type CreateRegToken = typeof Web.createRegToken;
|
|
3
|
-
export type DeleteRegToken = typeof Web.deleteRegToken;
|
|
4
|
-
export type ListenToPushNotificationsReceived = typeof Web.listenToPushNotificationsReceived;
|
|
5
|
-
export type ListenToPushNotificationsClicked = typeof Web.listenToPushNotificationsClicked;
|
|
6
|
-
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,GAAG,uBAAmB;AAEvC,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,CAAC,cAAc,CAAC;AACvD,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,CAAC,cAAc,CAAC;AACvD,MAAM,MAAM,iCAAiC,GAC3C,OAAO,GAAG,CAAC,iCAAiC,CAAC;AAC/C,MAAM,MAAM,gCAAgC,GAC1C,OAAO,GAAG,CAAC,gCAAgC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/index.ts"],"names":[],"mappings":"","sourcesContent":["import type * as Web from './push-web';\n\nexport type CreateRegToken = typeof Web.createRegToken;\nexport type DeleteRegToken = typeof Web.deleteRegToken;\nexport type ListenToPushNotificationsReceived =\n typeof Web.listenToPushNotificationsReceived;\nexport type ListenToPushNotificationsClicked =\n typeof Web.listenToPushNotificationsClicked;\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"push-web.cjs","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/push-web.ts"],"names":[],"mappings":";;;;;;AAIA,sCAAqD;AACrD,kDAA2D;AAC3D,8CAI+B;AAE/B,wDAA2B;AAG3B,sGAAqE;AACrE,iFAA+E;AAK/E,2BAA2B;AAC3B,uDAAuD;AAC5C,QAAA,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;IACrC,sBAAc,KAAd,sBAAc,GAAK,MAAM,IAAA,gBAAW,GAAE,EAAC;IACvC,OAAO,sBAAc,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,KAAK,EAC7B,GAAwB,EACF,EAAE;IACxB,IAAI;QACF,OAAO,IAAA,YAAM,GAAE,CAAC;KACjB;IAAC,MAAM;QACN,MAAM,cAAc,GAAG;YACrB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,aAAa,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B,EAAE,EAAE;QAChD,WAAW;QACX,MAAM,IAAI,GAAwB,KAAK,EAAE,YAAY,EAAE,IAAI,CAAC;QAC5D,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACvB,CAAC,CAAC;IAEF,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IACzD,MAAM,WAAW,GAAG,GAAG,EAAE,CACvB,IAAI,CAAC,mBAAmB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IAC9D,OAAO,WAAW,CAAC;AACrB,CAAC;AAbD,4EAaC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../../NotificationServicesController';\nimport { Processors } from '../../../NotificationServicesController';\nimport { toRawOnChainNotification } from '../../../shared/to-raw-notification';\nimport type { PushNotificationEnv } from '../../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async () => {\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVEDED\n * @returns unsubscribe handler\n */\nexport async function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n const data: Types.UnprocessedOnChainRawNotification | undefined =\n payload?.data?.data ? JSON.parse(payload?.data?.data) : undefined;\n\n if (!data) {\n return;\n }\n\n const notificationData = toRawOnChainNotification(data);\n const notification = Processors.processNotification(notificationData);\n await handler(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n throw new Error('Unable to send push notification');\n }\n },\n );\n\n const unsubscribe = () => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nexport function listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification?: Types.INotification) => void,\n) {\n const clickHandler = (event: NotificationEvent) => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = () =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n"]}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/// <reference lib="webworker" />
|
|
2
|
-
import type { Types } from "../../../NotificationServicesController/index.cjs";
|
|
3
|
-
import type { PushNotificationEnv } from "../../types/firebase.cjs";
|
|
4
|
-
export declare let supportedCache: boolean | null;
|
|
5
|
-
/**
|
|
6
|
-
* Creates a registration token for Firebase Cloud Messaging.
|
|
7
|
-
*
|
|
8
|
-
* @param env - env to configure push notifications
|
|
9
|
-
* @returns A promise that resolves with the registration token or null if an error occurs.
|
|
10
|
-
*/
|
|
11
|
-
export declare function createRegToken(env: PushNotificationEnv): Promise<string | null>;
|
|
12
|
-
/**
|
|
13
|
-
* Deletes the Firebase Cloud Messaging registration token.
|
|
14
|
-
*
|
|
15
|
-
* @param env - env to configure push notifications
|
|
16
|
-
* @returns A promise that resolves with true if the token was successfully deleted, false otherwise.
|
|
17
|
-
*/
|
|
18
|
-
export declare function deleteRegToken(env: PushNotificationEnv): Promise<boolean>;
|
|
19
|
-
/**
|
|
20
|
-
* Service Worker Listener for when push notifications are received.
|
|
21
|
-
* @param env - push notification environment
|
|
22
|
-
* @param handler - handler to actually showing notification, MUST BE PROVEDED
|
|
23
|
-
* @returns unsubscribe handler
|
|
24
|
-
*/
|
|
25
|
-
export declare function listenToPushNotificationsReceived(env: PushNotificationEnv, handler: (notification: Types.INotification) => void | Promise<void>): Promise<(() => void) | null>;
|
|
26
|
-
/**
|
|
27
|
-
* Service Worker Listener for when a notification is clicked
|
|
28
|
-
*
|
|
29
|
-
* @param handler - listen to NotificationEvent from the service worker
|
|
30
|
-
* @returns unsubscribe handler
|
|
31
|
-
*/
|
|
32
|
-
export declare function listenToPushNotificationsClicked(handler: (e: NotificationEvent, notification?: Types.INotification) => void): () => void;
|
|
33
|
-
//# sourceMappingURL=push-web.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"push-web.d.cts","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/push-web.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,0DAAgD;AAGrE,OAAO,KAAK,EAAE,mBAAmB,EAAE,iCAA6B;AAMhE,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAsCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAED;;;;;GAKG;AACH,wBAAsB,iCAAiC,CACrD,GAAG,EAAE,mBAAmB,EACxB,OAAO,EAAE,CAAC,YAAY,EAAE,KAAK,CAAC,aAAa,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GACnE,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,CAkC9B;AAED;;;;;GAKG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,CAAC,CAAC,EAAE,iBAAiB,EAAE,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAAK,IAAI,cAY5E"}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/// <reference lib="webworker" />
|
|
2
|
-
import type { Types } from "../../../NotificationServicesController/index.mjs";
|
|
3
|
-
import type { PushNotificationEnv } from "../../types/firebase.mjs";
|
|
4
|
-
export declare let supportedCache: boolean | null;
|
|
5
|
-
/**
|
|
6
|
-
* Creates a registration token for Firebase Cloud Messaging.
|
|
7
|
-
*
|
|
8
|
-
* @param env - env to configure push notifications
|
|
9
|
-
* @returns A promise that resolves with the registration token or null if an error occurs.
|
|
10
|
-
*/
|
|
11
|
-
export declare function createRegToken(env: PushNotificationEnv): Promise<string | null>;
|
|
12
|
-
/**
|
|
13
|
-
* Deletes the Firebase Cloud Messaging registration token.
|
|
14
|
-
*
|
|
15
|
-
* @param env - env to configure push notifications
|
|
16
|
-
* @returns A promise that resolves with true if the token was successfully deleted, false otherwise.
|
|
17
|
-
*/
|
|
18
|
-
export declare function deleteRegToken(env: PushNotificationEnv): Promise<boolean>;
|
|
19
|
-
/**
|
|
20
|
-
* Service Worker Listener for when push notifications are received.
|
|
21
|
-
* @param env - push notification environment
|
|
22
|
-
* @param handler - handler to actually showing notification, MUST BE PROVEDED
|
|
23
|
-
* @returns unsubscribe handler
|
|
24
|
-
*/
|
|
25
|
-
export declare function listenToPushNotificationsReceived(env: PushNotificationEnv, handler: (notification: Types.INotification) => void | Promise<void>): Promise<(() => void) | null>;
|
|
26
|
-
/**
|
|
27
|
-
* Service Worker Listener for when a notification is clicked
|
|
28
|
-
*
|
|
29
|
-
* @param handler - listen to NotificationEvent from the service worker
|
|
30
|
-
* @returns unsubscribe handler
|
|
31
|
-
*/
|
|
32
|
-
export declare function listenToPushNotificationsClicked(handler: (e: NotificationEvent, notification?: Types.INotification) => void): () => void;
|
|
33
|
-
//# sourceMappingURL=push-web.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"push-web.d.mts","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/push-web.ts"],"names":[],"mappings":";AAcA,OAAO,KAAK,EAAE,KAAK,EAAE,0DAAgD;AAGrE,OAAO,KAAK,EAAE,mBAAmB,EAAE,iCAA6B;AAMhE,eAAO,IAAI,cAAc,EAAE,OAAO,GAAG,IAAW,CAAC;AAsCjD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAexB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,mBAAmB,GACvB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAED;;;;;GAKG;AACH,wBAAsB,iCAAiC,CACrD,GAAG,EAAE,mBAAmB,EACxB,OAAO,EAAE,CAAC,YAAY,EAAE,KAAK,CAAC,aAAa,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GACnE,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,CAkC9B;AAED;;;;;GAKG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,CAAC,CAAC,EAAE,iBAAiB,EAAE,YAAY,CAAC,EAAE,KAAK,CAAC,aAAa,KAAK,IAAI,cAY5E"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"push-web.mjs","sourceRoot":"","sources":["../../../../src/NotificationServicesPushController/services/push/push-web.ts"],"names":[],"mappings":";;;;;;AAIA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,qBAAqB;AACrD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,2BAA2B;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,WAAW,EACZ,8BAA8B;AAE/B,OAAO,IAAG,iBAAiB;;AAG3B,OAAO,EAAE,UAAU,EAAE,0DAAgD;AACrE,OAAO,EAAE,wBAAwB,EAAE,gDAA4C;AAK/E,2BAA2B;AAC3B,uDAAuD;AACvD,MAAM,CAAC,IAAI,cAAc,GAAmB,IAAI,CAAC;AAEjD,MAAM,mBAAmB,GAAG,KAAK,IAAI,EAAE;IACrC,cAAc,KAAd,cAAc,GAAK,MAAM,WAAW,EAAE,EAAC;IACvC,OAAO,cAAc,CAAC;AACxB,CAAC,CAA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AM,WAAW,GAAG,GAAG,EAAE,CAAC,4BAA4B,EAAE,CAAC;IACzD,OAAO,WAAW,CAAC;AACrB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gCAAgC,CAC9C,OAA2E;IAE3E,MAAM,YAAY,GAAG,CAAC,KAAwB,EAAE,EAAE;QAChD,WAAW;QACX,MAAM,IAAI,GAAwB,KAAK,EAAE,YAAY,EAAE,IAAI,CAAC;QAC5D,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACvB,CAAC,CAAC;IAEF,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IACzD,MAAM,WAAW,GAAG,GAAG,EAAE,CACvB,IAAI,CAAC,mBAAmB,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;IAC9D,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["// We are defining that this file uses a webworker global scope.\n// eslint-disable-next-line spaced-comment\n/// <reference lib=\"webworker\" />\nimport type { FirebaseApp } from 'firebase/app';\nimport { getApp, initializeApp } from 'firebase/app';\nimport { getToken, deleteToken } from 'firebase/messaging';\nimport {\n getMessaging,\n onBackgroundMessage,\n isSupported,\n} from 'firebase/messaging/sw';\nimport type { Messaging, MessagePayload } from 'firebase/messaging/sw';\nimport log from 'loglevel';\n\nimport type { Types } from '../../../NotificationServicesController';\nimport { Processors } from '../../../NotificationServicesController';\nimport { toRawOnChainNotification } from '../../../shared/to-raw-notification';\nimport type { PushNotificationEnv } from '../../types/firebase';\n\ndeclare const self: ServiceWorkerGlobalScope;\n\n// Exported to help testing\n// eslint-disable-next-line import-x/no-mutable-exports\nexport let supportedCache: boolean | null = null;\n\nconst getPushAvailability = async () => {\n supportedCache ??= await isSupported();\n return supportedCache;\n};\n\nconst createFirebaseApp = async (\n env: PushNotificationEnv,\n): Promise<FirebaseApp> => {\n try {\n return getApp();\n } catch {\n const firebaseConfig = {\n apiKey: env.apiKey,\n authDomain: env.authDomain,\n storageBucket: env.storageBucket,\n projectId: env.projectId,\n messagingSenderId: env.messagingSenderId,\n appId: env.appId,\n measurementId: env.measurementId,\n };\n return initializeApp(firebaseConfig);\n }\n};\n\nconst getFirebaseMessaging = async (\n env: PushNotificationEnv,\n): Promise<Messaging | null> => {\n const supported = await getPushAvailability();\n if (!supported) {\n return null;\n }\n\n const app = await createFirebaseApp(env);\n return getMessaging(app);\n};\n\n/**\n * Creates a registration token for Firebase Cloud Messaging.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with the registration token or null if an error occurs.\n */\nexport async function createRegToken(\n env: PushNotificationEnv,\n): Promise<string | null> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const token = await getToken(messaging, {\n serviceWorkerRegistration: self.registration,\n vapidKey: env.vapidKey,\n });\n return token;\n } catch {\n return null;\n }\n}\n\n/**\n * Deletes the Firebase Cloud Messaging registration token.\n *\n * @param env - env to configure push notifications\n * @returns A promise that resolves with true if the token was successfully deleted, false otherwise.\n */\nexport async function deleteRegToken(\n env: PushNotificationEnv,\n): Promise<boolean> {\n try {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return true;\n }\n\n await deleteToken(messaging);\n return true;\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Service Worker Listener for when push notifications are received.\n * @param env - push notification environment\n * @param handler - handler to actually showing notification, MUST BE PROVEDED\n * @returns unsubscribe handler\n */\nexport async function listenToPushNotificationsReceived(\n env: PushNotificationEnv,\n handler: (notification: Types.INotification) => void | Promise<void>,\n): Promise<(() => void) | null> {\n const messaging = await getFirebaseMessaging(env);\n if (!messaging) {\n return null;\n }\n\n const unsubscribePushNotifications = onBackgroundMessage(\n messaging,\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (payload: MessagePayload) => {\n try {\n const data: Types.UnprocessedOnChainRawNotification | undefined =\n payload?.data?.data ? JSON.parse(payload?.data?.data) : undefined;\n\n if (!data) {\n return;\n }\n\n const notificationData = toRawOnChainNotification(data);\n const notification = Processors.processNotification(notificationData);\n await handler(notification);\n } catch (error) {\n // Do Nothing, cannot parse a bad notification\n log.error('Unable to send push notification:', {\n notification: payload?.data?.data,\n error,\n });\n throw new Error('Unable to send push notification');\n }\n },\n );\n\n const unsubscribe = () => unsubscribePushNotifications();\n return unsubscribe;\n}\n\n/**\n * Service Worker Listener for when a notification is clicked\n *\n * @param handler - listen to NotificationEvent from the service worker\n * @returns unsubscribe handler\n */\nexport function listenToPushNotificationsClicked(\n handler: (e: NotificationEvent, notification?: Types.INotification) => void,\n) {\n const clickHandler = (event: NotificationEvent) => {\n // Get Data\n const data: Types.INotification = event?.notification?.data;\n handler(event, data);\n };\n\n self.addEventListener('notificationclick', clickHandler);\n const unsubscribe = () =>\n self.removeEventListener('notificationclick', clickHandler);\n return unsubscribe;\n}\n"]}
|